From bde2f0edf2202b15950e92ce0f1392ab14df42af Mon Sep 17 00:00:00 2001 From: Alfredo Sumaran Date: Wed, 17 Jul 2024 18:15:00 -0500 Subject: [PATCH] [ECE] Add ECE support for WooCommerce Deposits. (#9081) Co-authored-by: Rafael Zaleski --- changelog/as-ece-wc-deposits-support | 4 +++ client/express-checkout/index.js | 30 ++++++++++++++++--- ...payments-express-checkout-ajax-handler.php | 2 +- ...ayments-express-checkout-button-helper.php | 11 +------ psalm-baseline.xml | 5 ++++ 5 files changed, 37 insertions(+), 15 deletions(-) create mode 100644 changelog/as-ece-wc-deposits-support diff --git a/changelog/as-ece-wc-deposits-support b/changelog/as-ece-wc-deposits-support new file mode 100644 index 00000000000..58dfb6ded51 --- /dev/null +++ b/changelog/as-ece-wc-deposits-support @@ -0,0 +1,4 @@ +Significance: minor +Type: add + +Add ECE support for WooCommerce Deposits. diff --git a/client/express-checkout/index.js b/client/express-checkout/index.js index 71d2e75135e..248353af8a8 100644 --- a/client/express-checkout/index.js +++ b/client/express-checkout/index.js @@ -407,6 +407,21 @@ jQuery( ( $ ) => { }, attachProductPageEventListeners: ( elements ) => { + // WooCommerce Deposits support. + // Trigger the "woocommerce_variation_has_changed" event when the deposit option is changed. + // Needs to be defined before the `woocommerce_variation_has_changed` event handler is set. + $( + 'input[name=wc_deposit_option],input[name=wc_deposit_payment_plan]' + ) + .off( 'change' ) + .on( 'change', () => { + $( 'form' ) + .has( + 'input[name=wc_deposit_option],input[name=wc_deposit_payment_plan]' + ) + .trigger( 'woocommerce_variation_has_changed' ); + } ); + $( document.body ) .off( 'woocommerce_variation_has_changed' ) .on( 'woocommerce_variation_has_changed', () => { @@ -414,6 +429,7 @@ jQuery( ( $ ) => { $.when( wcpayECE.getSelectedProductData() ) .then( ( response ) => { + const isDeposits = wcpayECE.productHasDepositOption(); /** * If the customer aborted the express checkout, * we need to re init the express checkout button to ensure the shipping @@ -421,14 +437,14 @@ jQuery( ( $ ) => { * and the product's shipping status is consistent, * we can simply update the express checkout button with the new total and display items. */ - if ( + const needsShipping = ! wcpayECE.paymentAborted && getExpressCheckoutData( 'product' ) - .needs_shipping === response.needs_shipping - ) { + .needs_shipping === response.needs_shipping; + + if ( ! isDeposits && needsShipping ) { elements.update( { amount: response.total.amount, - displayItems: response.displayItems, } ); } else { wcpayECE.reInitExpressCheckoutElement( @@ -534,6 +550,12 @@ jQuery( ( $ ) => { } }, + productHasDepositOption() { + return !! $( 'form' ).has( + 'input[name=wc_deposit_option],input[name=wc_deposit_payment_plan]' + ).length; + }, + /** * Initialize event handlers and UI state */ diff --git a/includes/express-checkout/class-wc-payments-express-checkout-ajax-handler.php b/includes/express-checkout/class-wc-payments-express-checkout-ajax-handler.php index c129b4d394d..24d9fe1d21a 100644 --- a/includes/express-checkout/class-wc-payments-express-checkout-ajax-handler.php +++ b/includes/express-checkout/class-wc-payments-express-checkout-ajax-handler.php @@ -316,7 +316,7 @@ public function ajax_get_selected_product_data() { 'pending' => true, ]; - $data['needs_shipping'] = ( wc_shipping_enabled() && $product->needs_shipping() ); + $data['needs_shipping'] = wc_shipping_enabled() && 0 !== wc_get_shipping_method_count( true ) && $product->needs_shipping(); $data['currency'] = strtolower( get_woocommerce_currency() ); $data['country_code'] = substr( get_option( 'woocommerce_default_country' ), 0, 2 ); diff --git a/includes/express-checkout/class-wc-payments-express-checkout-button-helper.php b/includes/express-checkout/class-wc-payments-express-checkout-button-helper.php index eddb8bf24ef..4fa08fab878 100644 --- a/includes/express-checkout/class-wc-payments-express-checkout-button-helper.php +++ b/includes/express-checkout/class-wc-payments-express-checkout-button-helper.php @@ -807,20 +807,11 @@ public function get_product_price( $product, ?bool $is_deposit = null, int $depo // If WooCommerce Deposits is active, we need to get the correct price for the product. if ( class_exists( 'WC_Deposits_Product_Manager' ) && class_exists( 'WC_Deposits_Plans_Manager' ) && WC_Deposits_Product_Manager::deposits_enabled( $product->get_id() ) ) { + // If is_deposit is null, we use the default deposit type for the product. if ( is_null( $is_deposit ) ) { - /** - * If is_deposit is null, we use the default deposit type for the product. - * - * @psalm-suppress UndefinedClass - */ $is_deposit = 'deposit' === WC_Deposits_Product_Manager::get_deposit_selected_type( $product->get_id() ); } if ( $is_deposit ) { - /** - * Ignore undefined classes from 3rd party plugins. - * - * @psalm-suppress UndefinedClass - */ $deposit_type = WC_Deposits_Product_Manager::get_deposit_type( $product->get_id() ); $available_plan_ids = WC_Deposits_Plans_Manager::get_plan_ids_for_product( $product->get_id() ); // Default to first (default) plan if no plan is specified. diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 751f04cb47f..2a5164e8af3 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -36,6 +36,11 @@ WC_Subscriptions_Cart + + + WC_Deposits_Order_Manager + + WC_Subscriptions_Product