From 912f9f5c452b8e2a2c3a9099ca5ae02752d0299b Mon Sep 17 00:00:00 2001 From: dkoo Date: Fri, 8 Dec 2023 13:36:14 -0700 Subject: [PATCH 1/4] feat: add support for optional order notes field --- includes/class-modal-checkout.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/includes/class-modal-checkout.php b/includes/class-modal-checkout.php index 89cfbe51f..0dac3a0e8 100644 --- a/includes/class-modal-checkout.php +++ b/includes/class-modal-checkout.php @@ -602,6 +602,12 @@ public static function should_show_order_details() { if ( 1 < $cart->get_cart_contents_count() ) { return true; } + if ( $cart->needs_shipping_address() ) { + $totals = $cart->get_totals(); + if ( (float) $totals['total'] !== (float) $totals['subtotal'] ) { + return true; + } + } return false; } @@ -947,7 +953,7 @@ public static function render_before_terms_and_conditions() { } /** - * Disable order notes in the modal checkout. + * Maybe disable order notes in the modal checkout. * * @param bool $enable Whether to enable the order notes field. * @@ -955,7 +961,8 @@ public static function render_before_terms_and_conditions() { */ public static function enable_order_notes_field( $enable ) { if ( self::is_modal_checkout() ) { - return false; + $billing_fields = apply_filters( 'newspack_blocks_donate_billing_fields_keys', [] ); + return in_array( 'order_comments', $billing_fields, true ); } return $enable; } From 31bf8d51690a18c647b0da60297a793b0198520b Mon Sep 17 00:00:00 2001 From: dkoo Date: Fri, 8 Dec 2023 18:13:54 -0700 Subject: [PATCH 2/4] feat: support new option for non-donation billing fields --- includes/class-modal-checkout.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/includes/class-modal-checkout.php b/includes/class-modal-checkout.php index 0dac3a0e8..c821c486a 100644 --- a/includes/class-modal-checkout.php +++ b/includes/class-modal-checkout.php @@ -569,7 +569,13 @@ public static function woocommerce_checkout_fields( $fields ) { if ( $cart->needs_shipping_address() ) { // Don't modify fields if shipping is required. return $fields; } - $billing_fields = apply_filters( 'newspack_blocks_donate_billing_fields_keys', [] ); + + /** + * Filters the billing fields to show in the modal checkout. + * + * @param WC_Cart $cart The current checkout cart. + */ + $billing_fields = apply_filters( 'newspack_blocks_donate_billing_fields_keys', [], $cart ); if ( empty( $billing_fields ) ) { return $fields; } @@ -961,7 +967,8 @@ public static function render_before_terms_and_conditions() { */ public static function enable_order_notes_field( $enable ) { if ( self::is_modal_checkout() ) { - $billing_fields = apply_filters( 'newspack_blocks_donate_billing_fields_keys', [] ); + $cart = \WC()->cart; + $billing_fields = apply_filters( 'newspack_blocks_donate_billing_fields_keys', [], $cart ); return in_array( 'order_comments', $billing_fields, true ); } return $enable; From 93fd32a5a836a1dafaa0192978428e3385c0eb82 Mon Sep 17 00:00:00 2001 From: dkoo Date: Fri, 8 Dec 2023 19:01:55 -0700 Subject: [PATCH 3/4] fix: show order details if more than one shipping method --- includes/class-modal-checkout.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/includes/class-modal-checkout.php b/includes/class-modal-checkout.php index c821c486a..cdf66ba87 100644 --- a/includes/class-modal-checkout.php +++ b/includes/class-modal-checkout.php @@ -609,8 +609,11 @@ public static function should_show_order_details() { return true; } if ( $cart->needs_shipping_address() ) { - $totals = $cart->get_totals(); - if ( (float) $totals['total'] !== (float) $totals['subtotal'] ) { + $shipping = \WC()->shipping; + $totals = $cart->get_totals(); + + // Show details if shipping requires a fee or if there are multiple shipping methods to choose from. + if ( (float) $totals['total'] !== (float) $totals['subtotal'] || 1 < count( $shipping->get_shipping_methods() ) ) { return true; } } From a4ca300fb7340164b33996ada74c6606aea122db Mon Sep 17 00:00:00 2001 From: dkoo Date: Wed, 13 Dec 2023 19:58:27 -0700 Subject: [PATCH 4/4] fix: check shipping rates only for current transaction --- includes/class-modal-checkout.php | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/includes/class-modal-checkout.php b/includes/class-modal-checkout.php index cdf66ba87..34066813e 100644 --- a/includes/class-modal-checkout.php +++ b/includes/class-modal-checkout.php @@ -609,11 +609,22 @@ public static function should_show_order_details() { return true; } if ( $cart->needs_shipping_address() ) { - $shipping = \WC()->shipping; - $totals = $cart->get_totals(); + $shipping = \WC()->shipping; + $packages = $shipping->get_packages(); + $totals = $cart->get_totals(); + $shipping_rates = []; + + // Find all the shipping rates that apply to the current transaction. + foreach ( $packages as $package ) { + if ( ! empty( $package['rates'] ) ) { + foreach ( $package['rates'] as $rate_key => $rate ) { + $shipping_rates[ $rate_key ] = $rate; + } + } + } - // Show details if shipping requires a fee or if there are multiple shipping methods to choose from. - if ( (float) $totals['total'] !== (float) $totals['subtotal'] || 1 < count( $shipping->get_shipping_methods() ) ) { + // Show details if shipping requires a fee or if there are multiple shipping rates to choose from. + if ( (float) $totals['total'] !== (float) $totals['subtotal'] || 1 < count( array_values( $shipping_rates ) ) ) { return true; } }