From e6bd07d46535c0df21b4be501e2f72eef780ed70 Mon Sep 17 00:00:00 2001 From: Thomas Roberts <5656702+opr@users.noreply.github.com> Date: Wed, 22 May 2024 20:55:31 +0100 Subject: [PATCH] Loop through each package to check if the chosen rate for it is valid (#47716) * Loop through each package to check if the chosen rate for it is valid * Add changelog --- .../changelog/fix-check-valid-shipping-rates | 4 ++++ .../src/StoreApi/Utilities/OrderController.php | 18 ++++++++++++------ 2 files changed, 16 insertions(+), 6 deletions(-) create mode 100644 plugins/woocommerce/changelog/fix-check-valid-shipping-rates diff --git a/plugins/woocommerce/changelog/fix-check-valid-shipping-rates b/plugins/woocommerce/changelog/fix-check-valid-shipping-rates new file mode 100644 index 00000000000..85551258293 --- /dev/null +++ b/plugins/woocommerce/changelog/fix-check-valid-shipping-rates @@ -0,0 +1,4 @@ +Significance: patch +Type: fix + +Check each pacakge's chosen shipping rate against each valid rate for that package diff --git a/plugins/woocommerce/src/StoreApi/Utilities/OrderController.php b/plugins/woocommerce/src/StoreApi/Utilities/OrderController.php index a5f84b8bf5f..5663362b013 100644 --- a/plugins/woocommerce/src/StoreApi/Utilities/OrderController.php +++ b/plugins/woocommerce/src/StoreApi/Utilities/OrderController.php @@ -556,14 +556,20 @@ class OrderController { 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; } }