Skip to content

Commit

Permalink
Fixes "Invalid recurring shipping method" errors when purchasing mult…
Browse files Browse the repository at this point in the history
…iple subscriptions with Apple / Google Pay (#8618)
  • Loading branch information
mattallan authored Apr 14, 2024
1 parent ff970e3 commit 2d2aeb0
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
4 changes: 4 additions & 0 deletions changelog/fix-8029-prb-invalid-recurring-shipping-method
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: fix

Resolves "Invalid recurring shipping method" errors when purchasing multiple subscriptions with Apple Pay and Google Pay.
40 changes: 39 additions & 1 deletion includes/class-wc-payments-payment-request-button-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -893,7 +893,7 @@ public function get_shipping_options( $shipping_address, $itemized_display_items
$data = [];

// Remember current shipping method before resetting.
$chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods' );
$chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods', [] );
$this->calculate_shipping( apply_filters( 'wcpay_payment_request_shipping_posted_values', $shipping_address ) );

$packages = WC()->shipping->get_packages();
Expand Down Expand Up @@ -943,6 +943,8 @@ public function get_shipping_options( $shipping_address, $itemized_display_items

WC()->cart->calculate_totals();

$this->maybe_restore_recurring_chosen_shipping_methods( $chosen_shipping_methods );

$data += $this->express_checkout_helper->build_display_items( $itemized_display_items );
$data['result'] = 'success';
} catch ( Exception $e ) {
Expand Down Expand Up @@ -1507,4 +1509,40 @@ private function get_taxes_like_cart( $product, $price ) {
// Normally there should be a single tax, but `calc_tax` returns an array, let's use it.
return WC_Tax::calc_tax( $price, $rates, false );
}

/**
* Restores the shipping methods previously chosen for each recurring cart after shipping was reset and recalculated
* during the Payment Request get_shipping_options flow.
*
* When the cart contains multiple subscriptions with different billing periods, customers are able to select different shipping
* methods for each subscription, however, this is not supported when purchasing with Apple Pay and Google Pay as it's
* only concerned about handling the initial purchase.
*
* In order to avoid Woo Subscriptions's `WC_Subscriptions_Cart::validate_recurring_shipping_methods` throwing an error, we need to restore
* the previously chosen shipping methods for each recurring cart.
*
* This function needs to be called after `WC()->cart->calculate_totals()` is run, otherwise `WC()->cart->recurring_carts` won't exist yet.
*
* @param array $previous_chosen_methods The previously chosen shipping methods.
*/
private function maybe_restore_recurring_chosen_shipping_methods( $previous_chosen_methods = [] ) {
if ( empty( WC()->cart->recurring_carts ) || ! method_exists( 'WC_Subscriptions_Cart', 'get_recurring_shipping_package_key' ) ) {
return;
}

$chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods', [] );

foreach ( WC()->cart->recurring_carts as $recurring_cart_key => $recurring_cart ) {
foreach ( $recurring_cart->get_shipping_packages() as $recurring_cart_package_index => $recurring_cart_package ) {
$package_key = WC_Subscriptions_Cart::get_recurring_shipping_package_key( $recurring_cart_key, $recurring_cart_package_index );

// If the recurring cart package key is found in the previous chosen methods, but not in the current chosen methods, restore it.
if ( isset( $previous_chosen_methods[ $package_key ] ) && ! isset( $chosen_shipping_methods[ $package_key ] ) ) {
$chosen_shipping_methods[ $package_key ] = $previous_chosen_methods[ $package_key ];
}
}
}

WC()->session->set( 'chosen_shipping_methods', $chosen_shipping_methods );
}
}
4 changes: 1 addition & 3 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@
</UndefinedClass>
</file>
<file src="includes/class-wc-payments-payment-request-button-handler.php">
<UndefinedClass occurrences="4">
<UndefinedClass occurrences="5">
<code>WC_Pre_Orders_Product</code>
<code>WC_Subscriptions_Product</code>
<code>WC_Subscriptions_Product</code>
<code>WC_Subscriptions_Cart</code>
<code>WC_Subscriptions_Cart</code>
</UndefinedClass>
</file>
Expand Down

0 comments on commit 2d2aeb0

Please sign in to comment.