Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ECE] Add ECE support for WooCommerce Deposits. #9081

Merged
merged 13 commits into from
Jul 17, 2024
Merged
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 @@ -775,20 +775,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
Loading