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

Commit

Permalink
Ensure TotalsShipping shows only one package rate if local pickup chosen
Browse files Browse the repository at this point in the history
  • Loading branch information
opr committed Jan 26, 2023
1 parent c922333 commit c2f903e
Showing 1 changed file with 19 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -31,6 +34,10 @@ export interface TotalShippingProps {
isCheckout?: boolean;
}

const collectibleMethodIds = getSetting< string[] >(
'collectibleMethodIds',
[]
);
export const TotalsShipping = ( {
currency,
values,
Expand All @@ -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 );
}
);
Expand Down

0 comments on commit c2f903e

Please sign in to comment.