2021-07-22 11:03:00 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
2022-03-04 17:43:45 +00:00
|
|
|
import { 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-03-13 11:49:28 +00:00
|
|
|
import { CART_STORE_KEY } from '@woocommerce/block-data';
|
2023-03-22 07:15:13 +00:00
|
|
|
import { useSelect } from '@wordpress/data';
|
|
|
|
import type { ReactElement } from 'react';
|
2021-07-22 11:03:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2021-09-16 12:16:21 +00:00
|
|
|
import './style.scss';
|
2023-03-13 11:49:28 +00:00
|
|
|
import { shippingAddressHasValidationErrors } from '../../../../data/cart/utils';
|
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-03-13 11:49:28 +00:00
|
|
|
const Block = ( {
|
|
|
|
noShippingPlaceholder = null,
|
|
|
|
shippingCostRequiresAddress = false,
|
|
|
|
} ): 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-03-13 11:49:28 +00:00
|
|
|
const shippingAddressPushed = useSelect( ( select ) => {
|
|
|
|
return select( CART_STORE_KEY ).getFullShippingAddressPushed();
|
|
|
|
} );
|
|
|
|
|
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;
|
|
|
|
|
2023-03-22 07:15:13 +00:00
|
|
|
const shippingAddress = useSelect( ( select ) => {
|
|
|
|
return select( CART_STORE_KEY ).getCustomerData()?.shippingAddress;
|
|
|
|
} );
|
|
|
|
|
2021-07-22 11:03:00 +00:00
|
|
|
if ( ! needsShipping ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-03-22 07:15:13 +00:00
|
|
|
const shippingAddressHasErrors = ! shippingAddressHasValidationErrors();
|
|
|
|
const addressComplete = isAddressComplete( shippingAddress );
|
2023-03-13 11:49:28 +00:00
|
|
|
|
2022-06-15 09:56:52 +00:00
|
|
|
const shippingRatesPackageCount =
|
|
|
|
getShippingRatesPackageCount( shippingRates );
|
2021-09-17 11:07:01 +00:00
|
|
|
|
|
|
|
if (
|
2023-03-13 11:49:28 +00:00
|
|
|
( ! hasCalculatedShipping && ! shippingRatesPackageCount ) ||
|
|
|
|
( shippingCostRequiresAddress &&
|
2023-03-22 07:15:13 +00:00
|
|
|
( ! shippingAddressPushed || ! shippingAddressHasErrors ) )
|
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>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
<>
|
|
|
|
{ addressComplete
|
|
|
|
? __(
|
|
|
|
'There are no shipping options available. Please check your shipping address.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
)
|
|
|
|
: __(
|
|
|
|
'Add a shipping address to view shipping options.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
</>
|
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;
|