2020-03-06 12:27:54 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
2020-03-13 13:41:59 +00:00
|
|
|
import { DISPLAY_CART_PRICES_INCLUDING_TAX } from '@woocommerce/block-settings';
|
2020-03-06 12:27:54 +00:00
|
|
|
import ShippingCalculator from '@woocommerce/base-components/shipping-calculator';
|
|
|
|
import ShippingLocation from '@woocommerce/base-components/shipping-location';
|
|
|
|
import PropTypes from 'prop-types';
|
2020-03-13 13:41:59 +00:00
|
|
|
import { useState } from '@wordpress/element';
|
|
|
|
import { useShippingRates } from '@woocommerce/base-hooks';
|
2020-03-06 12:27:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import TotalsItem from '../totals-item';
|
2020-03-13 13:41:59 +00:00
|
|
|
import ShippingRateSelector from './shipping-rate-selector';
|
|
|
|
import hasShippingRate from './has-shipping-rate';
|
2020-03-16 13:13:04 +00:00
|
|
|
import './style.scss';
|
2020-03-06 12:27:54 +00:00
|
|
|
|
2020-03-13 13:41:59 +00:00
|
|
|
/**
|
|
|
|
* Renders the shipping totals row, rates, and calculator if enabled.
|
|
|
|
*/
|
2020-03-06 12:27:54 +00:00
|
|
|
const TotalsShippingItem = ( {
|
|
|
|
currency,
|
|
|
|
values,
|
2020-03-16 13:13:04 +00:00
|
|
|
isCheckout = false,
|
2020-03-13 13:41:59 +00:00
|
|
|
showCalculator = true,
|
|
|
|
showRatesWithoutAddress = false,
|
2020-03-06 12:27:54 +00:00
|
|
|
} ) => {
|
2020-03-13 13:41:59 +00:00
|
|
|
const [ isShippingCalculatorOpen, setIsShippingCalculatorOpen ] = useState(
|
|
|
|
false
|
|
|
|
);
|
|
|
|
const defaultAddressFields = [ 'country', 'state', 'city', 'postcode' ];
|
2020-03-06 12:27:54 +00:00
|
|
|
const {
|
2020-03-13 13:41:59 +00:00
|
|
|
shippingRates,
|
|
|
|
shippingAddress,
|
|
|
|
shippingRatesLoading,
|
|
|
|
hasShippingAddress,
|
|
|
|
setShippingAddress,
|
|
|
|
} = useShippingRates( defaultAddressFields );
|
|
|
|
const totalShippingValue = DISPLAY_CART_PRICES_INCLUDING_TAX
|
|
|
|
? parseInt( values.total_shipping, 10 ) +
|
|
|
|
parseInt( values.total_shipping_tax, 10 )
|
|
|
|
: parseInt( values.total_shipping, 10 );
|
|
|
|
const hasRates = hasShippingRate( shippingRates ) || totalShippingValue;
|
|
|
|
const showingRates = showRatesWithoutAddress || hasShippingAddress;
|
|
|
|
|
|
|
|
// If we have no rates, and an address is needed.
|
2020-03-16 13:13:04 +00:00
|
|
|
if ( ! hasRates && ! hasShippingAddress && ! isCheckout ) {
|
2020-03-13 13:41:59 +00:00
|
|
|
return (
|
|
|
|
<TotalsItem
|
|
|
|
label={ __( 'Shipping', 'woo-gutenberg-products-block' ) }
|
|
|
|
value={
|
|
|
|
showCalculator ? (
|
|
|
|
<button
|
2020-03-16 13:13:04 +00:00
|
|
|
className="wc-block-totals__change-address-button"
|
2020-03-13 13:41:59 +00:00
|
|
|
onClick={ () => {
|
|
|
|
setIsShippingCalculatorOpen(
|
|
|
|
! isShippingCalculatorOpen
|
|
|
|
);
|
|
|
|
} }
|
|
|
|
>
|
|
|
|
{ __(
|
|
|
|
'Calculate',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
</button>
|
|
|
|
) : (
|
|
|
|
<em>
|
|
|
|
{ __(
|
|
|
|
'Calculated during checkout',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
</em>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
description={
|
|
|
|
<>
|
|
|
|
{ showCalculator && isShippingCalculatorOpen && (
|
|
|
|
<ShippingCalculator
|
|
|
|
onUpdate={ ( newAddress ) => {
|
|
|
|
setShippingAddress( newAddress );
|
|
|
|
setIsShippingCalculatorOpen( false );
|
|
|
|
} }
|
|
|
|
address={ shippingAddress }
|
|
|
|
addressFields={ defaultAddressFields }
|
|
|
|
/>
|
|
|
|
) }
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
2020-03-06 12:27:54 +00:00
|
|
|
|
|
|
|
return (
|
2020-03-13 13:41:59 +00:00
|
|
|
<>
|
|
|
|
<TotalsItem
|
|
|
|
label={ __( 'Shipping', 'woo-gutenberg-products-block' ) }
|
|
|
|
value={ totalShippingValue ? totalShippingValue : '' }
|
|
|
|
description={
|
|
|
|
<>
|
|
|
|
<ShippingLocation address={ shippingAddress } />{ ' ' }
|
|
|
|
{ showCalculator && (
|
|
|
|
<button
|
2020-03-16 13:13:04 +00:00
|
|
|
className="wc-block-totals__change-address-button"
|
2020-03-13 13:41:59 +00:00
|
|
|
onClick={ () => {
|
|
|
|
setIsShippingCalculatorOpen(
|
|
|
|
! isShippingCalculatorOpen
|
|
|
|
);
|
|
|
|
} }
|
|
|
|
>
|
|
|
|
{ __(
|
|
|
|
'(change address)',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
</button>
|
|
|
|
) }
|
|
|
|
{ showCalculator && isShippingCalculatorOpen && (
|
|
|
|
<ShippingCalculator
|
|
|
|
onUpdate={ ( newAddress ) => {
|
|
|
|
setShippingAddress( newAddress );
|
|
|
|
setIsShippingCalculatorOpen( false );
|
|
|
|
} }
|
|
|
|
address={ shippingAddress }
|
|
|
|
addressFields={ defaultAddressFields }
|
|
|
|
/>
|
|
|
|
) }
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
currency={ currency }
|
|
|
|
/>
|
2020-03-16 13:13:04 +00:00
|
|
|
{ ! isCheckout && showingRates && (
|
|
|
|
<ShippingRateSelector
|
|
|
|
shippingRates={ shippingRates }
|
|
|
|
shippingRatesLoading={ shippingRatesLoading }
|
|
|
|
/>
|
2020-03-13 13:41:59 +00:00
|
|
|
) }
|
|
|
|
</>
|
2020-03-06 12:27:54 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
TotalsShippingItem.propTypes = {
|
|
|
|
currency: PropTypes.object.isRequired,
|
|
|
|
values: PropTypes.shape( {
|
|
|
|
total_shipping: PropTypes.string,
|
|
|
|
total_shipping_tax: PropTypes.string,
|
|
|
|
} ).isRequired,
|
2020-03-16 13:13:04 +00:00
|
|
|
isCheckout: PropTypes.bool,
|
2020-03-13 13:41:59 +00:00
|
|
|
showCalculator: PropTypes.bool,
|
|
|
|
showRatesWithoutAddress: PropTypes.bool,
|
2020-03-06 12:27:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default TotalsShippingItem;
|