Skip to content

Commit

Permalink
[ECE] Add ECE support for WooCommerce Deposits. (#9081)
Browse files Browse the repository at this point in the history
Co-authored-by: Rafael Zaleski <[email protected]>
  • Loading branch information
2 people authored and lovo-h committed Aug 1, 2024
1 parent be27cc9 commit bde2f0e
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 15 deletions.
4 changes: 4 additions & 0 deletions changelog/as-ece-wc-deposits-support
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: add

Add ECE support for WooCommerce Deposits.
30 changes: 26 additions & 4 deletions client/express-checkout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,28 +407,44 @@ 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', () => {
wcpayECE.blockExpressCheckoutButton();

$.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
* options are refetched. If the customer didn't abort the express checkout,
* 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(
Expand Down Expand Up @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
<code>WC_Subscriptions_Cart</code>
</UndefinedClass>
</file>
<file src="includes/express-checkout/class-wc-payments-express-checkout-button-helper.php">
<UndefinedClass occurrences="1">
<code>WC_Deposits_Order_Manager</code>
</UndefinedClass>
</file>
<file src="includes/class-wc-payments.php">
<UndefinedClass occurrences="1">
<code>WC_Subscriptions_Product</code>
Expand Down

0 comments on commit bde2f0e

Please sign in to comment.