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

Commit 5c0bb97

Browse files
committed
Move no rates logic to package
1 parent 87cff17 commit 5c0bb97

File tree

2 files changed

+13
-20
lines changed

2 files changed

+13
-20
lines changed

assets/js/base/components/cart-checkout/shipping-rates-control/index.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,7 @@ const ShippingRatesControl = ( {
4444
}
4545
const packageCount = getShippingRatesPackageCount( shippingRates );
4646
const shippingOptions = getShippingRatesRateCount( shippingRates );
47-
if ( shippingOptions === 0 ) {
48-
speak(
49-
__(
50-
'No shipping options were found.',
51-
'woo-gutenberg-products-block'
52-
)
53-
);
54-
} else if ( packageCount === 1 ) {
47+
if ( packageCount === 1 ) {
5548
speak(
5649
sprintf(
5750
// translators: %d number of shipping options found.
@@ -121,13 +114,13 @@ const ShippingRatesControl = ( {
121114
*
122115
* @param {Object} props Incoming props.
123116
* @param {Array} props.packages Array of packages.
124-
* @param {React.ReactElement} props.noResultsMessage Rendered when there are no packages.
117+
* @param {React.ReactElement} props.noResultsMessage Rendered when there are no rates in a package.
125118
* @param {boolean} props.collapsible If the package should be rendered as a
126119
* collapsible panel.
127120
* @param {boolean} props.collapse If the panel should be collapsed by default,
128121
* only works if collapsible is true.
129122
* @param {boolean} props.showItems If we should items below the package name.
130-
* @return {React.ReactElement|Array} Rendered components.
123+
* @return {React.ReactElement|Array|null} Rendered components.
131124
*/
132125
const Packages = ( {
133126
packages,
@@ -136,8 +129,9 @@ const Packages = ( {
136129
collapsible,
137130
noResultsMessage,
138131
} ) => {
132+
// If there are no packages, return nothing.
139133
if ( ! packages.length ) {
140-
return noResultsMessage;
134+
return null;
141135
}
142136

143137
return packages.map( ( { package_id: packageId, ...packageData } ) => (
@@ -148,6 +142,7 @@ const Packages = ( {
148142
collapsible={ collapsible }
149143
collapse={ collapse }
150144
showItems={ showItems }
145+
noResultsMessage={ noResultsMessage }
151146
/>
152147
) );
153148
};

src/StoreApi/Routes/CartSelectShippingRate.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,15 @@ protected function get_route_post_response( \WP_REST_Request $request ) {
6464

6565
$controller = new CartController();
6666
$cart = $controller->get_cart_instance();
67+
$package_id = wc_clean( wp_unslash( $request['package_id'] ) );
68+
$rate_id = wc_clean( wp_unslash( $request['rate_id'] ) );
6769

68-
if ( $cart->needs_shipping() ) {
69-
$package_id = wc_clean( wp_unslash( $request['package_id'] ) );
70-
$rate_id = wc_clean( wp_unslash( $request['rate_id'] ) );
71-
72-
try {
73-
$controller->select_shipping_rate( $package_id, $rate_id );
74-
} catch ( \WC_Rest_Exception $e ) {
75-
throw new RouteException( $e->getErrorCode(), $e->getMessage(), $e->getCode() );
76-
}
70+
try {
71+
$controller->select_shipping_rate( $package_id, $rate_id );
72+
} catch ( \WC_Rest_Exception $e ) {
73+
throw new RouteException( $e->getErrorCode(), $e->getMessage(), $e->getCode() );
7774
}
75+
7876
$cart->calculate_shipping();
7977
$cart->calculate_totals();
8078

0 commit comments

Comments
 (0)