diff --git a/plugins/woocommerce/changelog/46719-fix-less-restricive-shipping-validation b/plugins/woocommerce/changelog/46719-fix-less-restricive-shipping-validation new file mode 100644 index 00000000000..e96b5a8f643 --- /dev/null +++ b/plugins/woocommerce/changelog/46719-fix-less-restricive-shipping-validation @@ -0,0 +1,4 @@ +Significance: patch +Type: fix + +Be less aggressive when checking for selected shipping rates in Store API. Reminder that shipping rate id should be on the shape of `method_id:instance_id`. \ No newline at end of file diff --git a/plugins/woocommerce/src/StoreApi/Utilities/ArrayUtils.php b/plugins/woocommerce/src/StoreApi/Utilities/ArrayUtils.php index 7624cea3400..bea5b8a6aa2 100644 --- a/plugins/woocommerce/src/StoreApi/Utilities/ArrayUtils.php +++ b/plugins/woocommerce/src/StoreApi/Utilities/ArrayUtils.php @@ -16,7 +16,7 @@ class ArrayUtils { public static function natural_language_join( $array, $enclose_items_with_quotes = false ) { if ( true === $enclose_items_with_quotes ) { $array = array_map( - function( $item ) { + function ( $item ) { return '"' . $item . '"'; }, $array @@ -33,4 +33,21 @@ class ArrayUtils { } return $last; } + + /** + * Check if a string contains any of the items in an array. + * + * @param string $needle The string to check. + * @param array $haystack The array of items to check for. + * + * @return bool true if the string contains any of the items in the array, false otherwise. + */ + public static function string_contains_array( $needle, $haystack ) { + foreach ( $haystack as $item ) { + if ( false !== strpos( $needle, $item ) ) { + return true; + } + } + return false; + } } diff --git a/plugins/woocommerce/src/StoreApi/Utilities/OrderController.php b/plugins/woocommerce/src/StoreApi/Utilities/OrderController.php index 32ac2d12453..a5f84b8bf5f 100644 --- a/plugins/woocommerce/src/StoreApi/Utilities/OrderController.php +++ b/plugins/woocommerce/src/StoreApi/Utilities/OrderController.php @@ -562,7 +562,7 @@ class OrderController { if ( false === $chosen_shipping_method || ! is_string( $chosen_shipping_method ) || - ! in_array( current( explode( ':', $chosen_shipping_method ) ), $valid_methods, true ) + ! ArrayUtils::string_contains_array( $chosen_shipping_method, $valid_methods ) ) { throw $exception; }