Skip to content

Commit

Permalink
Fix ECE error for virtual products and when no shipping zones are set…
Browse files Browse the repository at this point in the history
… up (#3595)

* Fix ECE bug when there are no shipping zones

* Add unit tests

* Only set shippingRates if cart is shippable
  • Loading branch information
annemirasol authored and diegocurbelo committed Nov 14, 2024
1 parent f17383c commit cbb5fd6
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 14 deletions.
12 changes: 7 additions & 5 deletions client/blocks/express-checkout/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ export const useExpressCheckout = ( {

// Return a default shipping option, as a non-empty shippingRates array
// is required when shippingAddressRequired is true.
return [
getExpressCheckoutData( 'checkout' )
?.default_shipping_option,
];
const defaultShippingOption = getExpressCheckoutData(
'checkout'
)?.default_shipping_option;
return defaultShippingOption ? [ defaultShippingOption ] : [];
};

const options = {
Expand All @@ -82,7 +82,9 @@ export const useExpressCheckout = ( {
phoneNumberRequired:
getExpressCheckoutData( 'checkout' )?.needs_payer_phone ??
false,
shippingRates: getShippingRates(),
...( shippingData?.needsShipping && {
shippingRates: getShippingRates(),
} ),
};

// Click event from WC Blocks.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,14 @@ public function get_checkout_data() {

/**
* Default shipping option, used by product, cart and checkout pages.
*
* @return void|array
*/
private function get_default_shipping_option() {
if ( wc_get_shipping_method_count( true, true ) === 0 ) {
return null;
}

return [
'id' => 'pending',
'displayName' => __( 'Pending', 'woocommerce-gateway-stripe' ),
Expand Down
50 changes: 41 additions & 9 deletions tests/phpunit/test-wc-stripe-express-checkout-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
* WC_Stripe_Express_Checkout_Helper_Test class.
*/
class WC_Stripe_Express_Checkout_Helper_Test extends WP_UnitTestCase {
private $shipping_zone;
private $shipping_method;

public function set_up() {
parent::set_up();

Expand All @@ -19,24 +22,37 @@ public function set_up() {
$stripe_settings['test_publishable_key'] = 'pk_test_key';
$stripe_settings['test_secret_key'] = 'sk_test_key';
WC_Stripe_Helper::update_main_stripe_settings( $stripe_settings );
}

public function tear_down() {
if ( $this->shipping_zone ) {
delete_option( $this->shipping_method->get_instance_option_key() );
$this->shipping_zone->delete();
}

parent::tear_down();
}

public function set_up_shipping_methods() {
// Add a shipping zone.
$zone = new WC_Shipping_Zone();
$zone->set_zone_name( 'Worldwide' );
$zone->set_zone_order( 1 );
$zone->save();

$flat_rate_id = $zone->add_shipping_method( 'flat_rate' );
$method = WC_Shipping_Zones::get_shipping_method( $flat_rate_id );
$option_key = $method->get_instance_option_key();
$options['cost'] = '5';
$this->shipping_zone = new WC_Shipping_Zone();
$this->shipping_zone->set_zone_name( 'Worldwide' );
$this->shipping_zone->set_zone_order( 1 );
$this->shipping_zone->save();

$flat_rate_id = $this->shipping_zone->add_shipping_method( 'flat_rate' );
$this->shipping_method = WC_Shipping_Zones::get_shipping_method( $flat_rate_id );
$option_key = $this->shipping_method->get_instance_option_key();
$options['cost'] = '5';
update_option( $option_key, $options );
}

/**
* Test should_show_express_checkout_button, tax logic.
*/
public function test_hides_ece_if_cannot_compute_taxes() {
$this->set_up_shipping_methods();

$wc_stripe_ece_helper_mock = $this->createPartialMock(
WC_Stripe_Express_Checkout_Helper::class,
[
Expand Down Expand Up @@ -98,6 +114,8 @@ public function test_hides_ece_if_cannot_compute_taxes() {
* Test should_show_express_checkout_button, gateway logic.
*/
public function test_hides_ece_if_stripe_gateway_unavailable() {
$this->set_up_shipping_methods();

$wc_stripe_ece_helper_mock = $this->createPartialMock(
WC_Stripe_Express_Checkout_Helper::class,
[
Expand Down Expand Up @@ -142,6 +160,8 @@ public function test_get_checkout_data() {
update_option( 'woocommerce_currency', 'USD' );
WC()->cart->empty_cart();

$this->set_up_shipping_methods();

$wc_stripe_ece_helper = new WC_Stripe_Express_Checkout_Helper();
$checkout_data = $wc_stripe_ece_helper->get_checkout_data();

Expand All @@ -154,4 +174,16 @@ public function test_get_checkout_data() {
$this->assertArrayHasKey( 'displayName', $checkout_data['default_shipping_option'] );
$this->assertArrayHasKey( 'amount', $checkout_data['default_shipping_option'] );
}

/**
* Test for get_checkout_data(), no shipping zones.
*
* This is in a separate test, to avoid problems with cached data.
*/
public function test_get_checkout_data_no_shipping_zones() {
// When no shipping zones are set up, the default shipping option should be empty.
$wc_stripe_ece_helper = new WC_Stripe_Express_Checkout_Helper();
$checkout_data = $wc_stripe_ece_helper->get_checkout_data();
$this->assertEmpty( $checkout_data['default_shipping_option'] );
}
}

0 comments on commit cbb5fd6

Please sign in to comment.