woocommerce/plugins/woocommerce-blocks/assets/js/base/utils/shipping-rates.ts

58 lines
1.5 KiB
TypeScript
Raw Normal View History

Allow third party methods to appear in local pickup area (https://github.com/woocommerce/woocommerce-blocks/pull/8256) * Add get_collectible_method_ids function * Add collectibleMethodIds to asset data registry * Check whether method id is pickup_location/in collectibleMethodIds * Allow selectShippingRate to be called without a package id * Prevent collectible methods showing in the main shipping area * Remove unnecessary pluck and add pickup_location to returned array * No longer insert pickup_location in collectibleMethodIds * Allow third party methods to influence low/high collection price * Update useShippingData to consider any collectible method * Add hasSelectedLocalPickup to shipping types * Add dependency to selectShippingRate in useShippingData * Register collectibleMethodIds as a callback This is so the shipping methods get change to register before this is called. Passing a callback to `add` means it won't be called until just before it is output. * Update supports key to 'local_pickup' * Rename utils/shipping-rates to TS * Convert to TS, add isPackageRateCollectible & hasCollectableRate * Add tests for hasCollectableRate and isPackageRateCollectible * Update shipping controller to output only method names * Make PickupLocation shipping method support local_pickup * Set prefersCollection based on rate ID being collectible * Remove need to retrieve settings and use helper function instead * rename hasCollectableRate to hasCollectibleRate * Use array_reduce and update comments in get_local_pickup_method_ids * Switch order of array_unique and array_values * Remove unneeded dependency * Hyphenate local-pickup so it follows the same format as other features * Update use of collectible to collectable * Change supports feature to be hyphenated
2023-02-03 16:00:24 +00:00
/**
* External dependencies
*/
import {
CartShippingPackageShippingRate,
CartShippingRate,
} from '@woocommerce/type-defs/cart';
import { getSetting } from '@woocommerce/settings';
/**
* Get the number of packages in a shippingRates array.
*
* @param {Array} shippingRates Shipping rates and packages array.
*/
export const getShippingRatesPackageCount = (
shippingRates: CartShippingRate[]
) => {
return shippingRates.length;
};
const collectableMethodIds = getSetting< string[] >(
'collectableMethodIds',
[]
);
/**
* If the package rate's method_id is in the collectableMethodIds array, return true.
*/
export const isPackageRateCollectable = (
rate: CartShippingPackageShippingRate
): boolean => collectableMethodIds.includes( rate.method_id );
/**
* Check if the specified rates are collectable. Accepts either an array of rate names, or a single string.
*/
export const hasCollectableRate = (
chosenRates: string[] | string
): boolean => {
if ( Array.isArray( chosenRates ) ) {
return !! chosenRates.find( ( rate ) =>
collectableMethodIds.includes( rate )
);
}
return collectableMethodIds.includes( chosenRates );
};
/**
* Get the number of rates in a shippingRates array.
*
* @param {Array} shippingRates Shipping rates and packages array.
*/
export const getShippingRatesRateCount = (
shippingRates: CartShippingRate[]
) => {
return shippingRates.reduce( function ( count, shippingPackage ) {
return count + shippingPackage.shipping_rates.length;
}, 0 );
};