Skip to content

Commit

Permalink
Disable ECE for non shipping products if Tax is calculated on billing…
Browse files Browse the repository at this point in the history
… address (#9089)
  • Loading branch information
asumaran authored Jul 13, 2024
1 parent 76d2cd5 commit 6f434a4
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: fix

Disable ECE for non shipping products if Tax is calculated on billing address.
1 change: 1 addition & 0 deletions client/express-checkout/utils/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export const normalizePayForOrderData = ( event, paymentMethodId ) => {
payment_method: 'woocommerce_payments',
'wcpay-payment-method': paymentMethodId,
express_payment_type: event?.expressPaymentType,
'wcpay-fraud-prevention-token': window.wcpayFraudPreventionToken ?? '',
};
};

Expand Down
2 changes: 2 additions & 0 deletions client/express-checkout/utils/test/normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ describe( 'Express checkout normalization', () => {
expect( normalizePayForOrderData( event, 'pm_123456' ) ).toEqual( {
payment_method: 'woocommerce_payments',
'wcpay-payment-method': 'pm_123456',
'wcpay-fraud-prevention-token': 'token123',
express_payment_type: 'express',
} );
} );
Expand All @@ -315,6 +316,7 @@ describe( 'Express checkout normalization', () => {
).toEqual( {
payment_method: 'woocommerce_payments',
'wcpay-payment-method': '',
'wcpay-fraud-prevention-token': 'token123',
express_payment_type: undefined,
} );
} );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,23 @@ public function should_show_express_checkout_button() {
return true;
}

// Non-shipping product and billing is calculated based on shopper billing addres. Excludes Pay for Order page.
if (
// If the product doesn't needs shipping.
(
// on the product page.
( $this->is_product() && ! $this->product_needs_shipping( $this->get_product() ) ) ||

// on the cart or checkout page.
( ( $this->is_cart() || $this->is_checkout() ) && ! WC()->cart->needs_shipping() )
)

// ...and billing is calculated based on billing address.
&& 'billing' === get_option( 'woocommerce_tax_based_on' )
) {
return false;
}

// Cart total is 0 or is on product page and product price is 0.
// Exclude pay-for-order pages from this check.
if (
Expand All @@ -431,6 +448,21 @@ public function should_show_express_checkout_button() {
return true;
}

/**
* Check if the passed product needs to be shipped.
*
* @param WC_Product $product The product to check.
*
* @return bool Returns true if the product requires shipping; otherwise, returns false.
*/
public function product_needs_shipping( WC_Product $product ) {
if ( ! $product ) {
return false;
}

return wc_shipping_enabled() && 0 !== wc_get_shipping_method_count( true ) && $product->needs_shipping();
}

/**
* Checks to make sure product type is supported.
*
Expand Down
13 changes: 11 additions & 2 deletions includes/fraud-prevention/class-fraud-prevention-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ public static function maybe_append_fraud_prevention_token() {
return;
}

// Don't add the token if the user isn't on the cart, checkout or product page.
// Don't add the token if the user isn't on the cart, checkout, product or pay for order page.
// Checking the product and cart page too because the user can pay quickly via the payment buttons on that page.
if ( ! is_checkout() && ! is_cart() && ! is_product() ) {
if ( ! is_checkout() && ! is_cart() && ! is_product() && ! $instance->is_pay_for_order_page() ) {
return;
}

Expand All @@ -103,6 +103,15 @@ public static function maybe_append_fraud_prevention_token() {
);
}

/**
* Checks if this is the Pay for Order page.
*
* @return bool
*/
public function is_pay_for_order_page() {
return is_checkout() && isset( $_GET['pay_for_order'] ); // phpcs:ignore WordPress.Security.NonceVerification
}

/**
* Sets a instance to be used in request cycle.
* Introduced primarily for supporting unit tests.
Expand Down

0 comments on commit 6f434a4

Please sign in to comment.