Skip to content

Commit

Permalink
Format decimal before retrieving, e.g. shipping item total as the val…
Browse files Browse the repository at this point in the history
…ue may be an empty string. Added render context for checkboxes which applies the necessary filters (to trigger show/hide logic) automatically. Refactored checkout checkbox storage.
  • Loading branch information
dennisnissle committed Sep 29, 2023
1 parent ed1a097 commit bb91ee9
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 78 deletions.
8 changes: 4 additions & 4 deletions includes/admin/class-wc-gzd-admin-order.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,21 +364,21 @@ public function adjust_item_taxes( $item, $calculate_tax_for = array() ) {
protected function get_item_total( $item, $old_item = false ) {
// Let's grab a fresh copy (loaded from DB) to make sure we are not dependent on Woo's calculated taxes in $item.
if ( $old_item ) {
$item_total = $old_item->get_total();
$item_total = wc_format_decimal( floatval( $old_item->get_total() ) );

if ( wc_gzd_additional_costs_include_tax() ) {
$item_total += $old_item->get_total_tax();
$item_total += wc_format_decimal( floatval( $old_item->get_total_tax() ) );
}
} else {
$item_total = $item->get_total();
$item_total = wc_format_decimal( floatval( $item->get_total() ) );
$is_adding_item = wp_doing_ajax() && isset( $_POST['action'] ) && in_array( wp_unslash( $_POST['action'] ), array( 'woocommerce_add_order_fee', 'woocommerce_add_order_shipping' ), true ); // phpcs:ignore WordPress.Security.NonceVerification.Missing

/**
* When adding a fee through the admin panel, Woo by default calculates taxes
* based on the fee's tax class (which by default is standard). Ignore the tax data on first call.
*/
if ( ! $is_adding_item && wc_gzd_additional_costs_include_tax() ) {
$item_total += $item->get_total_tax();
$item_total += wc_format_decimal( floatval( $item->get_total_tax() ) );
}
}

Expand Down
94 changes: 37 additions & 57 deletions includes/class-wc-gzd-checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,7 @@ public function __construct() {
}

add_action( 'woocommerce_checkout_create_order', array( $this, 'order_meta' ), 5, 1 );
add_action( 'woocommerce_checkout_create_order', array( $this, 'order_store_checkbox_data' ), 10, 2 );
add_action( 'woocommerce_checkout_create_order', array( $this, 'order_age_verification' ), 20, 2 );
add_action( 'woocommerce_checkout_create_order', array( $this, 'order_store_checkbox_data' ), 10, 1 );
add_action( 'woocommerce_checkout_order_created', array( $this, 'add_order_notes' ), 20 );

// Make sure that, just like in Woo core, the order submit button gets refreshed
Expand Down Expand Up @@ -227,7 +226,7 @@ public function get_checkout_value( $key ) {
}
}

return $value;
return apply_filters( 'woocommerce_gzd_get_checkout_value', $value, $key );
}

