Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Checkout Block: Prevent changes in the selected shipping method when new rates are added or removed #10457

Merged
merged 3 commits into from
Sep 19, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/Shipping/ShippingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,36 @@ function() {
add_filter( 'wc_shipping_enabled', array( $this, 'force_shipping_enabled' ), 100, 1 );
add_filter( 'woocommerce_order_shipping_to_display', array( $this, 'show_local_pickup_details' ), 10, 2 );

add_filter( 'woocommerce_shipping_chosen_method', array( $this, 'prevent_shipping_method_selection_changes' ), 20, 3 );
tarunvijwani marked this conversation as resolved.
Show resolved Hide resolved

// This is required to short circuit `show_shipping` from class-wc-cart.php - without it, that function
// returns based on the option's value in the DB and we can't override it any other way.
add_filter( 'option_woocommerce_shipping_cost_requires_address', array( $this, 'override_cost_requires_address_option' ) );
}

/**
* Prevent changes in the selected shipping method when new rates are added or removed.
*
* If the chosen method exists within package rates, it is returned to maintain the selection.
* Otherwise, the default rate is returned.
*
* @param string $default Default shipping method.
* @param array $package_rates Associative array of available package rates.
* @param string $chosen_method Previously chosen shipping method.
*
* @return string Chosen shipping method or default.
*/
public function prevent_shipping_method_selection_changes( $default, $package_rates, $chosen_method ) {

// If the chosen method exists in the package rates, return it.
if ( $chosen_method && isset( $package_rates[ $chosen_method ] ) ) {
return $chosen_method;
}

// Otherwise, return the default method.
return $default;
}

/**
* Overrides the option to force shipping calculations NOT to wait until an address is entered, but only if the
* Checkout page contains the Checkout Block.
Expand Down