2021-07-22 11:03:00 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
2023-04-22 09:10:11 +00:00
|
|
|
import {
|
|
|
|
useCustomerData,
|
|
|
|
useShippingData,
|
|
|
|
} from '@woocommerce/base-context/hooks';
|
2021-07-22 11:03:00 +00:00
|
|
|
import { ShippingRatesControl } from '@woocommerce/base-components/cart-checkout';
|
2023-02-03 16:00:24 +00:00
|
|
|
import {
|
|
|
|
getShippingRatesPackageCount,
|
|
|
|
hasCollectableRate,
|
2023-03-22 07:15:13 +00:00
|
|
|
isAddressComplete,
|
2023-02-03 16:00:24 +00:00
|
|
|
} from '@woocommerce/base-utils';
|
2021-07-22 11:03:00 +00:00
|
|
|
import { getCurrencyFromPriceResponse } from '@woocommerce/price-format';
|
|
|
|
import FormattedMonetaryAmount from '@woocommerce/base-components/formatted-monetary-amount';
|
2022-12-19 15:30:13 +00:00
|
|
|
import { useEditorContext, noticeContexts } from '@woocommerce/base-context';
|
|
|
|
import { StoreNoticesContainer } from '@woocommerce/blocks-checkout';
|
2021-07-22 11:03:00 +00:00
|
|
|
import { decodeEntities } from '@wordpress/html-entities';
|
|
|
|
import { getSetting } from '@woocommerce/settings';
|
2022-12-23 11:59:02 +00:00
|
|
|
import type {
|
|
|
|
PackageRateOption,
|
|
|
|
CartShippingPackageShippingRate,
|
|
|
|
} from '@woocommerce/types';
|
2023-04-05 12:43:03 +00:00
|
|
|
import NoticeBanner from '@woocommerce/base-components/notice-banner';
|
2023-04-22 09:10:11 +00:00
|
|
|
import type { ReactElement } from 'react';
|
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 ),
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2023-04-22 09:10:11 +00:00
|
|
|
const Block = ( { noShippingPlaceholder = null } ): ReactElement | null => {
|
2021-07-22 11:03:00 +00:00
|
|
|
const { isEditor } = useEditorContext();
|
2022-03-04 17:43:45 +00:00
|
|
|
|
2021-07-22 11:03:00 +00:00
|
|
|
const {
|
|
|
|
shippingRates,
|
|
|
|
needsShipping,
|
2022-03-14 11:29:25 +00:00
|
|
|
isLoadingRates,
|
2021-07-22 11:03:00 +00:00
|
|
|
hasCalculatedShipping,
|
2022-10-18 16:31:06 +00:00
|
|
|
isCollectable,
|
2022-03-04 17:43:45 +00:00
|
|
|
} = useShippingData();
|
2021-07-22 11:03:00 +00:00
|
|
|
|
2023-04-22 09:10:11 +00:00
|
|
|
const { shippingAddress } = useCustomerData();
|
2023-03-13 11:49:28 +00:00
|
|
|
|
2022-10-18 16:31:06 +00:00
|
|
|
const filteredShippingRates = isCollectable
|
|
|
|
? shippingRates.map( ( shippingRatesPackage ) => {
|
|
|
|
return {
|
|
|
|
...shippingRatesPackage,
|
|
|
|
shipping_rates: shippingRatesPackage.shipping_rates.filter(
|
|
|
|
( shippingRatesPackageRate ) =>
|
2023-02-03 16:00:24 +00:00
|
|
|
! hasCollectableRate(
|
|
|
|
shippingRatesPackageRate.method_id
|
|
|
|
)
|
2022-10-18 16:31:06 +00:00
|
|
|
),
|
|
|
|
};
|
|
|
|
} )
|
|
|
|
: shippingRates;
|
|
|
|
|
2021-07-22 11:03:00 +00:00
|
|
|
if ( ! needsShipping ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-06-15 09:56:52 +00:00
|
|
|
const shippingRatesPackageCount =
|
|
|
|
getShippingRatesPackageCount( shippingRates );
|
2021-09-17 11:07:01 +00:00
|
|
|
|
2023-04-22 09:10:11 +00:00
|
|
|
if ( ! hasCalculatedShipping && ! shippingRatesPackageCount ) {
|
2021-09-17 11:07:01 +00:00
|
|
|
return (
|
|
|
|
<p>
|
|
|
|
{ __(
|
|
|
|
'Shipping options will be displayed here after entering your full shipping address.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
</p>
|
|
|
|
);
|
|
|
|
}
|
2023-04-22 09:10:11 +00:00
|
|
|
const addressComplete = isAddressComplete( shippingAddress );
|
2021-09-17 11:07:01 +00:00
|
|
|
|
2021-07-22 11:03:00 +00:00
|
|
|
return (
|
|
|
|
<>
|
2022-12-19 15:30:13 +00:00
|
|
|
<StoreNoticesContainer
|
|
|
|
context={ noticeContexts.SHIPPING_METHODS }
|
|
|
|
/>
|
2021-09-17 11:07:01 +00:00
|
|
|
{ isEditor && ! shippingRatesPackageCount ? (
|
2023-02-27 10:13:00 +00:00
|
|
|
noShippingPlaceholder
|
2021-07-22 11:03:00 +00:00
|
|
|
) : (
|
|
|
|
<ShippingRatesControl
|
|
|
|
noResultsMessage={
|
2023-03-22 07:15:13 +00:00
|
|
|
<>
|
2023-04-05 12:43:03 +00:00
|
|
|
{ addressComplete ? (
|
|
|
|
<NoticeBanner
|
|
|
|
isDismissible={ false }
|
|
|
|
className="wc-block-components-shipping-rates-control__no-results-notice"
|
2023-04-06 09:42:04 +00:00
|
|
|
status="warning"
|
2023-04-05 12:43:03 +00:00
|
|
|
>
|
|
|
|
{ __(
|
2023-03-22 07:15:13 +00:00
|
|
|
'There are no shipping options available. Please check your shipping address.',
|
|
|
|
'woo-gutenberg-products-block'
|
2023-04-05 12:43:03 +00:00
|
|
|
) }
|
|
|
|
</NoticeBanner>
|
|
|
|
) : (
|
|
|
|
__(
|
|
|
|
'Add a shipping address to view shipping options.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
)
|
|
|
|
) }
|
2023-03-22 07:15:13 +00:00
|
|
|
</>
|
2021-07-22 11:03:00 +00:00
|
|
|
}
|
|
|
|
renderOption={ renderShippingRatesControlOption }
|
2022-12-15 14:17:22 +00:00
|
|
|
collapsible={ false }
|
2022-12-15 15:47:20 +00:00
|
|
|
shippingRates={ filteredShippingRates }
|
2022-03-14 11:29:25 +00:00
|
|
|
isLoadingRates={ isLoadingRates }
|
2022-02-22 09:33:52 +00:00
|
|
|
context="woocommerce/checkout"
|
2021-07-22 11:03:00 +00:00
|
|
|
/>
|
|
|
|
) }
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Block;
|