public function refresh_photovoltaic_systems_notice( $fragments ) {
Expand Down Expand Up @@ -560,41 +559,40 @@ public function refresh_order_submit( $fragments ) {

/**
* @param WC_Order $order
* @param $posted
*/
public function order_store_checkbox_data( $order, $posted ) {
public function order_store_checkbox_data( $order ) {
if ( $checkbox = wc_gzd_get_legal_checkbox( 'parcel_delivery' ) ) {
if ( $checkbox->is_enabled() && $order->has_shipping_address() && wc_gzd_is_parcel_delivery_data_transfer_checkbox_enabled( wc_gzd_get_chosen_shipping_rates( array( 'value' => 'id' ) ) ) ) {
$selected = false;
if ( $checkbox->is_enabled() && $order->has_shipping_address() ) {
$method_ids = array();
$items = $order->get_shipping_methods();

if ( isset( $_POST[ $checkbox->get_html_name() ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
$selected = true;
} elseif ( $checkbox->hide_input() ) {
$selected = true;
foreach ( $items as $item ) {
$method_ids[] = $item->get_method_id();
}

$order->update_meta_data( '_parcel_delivery_opted_in', $selected ? 'yes' : 'no' );
if ( wc_gzd_is_parcel_delivery_data_transfer_checkbox_enabled( $method_ids ) ) {
$selected = $this->checkbox_is_checked( $checkbox );

/**
* Parcel delivery notification.
*
* Execute whenever the parcel delivery notification data is stored for a certain order.
*
* @param int $order_id The order id.
* @param bool $selected True if the checkbox was checked. False otherwise.
*
* @since 1.7.2
*/
do_action( 'woocommerce_gzd_parcel_delivery_order_opted_in', $order->get_id(), $selected );
$order->update_meta_data( '_parcel_delivery_opted_in', $selected ? 'yes' : 'no' );

/**
* Parcel delivery notification.
*
* Execute whenever the parcel delivery notification data is stored for a certain order.
*
* @param int $order_id The order id.
* @param bool $selected True if the checkbox was checked. False otherwise.
*
* @since 1.7.2
*/
do_action( 'woocommerce_gzd_parcel_delivery_order_opted_in', $order->get_id(), $selected );
}
}
}

if ( $checkbox = wc_gzd_get_legal_checkbox( 'photovoltaic_systems' ) ) {
if ( $checkbox->is_enabled() && wc_gzd_cart_contains_photovoltaic_system() ) {
$value = WC()->checkout()->get_value( $checkbox->get_html_name() );
$visible = WC()->checkout()->get_value( $checkbox->get_html_name() . '-field' );

if ( $visible && ( ! empty( $value ) || $checkbox->hide_input() ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
if ( $this->checkbox_is_checked( $checkbox ) ) {
$order->update_meta_data( '_photovoltaic_systems_opted_in', 'yes' );

/**
Expand All @@ -612,6 +610,18 @@ public function order_store_checkbox_data( $order, $posted ) {
}
}
}

if ( $checkbox = wc_gzd_get_legal_checkbox( 'age_verification' ) ) {
if ( $checkbox->is_enabled() && wc_gzd_cart_needs_age_verification( $order->get_items() ) ) {
if ( $min_age = wc_gzd_cart_get_age_verification_min_age( $order->get_items() ) ) {
if ( $this->checkbox_is_checked( $checkbox ) ) {
$order->update_meta_data( '_min_age', $min_age );
}
}
}
}

do_action( 'woocommerce_gzd_checkout_store_checkbox_data', $order, $this );
}

/**
Expand All @@ -633,36 +643,6 @@ public function add_order_notes( $order ) {
}
}

/**
* @param WC_Order $order
* @param $posted
*/
public function order_age_verification( $order, $posted ) {
if ( $checkbox = wc_gzd_get_legal_checkbox( 'age_verification' ) ) {

if ( ! $checkbox->is_enabled() ) {
return;
}

if ( ! wc_gzd_cart_needs_age_verification( $order->get_items() ) ) {
return;
}

$min_age = wc_gzd_cart_get_age_verification_min_age( $order->get_items() );

if ( ! $min_age ) {
return;
}

// Checkbox has not been checked
if ( ! isset( $_POST[ $checkbox->get_html_name() ] ) && ! $checkbox->hide_input() ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
return;
}

$order->update_meta_data( '_min_age', $min_age );
}
}

public function set_payment_url_to_force_payment( $url, $order ) {

/**
Expand Down
8 changes: 8 additions & 0 deletions includes/class-wc-gzd-legal-checkbox-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -1034,6 +1034,14 @@ public function get_checkboxes( $args = array(), $context = '' ) {
)
);

if ( 'render' === $context && ! empty( $args['locations'] ) ) {
$locations = (array) $args['locations'];

foreach ( $locations as $location ) {
$this->maybe_do_hooks( $location );
}
}

$sort = $args['sort'];
$filter_args = array_diff_key( $args, array( 'sort' => '' ) );
$checkboxes = $this->filter( $filter_args, 'AND' );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,31 @@ class WC_GZD_Gateway_Direct_Debit extends WC_Payment_Gateway {

public static $has_loaded = false;

protected $enable_pre_notification;
protected $enable_pre_notification;

protected $debit_days;
protected $debit_days;

protected $generate_mandate_id;
protected $generate_mandate_id;

protected $mandate_id_format;
protected $mandate_id_format;

protected $company_info;
protected $company_info;

protected $company_identification_number;
protected $company_identification_number;

protected $company_account_holder;
protected $company_account_holder;

protected $company_account_iban;
protected $company_account_iban;

protected $company_account_bic;
protected $company_account_bic;

protected $pain_format;
protected $pain_format;

protected $remember;
protected $remember;

protected $mask;
protected $mask;

protected $mandate_text;
protected $mandate_text;

public $admin_fields = array();

Expand Down
6 changes: 3 additions & 3 deletions includes/gateways/invoice/class-wc-gzd-gateway-invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
*/
class WC_GZD_Gateway_Invoice extends WC_Payment_Gateway {

protected $default_order_status;
protected $default_order_status;

protected $customers_only;
protected $customers_only;

protected $customers_completed;
protected $customers_completed;

/**
* Constructor for the gateway.
Expand Down
1 change: 0 additions & 1 deletion includes/wc-gzd-cart-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -1210,7 +1210,6 @@ function wc_gzd_get_legal_text_service_email_notice() {
}

function wc_gzd_get_chosen_shipping_rates( $args = array() ) {

$args = wp_parse_args(
$args,
array(
Expand Down

0 comments on commit bb91ee9

Please sign in to comment.