2021-01-28 14:24:01 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { decodeEntities } from '@wordpress/html-entities';
|
2021-02-04 15:30:28 +00:00
|
|
|
import { getCurrencyFromPriceResponse } from '@woocommerce/price-format';
|
2023-10-18 14:43:34 +00:00
|
|
|
import { FormattedMonetaryAmount } from '@woocommerce/blocks-components';
|
2022-12-23 11:59:02 +00:00
|
|
|
import type { PackageRateOption } from '@woocommerce/types';
|
2021-04-22 11:37:27 +00:00
|
|
|
import { getSetting } from '@woocommerce/settings';
|
2022-12-23 11:59:02 +00:00
|
|
|
import { CartShippingPackageShippingRate } from '@woocommerce/types';
|
2024-04-17 20:54:43 +00:00
|
|
|
import { __ } from '@wordpress/i18n';
|
2021-01-28 14:24:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Default render function for package rate options.
|
|
|
|
*
|
2021-03-09 10:55:24 +00:00
|
|
|
* @param {Object} rate Rate data.
|
2021-01-28 14:24:01 +00:00
|
|
|
*/
|
2021-05-10 09:03:30 +00:00
|
|
|
export const renderPackageRateOption = (
|
|
|
|
rate: CartShippingPackageShippingRate
|
|
|
|
): PackageRateOption => {
|
2021-04-22 11:37:27 +00:00
|
|
|
const priceWithTaxes: number = getSetting(
|
|
|
|
'displayCartPricesIncludingTax',
|
|
|
|
false
|
|
|
|
)
|
2021-03-09 10:55:24 +00:00
|
|
|
? parseInt( rate.price, 10 ) + parseInt( rate.taxes, 10 )
|
|
|
|
: parseInt( rate.price, 10 );
|
|
|
|
|
2024-04-17 20:54:43 +00:00
|
|
|
let description = (
|
|
|
|
<>
|
|
|
|
{ Number.isFinite( priceWithTaxes ) && (
|
|
|
|
<FormattedMonetaryAmount
|
|
|
|
currency={ getCurrencyFromPriceResponse( rate ) }
|
|
|
|
value={ priceWithTaxes }
|
|
|
|
/>
|
|
|
|
) }
|
|
|
|
{ Number.isFinite( priceWithTaxes ) && rate.delivery_time
|
|
|
|
? ' — '
|
|
|
|
: null }
|
|
|
|
{ decodeEntities( rate.delivery_time ) }
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( priceWithTaxes === 0 ) {
|
|
|
|
description = (
|
|
|
|
<span className="wc-block-components-shipping-rates-control__package__description--free">
|
|
|
|
{ __( 'Free', 'woocommerce' ) }
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-01-28 14:24:01 +00:00
|
|
|
return {
|
2021-03-09 10:55:24 +00:00
|
|
|
label: decodeEntities( rate.name ),
|
|
|
|
value: rate.rate_id,
|
2024-04-17 20:54:43 +00:00
|
|
|
description,
|
2021-01-28 14:24:01 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export default renderPackageRateOption;
|