Skip to content

Commit bd33346

Browse files
authored
Disable ECE for incompatible tax scenarios (#3493)
* Disable ECE for incompatible tax scenarios * Add changelog and readme entries * Add unit test
1 parent 72a81a4 commit bd33346

File tree

4 files changed

+98
-0
lines changed

4 files changed

+98
-0
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
*** Changelog ***
22

33
= 8.8.0 - xxxx-xx-xx =
4+
* Tweak - Disable ECE when cart has virtual products and tax is based on customer billing or shipping address.
45
* Fix - Fix the usage of coupons and the total shipping amount when using the Express Checkout Element on the shortcode checkout.
56
* Fix - Fixes some JS console errors when making a purchase with the Stripe Express Checkout Element on the shortcode checkout.
67
* Fix - Updates the display logic for the OAuth re-connect promotional surface to follow the latest changes made to the connection settings object.

includes/payment-methods/class-wc-stripe-express-checkout-helper.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,9 +551,35 @@ public function should_show_express_checkout_button() {
551551
}
552552
}
553553

554+
// Hide if cart/product doesn't require shipping and tax is based on billing or shipping address.
555+
if (
556+
(
557+
( is_product() && ! $this->product_needs_shipping( $this->get_product() ) ) ||
558+
( ( is_cart() || is_checkout() ) && ! WC()->cart->needs_shipping() )
559+
) &&
560+
in_array( get_option( 'woocommerce_tax_based_on' ), [ 'billing', 'shipping' ], true )
561+
) {
562+
return false;
563+
}
564+
554565
return true;
555566
}
556567

568+
/**
569+
* Check if the passed product needs to be shipped.
570+
*
571+
* @param WC_Product $product The product to check.
572+
*
573+
* @return bool Returns true if the product requires shipping; otherwise, returns false.
574+
*/
575+
public function product_needs_shipping( WC_Product $product ) {
576+
if ( ! $product ) {
577+
return false;
578+
}
579+
580+
return wc_shipping_enabled() && 0 !== wc_get_shipping_method_count( true ) && $product->needs_shipping();
581+
}
582+
557583
/**
558584
* Returns true if express checkout buttons are enabled on the cart page, false
559585
* otherwise.

readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ If you get stuck, you can ask for help in the Plugin Forum.
129129
== Changelog ==
130130

131131
= 8.8.0 - xxxx-xx-xx =
132+
* Tweak - Disable ECE when cart has virtual products and tax is based on customer billing or shipping address.
132133
* Fix - Fix the usage of coupons and the total shipping amount when using the Express Checkout Element on the shortcode checkout.
133134
* Fix - Fixes some JS console errors when making a purchase with the Stripe Express Checkout Element on the shortcode checkout.
134135
* Fix - Updates the display logic for the OAuth re-connect promotional surface to follow the latest changes made to the connection settings object.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
/**
4+
* These tests make assertions against class WC_Stripe_Express_Checkout_Helper.
5+
*
6+
* @package WooCommerce_Stripe/Tests/WC_Stripe_Express_Checkout_Helper
7+
*/
8+
9+
/**
10+
* WC_Stripe_Express_Checkout_Helper class.
11+
*/
12+
class WC_Stripe_Express_Checkout_Helper_Test extends WP_UnitTestCase {
13+
public function set_up() {
14+
parent::set_up();
15+
16+
$stripe_settings = WC_Stripe_Helper::get_stripe_settings();
17+
$stripe_settings['enabled'] = 'yes';
18+
$stripe_settings['testmode'] = 'yes';
19+
$stripe_settings['test_publishable_key'] = 'pk_test_key';
20+
$stripe_settings['test_secret_key'] = 'sk_test_key';
21+
WC_Stripe_Helper::update_main_stripe_settings( $stripe_settings );
22+
}
23+
24+
/**
25+
* Test should_show_express_checkout_button, tax logic.
26+
*/
27+
public function test_hides_ece_if_cannot_compute_taxes() {
28+
$wc_stripe_ece_helper_mock = $this->createPartialMock(
29+
WC_Stripe_Express_Checkout_Helper::class,
30+
[
31+
'is_product',
32+
'allowed_items_in_cart',
33+
'should_show_ece_on_cart_page',
34+
'should_show_ece_on_checkout_page',
35+
]
36+
);
37+
$wc_stripe_ece_helper_mock->expects( $this->any() )->method( 'is_product' )->willReturn( false );
38+
$wc_stripe_ece_helper_mock->expects( $this->any() )->method( 'allowed_items_in_cart' )->willReturn( true );
39+
$wc_stripe_ece_helper_mock->expects( $this->any() )->method( 'should_show_ece_on_cart_page' )->willReturn( true );
40+
$wc_stripe_ece_helper_mock->expects( $this->any() )->method( 'should_show_ece_on_checkout_page' )->willReturn( true );
41+
$wc_stripe_ece_helper_mock->testmode = true;
42+
if ( ! defined( 'WOOCOMMERCE_CHECKOUT' ) ) {
43+
define( 'WOOCOMMERCE_CHECKOUT', true );
44+
}
45+
46+
// Create virtual product and add to cart.
47+
$virtual_product = WC_Helper_Product::create_simple_product();
48+
$virtual_product->set_virtual( true );
49+
$virtual_product->save();
50+
51+
WC()->session->init();
52+
WC()->cart->add_to_cart( $virtual_product->get_id(), 1 );
53+
54+
// Hide if cart has virtual product and tax is based on shipping or billing address.
55+
update_option( 'woocommerce_tax_based_on', 'billing' );
56+
$this->assertFalse( $wc_stripe_ece_helper_mock->should_show_express_checkout_button() );
57+
58+
update_option( 'woocommerce_tax_based_on', 'shipping' );
59+
$this->assertFalse( $wc_stripe_ece_helper_mock->should_show_express_checkout_button() );
60+
61+
// Do not hide if taxes are not based on customer billing or shipping address.
62+
update_option( 'woocommerce_tax_based_on', 'base' );
63+
$this->assertTrue( $wc_stripe_ece_helper_mock->should_show_express_checkout_button() );
64+
65+
// Do not hide if cart requires shipping.
66+
$shippable_product = WC_Helper_Product::create_simple_product();
67+
WC()->cart->add_to_cart( $shippable_product->get_id(), 1 );
68+
$this->assertTrue( $wc_stripe_ece_helper_mock->should_show_express_checkout_button() );
69+
}
70+
}

0 commit comments

Comments
 (0)