Skip to content

Commit

Permalink
Loop through each package to check if the chosen rate for it is valid (
Browse files Browse the repository at this point in the history
…woocommerce#47716)

* Loop through each package to check if the chosen rate for it is valid

* Add changelog
  • Loading branch information
opr authored May 22, 2024
1 parent 5e6bce3 commit e6bd07d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
4 changes: 4 additions & 0 deletions plugins/woocommerce/changelog/fix-check-valid-shipping-rates
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fix

Check each pacakge's chosen shipping rate against each valid rate for that package
18 changes: 12 additions & 6 deletions plugins/woocommerce/src/StoreApi/Utilities/OrderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -556,14 +556,20 @@ public function validate_selected_shipping_methods( $needs_shipping, $chosen_shi
throw $exception;
}

$valid_methods = array_keys( WC()->shipping()->get_shipping_methods() );
$packages = WC()->shipping()->get_packages();
foreach ( $packages as $package_id => $package ) {
$chosen_rate_for_package = wc_get_chosen_shipping_method_for_package( $package_id, $package );
$valid_rate_ids_for_package = array_map(
function ( $rate ) {
return $rate->id;
},
$package['rates']
);

foreach ( $chosen_shipping_methods as $chosen_shipping_method ) {
if (
false === $chosen_shipping_method ||
! is_string( $chosen_shipping_method ) ||
! ArrayUtils::string_contains_array( $chosen_shipping_method, $valid_methods )
) {
false === $chosen_rate_for_package ||
! is_string( $chosen_rate_for_package ) ||
! ArrayUtils::string_contains_array( $chosen_rate_for_package, $valid_rate_ids_for_package ) ) {
throw $exception;
}
}
Expand Down

0 comments on commit e6bd07d

Please sign in to comment.