2021-07-22 11:03:00 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import { ShippingRatesControl } from '@woocommerce/base-components/cart-checkout';
|
|
|
|
import { getShippingRatesPackageCount } from '@woocommerce/base-utils';
|
|
|
|
import { getCurrencyFromPriceResponse } from '@woocommerce/price-format';
|
|
|
|
import FormattedMonetaryAmount from '@woocommerce/base-components/formatted-monetary-amount';
|
|
|
|
import {
|
|
|
|
useEditorContext,
|
|
|
|
useShippingDataContext,
|
|
|
|
} from '@woocommerce/base-context';
|
|
|
|
import { decodeEntities } from '@wordpress/html-entities';
|
|
|
|
import { Notice } from 'wordpress-components';
|
|
|
|
import classnames from 'classnames';
|
|
|
|
import { getSetting } from '@woocommerce/settings';
|
|
|
|
import type { PackageRateOption } from '@woocommerce/type-defs/shipping';
|
|
|
|
import type { CartShippingPackageShippingRate } from '@woocommerce/type-defs/cart';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import NoShippingPlaceholder from './no-shipping-placeholder';
|
2021-09-16 12:16:21 +00:00
|
|
|
import './style.scss';
|
2021-07-22 11:03:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders a shipping rate control option.
|
|
|
|
*
|
|
|
|
* @param {Object} option Shipping Rate.
|
|
|
|
*/
|
|
|
|
const renderShippingRatesControlOption = (
|
|
|
|
option: CartShippingPackageShippingRate
|
|
|
|
): PackageRateOption => {
|
|
|
|
const priceWithTaxes = getSetting( 'displayCartPricesIncludingTax', false )
|
|
|
|
? parseInt( option.price, 10 ) + parseInt( option.taxes, 10 )
|
|
|
|
: parseInt( option.price, 10 );
|
|
|
|
return {
|
|
|
|
label: decodeEntities( option.name ),
|
|
|
|
value: option.rate_id,
|
|
|
|
description: decodeEntities( option.description ),
|
|
|
|
secondaryLabel: (
|
|
|
|
<FormattedMonetaryAmount
|
|
|
|
currency={ getCurrencyFromPriceResponse( option ) }
|
|
|
|
value={ priceWithTaxes }
|
|
|
|
/>
|
|
|
|
),
|
|
|
|
secondaryDescription: decodeEntities( option.delivery_time ),
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const Block = (): JSX.Element | null => {
|
|
|
|
const { isEditor } = useEditorContext();
|
|
|
|
const {
|
|
|
|
shippingRates,
|
|
|
|
shippingRatesLoading,
|
|
|
|
needsShipping,
|
|
|
|
hasCalculatedShipping,
|
|
|
|
} = useShippingDataContext();
|
|
|
|
|
|
|
|
if ( ! needsShipping ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-09-17 11:07:01 +00:00
|
|
|
const shippingRatesPackageCount = getShippingRatesPackageCount(
|
|
|
|
shippingRates
|
|
|
|
);
|
|
|
|
|
|
|
|
if (
|
|
|
|
! isEditor &&
|
|
|
|
! hasCalculatedShipping &&
|
|
|
|
! shippingRatesPackageCount
|
|
|
|
) {
|
|
|
|
return (
|
|
|
|
<p>
|
|
|
|
{ __(
|
|
|
|
'Shipping options will be displayed here after entering your full shipping address.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
</p>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-22 11:03:00 +00:00
|
|
|
return (
|
|
|
|
<>
|
2021-09-17 11:07:01 +00:00
|
|
|
{ isEditor && ! shippingRatesPackageCount ? (
|
2021-07-22 11:03:00 +00:00
|
|
|
<NoShippingPlaceholder />
|
|
|
|
) : (
|
|
|
|
<ShippingRatesControl
|
|
|
|
noResultsMessage={
|
2021-09-17 11:07:01 +00:00
|
|
|
<Notice
|
|
|
|
isDismissible={ false }
|
|
|
|
className={ classnames(
|
|
|
|
'wc-block-components-shipping-rates-control__no-results-notice',
|
|
|
|
'woocommerce-error'
|
|
|
|
) }
|
|
|
|
>
|
|
|
|
{ __(
|
|
|
|
'There are no shipping options available. Please check your shipping address.',
|
2021-07-22 11:03:00 +00:00
|
|
|
'woo-gutenberg-products-block'
|
2021-09-17 11:07:01 +00:00
|
|
|
) }
|
|
|
|
</Notice>
|
2021-07-22 11:03:00 +00:00
|
|
|
}
|
|
|
|
renderOption={ renderShippingRatesControlOption }
|
|
|
|
shippingRates={ shippingRates }
|
|
|
|
shippingRatesLoading={ shippingRatesLoading }
|
2022-02-22 09:33:52 +00:00
|
|
|
context="woocommerce/checkout"
|
2021-07-22 11:03:00 +00:00
|
|
|
/>
|
|
|
|
) }
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Block;
|