2019-12-03 14:12:46 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2020-03-23 20:13:41 +00:00
|
|
|
import { Fragment, useState, useCallback, useEffect } from '@wordpress/element';
|
2019-12-06 13:18:55 +00:00
|
|
|
import { __ } from '@wordpress/i18n';
|
2020-03-30 13:04:27 +00:00
|
|
|
import defaultAddressFields from '@woocommerce/base-components/cart-checkout/address-form/default-address-fields';
|
2020-03-06 12:20:17 +00:00
|
|
|
import {
|
2020-03-30 13:04:27 +00:00
|
|
|
AddressForm,
|
2020-03-06 12:20:17 +00:00
|
|
|
FormStep,
|
|
|
|
CheckoutForm,
|
|
|
|
NoShipping,
|
2020-03-16 16:38:24 +00:00
|
|
|
PlaceOrderButton,
|
2020-03-30 13:04:27 +00:00
|
|
|
Policies,
|
2020-03-16 16:38:24 +00:00
|
|
|
ReturnToCartButton,
|
2020-03-30 13:04:27 +00:00
|
|
|
ShippingRatesControl,
|
2020-03-16 16:38:24 +00:00
|
|
|
} from '@woocommerce/base-components/cart-checkout';
|
2020-03-23 11:22:00 +00:00
|
|
|
import { ValidatedTextInput } from '@woocommerce/base-components/text-input';
|
2020-03-06 10:26:03 +00:00
|
|
|
import CheckboxControl from '@woocommerce/base-components/checkbox-control';
|
2020-02-14 03:43:13 +00:00
|
|
|
import { getCurrencyFromPriceResponse } from '@woocommerce/base-utils';
|
|
|
|
import FormattedMonetaryAmount from '@woocommerce/base-components/formatted-monetary-amount';
|
2020-03-17 11:45:33 +00:00
|
|
|
import {
|
|
|
|
CheckoutProvider,
|
2020-03-23 20:13:41 +00:00
|
|
|
useCheckoutContext,
|
2020-04-01 09:27:53 +00:00
|
|
|
useEditorContext,
|
2020-03-26 13:31:09 +00:00
|
|
|
useShippingDataContext,
|
|
|
|
useBillingDataContext,
|
2020-04-02 09:27:54 +00:00
|
|
|
useValidationContext,
|
2020-03-17 11:45:33 +00:00
|
|
|
} from '@woocommerce/base-context';
|
2020-01-06 22:28:09 +00:00
|
|
|
import {
|
|
|
|
ExpressCheckoutFormControl,
|
|
|
|
PaymentMethods,
|
|
|
|
} from '@woocommerce/base-components/payment-methods';
|
2020-02-18 10:50:08 +00:00
|
|
|
import { decodeEntities } from '@wordpress/html-entities';
|
2020-03-12 09:41:35 +00:00
|
|
|
import {
|
|
|
|
Sidebar,
|
|
|
|
SidebarLayout,
|
|
|
|
Main,
|
2020-03-13 15:49:33 +00:00
|
|
|
} from '@woocommerce/base-components/sidebar-layout';
|
2020-03-16 16:38:24 +00:00
|
|
|
import { getSetting } from '@woocommerce/settings';
|
2020-03-23 11:22:00 +00:00
|
|
|
import withScrollToTop from '@woocommerce/base-hocs/with-scroll-to-top';
|
2020-01-06 22:28:09 +00:00
|
|
|
|
2019-12-03 14:12:46 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2020-03-26 13:31:09 +00:00
|
|
|
import CheckoutSidebar from './sidebar/index.js';
|
2020-03-31 10:47:48 +00:00
|
|
|
import CheckoutOrderError from './checkout-order-error/index.js';
|
2019-12-03 14:12:46 +00:00
|
|
|
import './style.scss';
|
|
|
|
|
2020-04-01 09:27:53 +00:00
|
|
|
const Block = ( props ) => (
|
|
|
|
<CheckoutProvider>
|
2020-03-23 20:13:41 +00:00
|
|
|
<Checkout { ...props } />
|
|
|
|
</CheckoutProvider>
|
|
|
|
);
|
|
|
|
|
|
|
|
const Checkout = ( {
|
2020-03-12 09:41:35 +00:00
|
|
|
attributes,
|
|
|
|
cartCoupons = [],
|
2020-03-13 15:49:33 +00:00
|
|
|
cartItems = [],
|
2020-03-12 09:41:35 +00:00
|
|
|
cartTotals = {},
|
2020-03-23 11:22:00 +00:00
|
|
|
scrollToTop,
|
2020-04-02 09:27:54 +00:00
|
|
|
shippingRates = [],
|
2020-03-12 09:41:35 +00:00
|
|
|
} ) => {
|
2020-04-01 09:27:53 +00:00
|
|
|
const { isEditor } = useEditorContext();
|
2020-04-02 09:27:54 +00:00
|
|
|
const { hasOrder, onCheckoutCompleteError } = useCheckoutContext();
|
|
|
|
const { showAllValidationErrors } = useValidationContext();
|
2020-03-26 13:31:09 +00:00
|
|
|
const {
|
|
|
|
shippingRatesLoading,
|
|
|
|
shippingAddress,
|
|
|
|
setShippingAddress,
|
2020-04-02 17:10:36 +00:00
|
|
|
needsShipping,
|
2020-03-26 13:31:09 +00:00
|
|
|
} = useShippingDataContext();
|
|
|
|
const { billingData, setBillingData } = useBillingDataContext();
|
2020-03-23 11:22:00 +00:00
|
|
|
|
2020-03-26 13:31:09 +00:00
|
|
|
const [ contactFields, setContactFields ] = useState( {} );
|
|
|
|
const [ shippingAsBilling, setShippingAsBilling ] = useState( true );
|
|
|
|
|
2020-02-26 15:50:53 +00:00
|
|
|
const renderShippingRatesControlOption = ( option ) => ( {
|
|
|
|
label: decodeEntities( option.name ),
|
|
|
|
value: option.rate_id,
|
|
|
|
description: decodeEntities( option.description ),
|
|
|
|
secondaryLabel: (
|
|
|
|
<FormattedMonetaryAmount
|
|
|
|
currency={ getCurrencyFromPriceResponse( option ) }
|
|
|
|
value={ option.price }
|
|
|
|
/>
|
|
|
|
),
|
|
|
|
secondaryDescription: decodeEntities( option.delivery_time ),
|
|
|
|
} );
|
|
|
|
|
2020-04-02 17:10:36 +00:00
|
|
|
const showBillingFields = ! needsShipping || ! shippingAsBilling;
|
2020-03-05 13:06:47 +00:00
|
|
|
const addressFields = {
|
2020-03-09 14:23:16 +00:00
|
|
|
...defaultAddressFields,
|
2020-03-05 13:06:47 +00:00
|
|
|
company: {
|
2020-03-09 14:23:16 +00:00
|
|
|
...defaultAddressFields.company,
|
2020-03-05 13:06:47 +00:00
|
|
|
hidden: ! attributes.showCompanyField,
|
|
|
|
required: attributes.requireCompanyField,
|
|
|
|
},
|
|
|
|
address_2: {
|
2020-03-09 14:23:16 +00:00
|
|
|
...defaultAddressFields.address_2,
|
2020-03-05 13:06:47 +00:00
|
|
|
hidden: ! attributes.showAddress2Field,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-03-23 20:13:41 +00:00
|
|
|
const setShippingFields = useCallback(
|
|
|
|
( address ) => {
|
|
|
|
if ( shippingAsBilling ) {
|
|
|
|
setShippingAddress( address );
|
2020-03-26 13:31:09 +00:00
|
|
|
setBillingData( address );
|
2020-03-23 20:13:41 +00:00
|
|
|
} else {
|
|
|
|
setShippingAddress( address );
|
|
|
|
}
|
|
|
|
},
|
2020-03-26 13:31:09 +00:00
|
|
|
[ setShippingAddress, setBillingData, shippingAsBilling ]
|
2020-03-23 20:13:41 +00:00
|
|
|
);
|
|
|
|
useEffect( () => {
|
|
|
|
if ( shippingAsBilling ) {
|
2020-04-02 09:27:54 +00:00
|
|
|
setBillingData( { ...shippingAddress, shippingAsBilling } );
|
|
|
|
} else {
|
|
|
|
setBillingData( { shippingAsBilling } );
|
2020-03-23 20:13:41 +00:00
|
|
|
}
|
2020-03-26 13:31:09 +00:00
|
|
|
}, [ shippingAsBilling, setBillingData ] );
|
2020-03-30 14:32:23 +00:00
|
|
|
|
2020-04-02 09:27:54 +00:00
|
|
|
useEffect( () => {
|
|
|
|
const unsubscribeCompleteError = onCheckoutCompleteError( () => {
|
|
|
|
showAllValidationErrors();
|
|
|
|
scrollToTop( { focusableSelector: 'input:invalid' } );
|
|
|
|
} );
|
|
|
|
return () => {
|
|
|
|
unsubscribeCompleteError();
|
|
|
|
};
|
|
|
|
}, [ onCheckoutCompleteError ] );
|
|
|
|
|
2020-03-30 14:32:23 +00:00
|
|
|
if ( ! isEditor && ! hasOrder ) {
|
2020-03-31 10:47:48 +00:00
|
|
|
return <CheckoutOrderError />;
|
2020-03-30 14:32:23 +00:00
|
|
|
}
|
|
|
|
|
2019-12-03 14:12:46 +00:00
|
|
|
return (
|
2020-03-30 12:43:42 +00:00
|
|
|
<>
|
|
|
|
<SidebarLayout className="wc-block-checkout">
|
2020-04-03 13:17:09 +00:00
|
|
|
<Main className="wc-block-checkout__main">
|
2020-03-30 12:43:42 +00:00
|
|
|
<ExpressCheckoutFormControl />
|
|
|
|
<CheckoutForm>
|
2020-03-23 11:22:00 +00:00
|
|
|
<FormStep
|
2020-03-30 12:43:42 +00:00
|
|
|
id="contact-fields"
|
|
|
|
className="wc-block-checkout__contact-fields"
|
2020-03-23 11:22:00 +00:00
|
|
|
title={ __(
|
2020-03-30 12:43:42 +00:00
|
|
|
'Contact information',
|
2020-03-23 11:22:00 +00:00
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
description={ __(
|
2020-03-30 12:43:42 +00:00
|
|
|
"We'll use this email to send you details and updates about your order.",
|
2020-03-23 11:22:00 +00:00
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
2020-03-30 12:43:42 +00:00
|
|
|
stepHeadingContent={ () => (
|
|
|
|
<Fragment>
|
|
|
|
{ __(
|
|
|
|
'Already have an account? ',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
<a href="/wp-login.php">
|
|
|
|
{ __(
|
|
|
|
'Log in.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
</a>
|
|
|
|
</Fragment>
|
|
|
|
) }
|
2020-03-23 11:22:00 +00:00
|
|
|
>
|
2020-03-30 12:43:42 +00:00
|
|
|
<ValidatedTextInput
|
2020-04-02 09:27:54 +00:00
|
|
|
id="email"
|
2020-03-30 12:43:42 +00:00
|
|
|
type="email"
|
|
|
|
label={ __(
|
|
|
|
'Email address',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
value={ billingData.email }
|
|
|
|
autoComplete="email"
|
|
|
|
onChange={ ( newValue ) =>
|
|
|
|
setBillingData( { email: newValue } )
|
|
|
|
}
|
|
|
|
required={ true }
|
2020-03-23 11:22:00 +00:00
|
|
|
/>
|
|
|
|
<CheckboxControl
|
2020-03-30 12:43:42 +00:00
|
|
|
className="wc-block-checkout__keep-updated"
|
2020-03-23 11:22:00 +00:00
|
|
|
label={ __(
|
2020-03-30 12:43:42 +00:00
|
|
|
'Keep me up to date on news and exclusive offers',
|
2020-03-23 11:22:00 +00:00
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
2020-03-30 12:43:42 +00:00
|
|
|
checked={ contactFields.keepUpdated }
|
|
|
|
onChange={ () =>
|
|
|
|
setContactFields( {
|
|
|
|
...contactFields,
|
|
|
|
keepUpdated: ! contactFields.keepUpdated,
|
|
|
|
} )
|
2020-03-23 11:22:00 +00:00
|
|
|
}
|
2020-03-23 20:13:41 +00:00
|
|
|
/>
|
|
|
|
</FormStep>
|
2020-04-02 17:10:36 +00:00
|
|
|
{ needsShipping && (
|
2020-03-30 12:43:42 +00:00
|
|
|
<FormStep
|
|
|
|
id="shipping-fields"
|
|
|
|
className="wc-block-checkout__shipping-fields"
|
|
|
|
title={ __(
|
|
|
|
'Shipping address',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
description={ __(
|
|
|
|
'Enter the physical address where you want us to deliver your order.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
>
|
|
|
|
<AddressForm
|
2020-04-02 09:27:54 +00:00
|
|
|
id="shipping"
|
2020-03-30 12:43:42 +00:00
|
|
|
onChange={ setShippingFields }
|
|
|
|
values={ shippingAddress }
|
|
|
|
fields={ Object.keys( addressFields ) }
|
|
|
|
fieldConfig={ addressFields }
|
|
|
|
/>
|
|
|
|
{ attributes.showPhoneField && (
|
|
|
|
<ValidatedTextInput
|
2020-04-02 09:27:54 +00:00
|
|
|
id="phone"
|
2020-03-30 12:43:42 +00:00
|
|
|
type="tel"
|
|
|
|
label={
|
|
|
|
attributes.requirePhoneField
|
|
|
|
? __(
|
|
|
|
'Phone',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
)
|
|
|
|
: __(
|
|
|
|
'Phone (optional)',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
value={ billingData.phone }
|
|
|
|
autoComplete="tel"
|
|
|
|
onChange={ ( newValue ) =>
|
|
|
|
setBillingData( {
|
|
|
|
phone: newValue,
|
|
|
|
} )
|
|
|
|
}
|
|
|
|
required={
|
|
|
|
attributes.requirePhoneField
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
) }
|
|
|
|
<CheckboxControl
|
|
|
|
className="wc-block-checkout__use-address-for-billing"
|
|
|
|
label={ __(
|
|
|
|
'Use same address for billing',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
checked={ shippingAsBilling }
|
|
|
|
onChange={ ( isChecked ) =>
|
|
|
|
setShippingAsBilling( isChecked )
|
|
|
|
}
|
|
|
|
required={ attributes.requirePhoneField }
|
|
|
|
/>
|
|
|
|
</FormStep>
|
|
|
|
) }
|
|
|
|
{ showBillingFields && (
|
|
|
|
<FormStep
|
|
|
|
id="billing-fields"
|
|
|
|
className="wc-block-checkout__billing-fields"
|
|
|
|
title={ __(
|
|
|
|
'Billing address',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
description={ __(
|
|
|
|
'Enter the address that matches your card or payment method.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
>
|
|
|
|
<AddressForm
|
2020-04-02 09:27:54 +00:00
|
|
|
id="billing"
|
2020-03-30 12:43:42 +00:00
|
|
|
onChange={ setBillingData }
|
|
|
|
type="billing"
|
|
|
|
values={ billingData }
|
|
|
|
fields={ Object.keys( addressFields ) }
|
|
|
|
fieldConfig={ addressFields }
|
|
|
|
/>
|
|
|
|
</FormStep>
|
|
|
|
) }
|
2020-04-02 17:10:36 +00:00
|
|
|
{ needsShipping && (
|
2020-03-30 12:43:42 +00:00
|
|
|
<FormStep
|
|
|
|
id="shipping-option"
|
|
|
|
className="wc-block-checkout__shipping-option"
|
|
|
|
title={ __(
|
|
|
|
'Shipping options',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
description={ __(
|
|
|
|
'Select a shipping method below.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
>
|
|
|
|
{ shippingRates.length === 0 && isEditor ? (
|
|
|
|
<NoShipping />
|
|
|
|
) : (
|
|
|
|
<ShippingRatesControl
|
|
|
|
address={
|
|
|
|
shippingAddress.country
|
|
|
|
? {
|
|
|
|
address_1:
|
|
|
|
shippingAddress.address_1,
|
|
|
|
address_2:
|
|
|
|
shippingAddress.address_2,
|
|
|
|
city:
|
|
|
|
shippingAddress.city,
|
|
|
|
state:
|
|
|
|
shippingAddress.state,
|
|
|
|
postcode:
|
|
|
|
shippingAddress.postcode,
|
|
|
|
country:
|
|
|
|
shippingAddress.country,
|
|
|
|
}
|
|
|
|
: null
|
|
|
|
}
|
|
|
|
noResultsMessage={ __(
|
|
|
|
'There are no shipping options available. Please ensure that your address has been entered correctly, or contact us if you need any help.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
renderOption={
|
|
|
|
renderShippingRatesControlOption
|
|
|
|
}
|
|
|
|
shippingRates={ shippingRates }
|
|
|
|
shippingRatesLoading={
|
|
|
|
shippingRatesLoading
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
) }
|
|
|
|
{ /*@todo This is not implemented*/ }
|
|
|
|
<CheckboxControl
|
|
|
|
className="wc-block-checkout__add-note"
|
|
|
|
label="Add order notes?"
|
|
|
|
checked={ false }
|
|
|
|
onChange={ () => null }
|
|
|
|
/>
|
|
|
|
</FormStep>
|
|
|
|
) }
|
2020-03-23 11:22:00 +00:00
|
|
|
<FormStep
|
2020-03-30 12:43:42 +00:00
|
|
|
id="payment-method"
|
|
|
|
className="wc-block-checkout__payment-method"
|
2020-03-23 11:22:00 +00:00
|
|
|
title={ __(
|
2020-03-30 12:43:42 +00:00
|
|
|
'Payment method',
|
2020-03-23 11:22:00 +00:00
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
description={ __(
|
2020-03-30 12:43:42 +00:00
|
|
|
'Select a payment method below.',
|
2020-03-23 11:22:00 +00:00
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
>
|
2020-03-30 12:43:42 +00:00
|
|
|
<PaymentMethods />
|
2020-03-23 11:22:00 +00:00
|
|
|
</FormStep>
|
2020-03-30 12:43:42 +00:00
|
|
|
</CheckoutForm>
|
|
|
|
</Main>
|
|
|
|
<Sidebar className="wc-block-checkout__sidebar">
|
|
|
|
<CheckoutSidebar
|
|
|
|
cartCoupons={ cartCoupons }
|
|
|
|
cartItems={ cartItems }
|
|
|
|
cartTotals={ cartTotals }
|
|
|
|
/>
|
|
|
|
</Sidebar>
|
2020-04-03 13:17:09 +00:00
|
|
|
<Main className="wc-block-checkout__main-totals">
|
2020-03-23 20:13:41 +00:00
|
|
|
<div className="wc-block-checkout__actions">
|
|
|
|
{ attributes.showReturnToCart && (
|
|
|
|
<ReturnToCartButton
|
|
|
|
link={ getSetting(
|
|
|
|
'page-' + attributes?.cartPageId,
|
|
|
|
false
|
|
|
|
) }
|
2020-03-23 11:22:00 +00:00
|
|
|
/>
|
2020-03-23 20:13:41 +00:00
|
|
|
) }
|
2020-04-02 09:27:54 +00:00
|
|
|
<PlaceOrderButton />
|
2020-03-23 20:13:41 +00:00
|
|
|
</div>
|
|
|
|
{ attributes.showPolicyLinks && <Policies /> }
|
2020-03-30 12:43:42 +00:00
|
|
|
</Main>
|
|
|
|
</SidebarLayout>
|
|
|
|
</>
|
2019-12-03 14:12:46 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-03-23 11:22:00 +00:00
|
|
|
export default withScrollToTop( Block );
|