From b753412ab278bc1752f0e176f7568334feae3edb Mon Sep 17 00:00:00 2001 From: Thomas Roberts Date: Thu, 26 Jan 2023 16:11:02 +0000 Subject: [PATCH] Ensure TotalsShipping shows only one package rate if local pickup chosen --- .../cart-checkout/totals/shipping/index.tsx | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/assets/js/base/components/cart-checkout/totals/shipping/index.tsx b/assets/js/base/components/cart-checkout/totals/shipping/index.tsx index 56cd51cb91e..da04fdb1556 100644 --- a/assets/js/base/components/cart-checkout/totals/shipping/index.tsx +++ b/assets/js/base/components/cart-checkout/totals/shipping/index.tsx @@ -8,6 +8,9 @@ import { useStoreCart } from '@woocommerce/base-context/hooks'; import { TotalsItem } from '@woocommerce/blocks-checkout'; import type { Currency } from '@woocommerce/price-format'; import { ShippingVia } from '@woocommerce/base-components/cart-checkout/totals/shipping/shipping-via'; +import { CHECKOUT_STORE_KEY } from '@woocommerce/block-data'; +import { useSelect } from '@wordpress/data'; +import { getSetting } from '@woocommerce/settings'; /** * Internal dependencies @@ -31,6 +34,10 @@ export interface TotalShippingProps { isCheckout?: boolean; } +const collectibleMethodIds = getSetting< string[] >( + 'collectibleMethodIds', + [] +); export const TotalsShipping = ( { currency, values, @@ -51,10 +58,21 @@ export const TotalsShipping = ( { const hasRates = hasShippingRate( shippingRates ) || totalShippingValue > 0; const showShippingCalculatorForm = showCalculator && isShippingCalculatorOpen; + const prefersCollection = useSelect( ( select ) => { + return select( CHECKOUT_STORE_KEY ).prefersCollection(); + } ); const selectedShippingRates = shippingRates.flatMap( ( shippingPackage ) => { return shippingPackage.shipping_rates - .filter( ( rate ) => rate.selected ) + .filter( + ( rate ) => + // If the shopper prefers collection, the rate is collectible AND selected. + ( prefersCollection && + collectibleMethodIds.includes( rate.method_id ) && + rate.selected ) || + // Or the shopper does not prefer collection and the rate is selected + ( ! prefersCollection && rate.selected ) + ) .flatMap( ( rate ) => rate.name ); } );