Skip to content

Commit cbb5fd6

Browse files
annemirasoldiegocurbelo
authored andcommitted
Fix ECE error for virtual products and when no shipping zones are set up (#3595)
* Fix ECE bug when there are no shipping zones * Add unit tests * Only set shippingRates if cart is shippable
1 parent f17383c commit cbb5fd6

File tree

3 files changed

+54
-14
lines changed

3 files changed

+54
-14
lines changed

client/blocks/express-checkout/hooks.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ export const useExpressCheckout = ( {
6969

7070
// Return a default shipping option, as a non-empty shippingRates array
7171
// is required when shippingAddressRequired is true.
72-
return [
73-
getExpressCheckoutData( 'checkout' )
74-
?.default_shipping_option,
75-
];
72+
const defaultShippingOption = getExpressCheckoutData(
73+
'checkout'
74+
)?.default_shipping_option;
75+
return defaultShippingOption ? [ defaultShippingOption ] : [];
7676
};
7777

7878
const options = {
@@ -82,7 +82,9 @@ export const useExpressCheckout = ( {
8282
phoneNumberRequired:
8383
getExpressCheckoutData( 'checkout' )?.needs_payer_phone ??
8484
false,
85-
shippingRates: getShippingRates(),
85+
...( shippingData?.needsShipping && {
86+
shippingRates: getShippingRates(),
87+
} ),
8688
};
8789

8890
// Click event from WC Blocks.

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,14 @@ public function get_checkout_data() {
296296

297297
/**
298298
* Default shipping option, used by product, cart and checkout pages.
299+
*
300+
* @return void|array
299301
*/
300302
private function get_default_shipping_option() {
303+
if ( wc_get_shipping_method_count( true, true ) === 0 ) {
304+
return null;
305+
}
306+
301307
return [
302308
'id' => 'pending',
303309
'displayName' => __( 'Pending', 'woocommerce-gateway-stripe' ),

tests/phpunit/test-wc-stripe-express-checkout-helper.php

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
* WC_Stripe_Express_Checkout_Helper_Test class.
1111
*/
1212
class WC_Stripe_Express_Checkout_Helper_Test extends WP_UnitTestCase {
13+
private $shipping_zone;
14+
private $shipping_method;
15+
1316
public function set_up() {
1417
parent::set_up();
1518

@@ -19,24 +22,37 @@ public function set_up() {
1922
$stripe_settings['test_publishable_key'] = 'pk_test_key';
2023
$stripe_settings['test_secret_key'] = 'sk_test_key';
2124
WC_Stripe_Helper::update_main_stripe_settings( $stripe_settings );
25+
}
26+
27+
public function tear_down() {
28+
if ( $this->shipping_zone ) {
29+
delete_option( $this->shipping_method->get_instance_option_key() );
30+
$this->shipping_zone->delete();
31+
}
2232

33+
parent::tear_down();
34+
}
35+
36+
public function set_up_shipping_methods() {
2337
// Add a shipping zone.
24-
$zone = new WC_Shipping_Zone();
25-
$zone->set_zone_name( 'Worldwide' );
26-
$zone->set_zone_order( 1 );
27-
$zone->save();
28-
29-
$flat_rate_id = $zone->add_shipping_method( 'flat_rate' );
30-
$method = WC_Shipping_Zones::get_shipping_method( $flat_rate_id );
31-
$option_key = $method->get_instance_option_key();
32-
$options['cost'] = '5';
38+
$this->shipping_zone = new WC_Shipping_Zone();
39+
$this->shipping_zone->set_zone_name( 'Worldwide' );
40+
$this->shipping_zone->set_zone_order( 1 );
41+
$this->shipping_zone->save();
42+
43+
$flat_rate_id = $this->shipping_zone->add_shipping_method( 'flat_rate' );
44+
$this->shipping_method = WC_Shipping_Zones::get_shipping_method( $flat_rate_id );
45+
$option_key = $this->shipping_method->get_instance_option_key();
46+
$options['cost'] = '5';
3347
update_option( $option_key, $options );
3448
}
3549

3650
/**
3751
* Test should_show_express_checkout_button, tax logic.
3852
*/
3953
public function test_hides_ece_if_cannot_compute_taxes() {
54+
$this->set_up_shipping_methods();
55+
4056
$wc_stripe_ece_helper_mock = $this->createPartialMock(
4157
WC_Stripe_Express_Checkout_Helper::class,
4258
[
@@ -98,6 +114,8 @@ public function test_hides_ece_if_cannot_compute_taxes() {
98114
* Test should_show_express_checkout_button, gateway logic.
99115
*/
100116
public function test_hides_ece_if_stripe_gateway_unavailable() {
117+
$this->set_up_shipping_methods();
118+
101119
$wc_stripe_ece_helper_mock = $this->createPartialMock(
102120
WC_Stripe_Express_Checkout_Helper::class,
103121
[
@@ -142,6 +160,8 @@ public function test_get_checkout_data() {
142160
update_option( 'woocommerce_currency', 'USD' );
143161
WC()->cart->empty_cart();
144162

163+
$this->set_up_shipping_methods();
164+
145165
$wc_stripe_ece_helper = new WC_Stripe_Express_Checkout_Helper();
146166
$checkout_data = $wc_stripe_ece_helper->get_checkout_data();
147167

@@ -154,4 +174,16 @@ public function test_get_checkout_data() {
154174
$this->assertArrayHasKey( 'displayName', $checkout_data['default_shipping_option'] );
155175
$this->assertArrayHasKey( 'amount', $checkout_data['default_shipping_option'] );
156176
}
177+
178+
/**
179+
* Test for get_checkout_data(), no shipping zones.
180+
*
181+
* This is in a separate test, to avoid problems with cached data.
182+
*/
183+
public function test_get_checkout_data_no_shipping_zones() {
184+
// When no shipping zones are set up, the default shipping option should be empty.
185+
$wc_stripe_ece_helper = new WC_Stripe_Express_Checkout_Helper();
186+
$checkout_data = $wc_stripe_ece_helper->get_checkout_data();
187+
$this->assertEmpty( $checkout_data['default_shipping_option'] );
188+
}
157189
}

0 commit comments

Comments
 (0)