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.
14 changes: 14 additions & 0 deletions client/express-checkout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,20 @@ jQuery( ( $ ) => {
} );
}, 250 )
);

// WooCommerce Deposits support.
// Trigger the "woocommerce_variation_has_changed" event when the deposit option is changed.
$(
'input[name=wc_deposit_option],input[name=wc_deposit_payment_plan]'
)
.off( 'change' )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the purpose of this off? Do we need to remove all other event handlers from the change event? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was being extra cautious because the ECE element can be reinitialized, event handlers are attached each time it happens. To avoid attaching multiple handlers to the same selector, I remove any existing event handlers first. Although the elements are not being removed and added again, it seems unnecessary, but I prefer to keep it this way for safety. Does that sound good to you?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I appreciate the caution, but if we're going to do that I think we should pass the specific handler we're removing to the function so we're not just removing every single event handler that may have been attached.

See the off() docs for details.

.on( 'change', () => {
$( 'form' )
.has(
'input[name=wc_deposit_option],input[name=wc_deposit_payment_plan]'
)
.trigger( 'woocommerce_variation_has_changed' );
} );
},

reInitExpressCheckoutElement: ( response ) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,13 @@ public function should_show_express_checkout_button() {

// Order total doesn't matter for Pay for Order page. Thus, this page should always display payment buttons.
if ( $this->is_pay_for_order_page() ) {

// ECE doesn't work on the Pay For Order Page.
$order = wc_get_order( absint( get_query_var( 'order-pay' ) ) );
asumaran marked this conversation as resolved.
Show resolved Hide resolved
if ( $order && class_exists( 'WC_Deposits_Order_Manager' ) && WC_Deposits_Order_Manager::is_follow_up_order( $order ) ) {
return false;
}
asumaran marked this conversation as resolved.
Show resolved Hide resolved

return true;
}

Expand Down Expand Up @@ -755,20 +762,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