2019-12-03 14:12:46 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2019-12-16 22:13:41 +00:00
|
|
|
import { Fragment, useState } from '@wordpress/element';
|
2019-12-06 13:18:55 +00:00
|
|
|
import { __ } from '@wordpress/i18n';
|
2020-03-09 14:23:16 +00:00
|
|
|
import AddressForm from '@woocommerce/base-components/address-form';
|
|
|
|
import defaultAddressFields from '@woocommerce/base-components/address-form/default-address-fields';
|
2020-03-06 12:20:17 +00:00
|
|
|
import {
|
|
|
|
FormStep,
|
|
|
|
CheckoutForm,
|
|
|
|
NoShipping,
|
|
|
|
Policies,
|
|
|
|
} from '@woocommerce/base-components/checkout';
|
2020-03-16 16:38:24 +00:00
|
|
|
import {
|
|
|
|
PlaceOrderButton,
|
|
|
|
ReturnToCartButton,
|
|
|
|
} from '@woocommerce/base-components/cart-checkout';
|
2019-12-16 22:13:41 +00:00
|
|
|
import TextInput from '@woocommerce/base-components/text-input';
|
2020-03-05 19:54:05 +00:00
|
|
|
import ShippingRatesControl from '@woocommerce/base-components/shipping-rates-control';
|
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,
|
|
|
|
ValidationContextProvider,
|
|
|
|
} from '@woocommerce/base-context';
|
2020-01-06 22:28:09 +00:00
|
|
|
import {
|
|
|
|
ExpressCheckoutFormControl,
|
|
|
|
PaymentMethods,
|
|
|
|
} from '@woocommerce/base-components/payment-methods';
|
2020-02-21 16:40:25 +00:00
|
|
|
import { SHIPPING_ENABLED } from '@woocommerce/block-settings';
|
2020-02-18 10:50:08 +00:00
|
|
|
import { decodeEntities } from '@wordpress/html-entities';
|
2020-03-05 19:54:05 +00:00
|
|
|
import { useShippingRates } from '@woocommerce/base-hooks';
|
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-01-06 22:28:09 +00:00
|
|
|
|
2019-12-03 14:12:46 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2020-03-12 09:41:35 +00:00
|
|
|
import CheckoutSidebar from './sidebar.js';
|
2019-12-03 14:12:46 +00:00
|
|
|
import './style.scss';
|
2020-01-06 22:28:09 +00:00
|
|
|
import '../../../payment-methods-demo';
|
2019-12-03 14:12:46 +00:00
|
|
|
|
2020-03-12 09:41:35 +00:00
|
|
|
const Block = ( {
|
|
|
|
attributes,
|
|
|
|
cartCoupons = [],
|
2020-03-13 15:49:33 +00:00
|
|
|
cartItems = [],
|
2020-03-12 09:41:35 +00:00
|
|
|
cartTotals = {},
|
|
|
|
isEditor = false,
|
|
|
|
shippingRates = [],
|
|
|
|
} ) => {
|
2020-02-26 15:50:53 +00:00
|
|
|
const [ selectedShippingRate, setSelectedShippingRate ] = useState( {} );
|
2019-12-16 22:13:41 +00:00
|
|
|
const [ contactFields, setContactFields ] = useState( {} );
|
|
|
|
const [ shouldSavePayment, setShouldSavePayment ] = useState( true );
|
2020-03-04 15:13:38 +00:00
|
|
|
const [ billingFields, setBillingFields ] = useState( {} );
|
|
|
|
const [ useShippingAsBilling, setUseShippingAsBilling ] = useState(
|
|
|
|
attributes.useShippingAsBilling
|
|
|
|
);
|
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-03-04 15:13:38 +00:00
|
|
|
const useShippingAddressAsBilling = isEditor
|
|
|
|
? attributes.useShippingAsBilling
|
|
|
|
: useShippingAsBilling;
|
|
|
|
const showBillingFields =
|
|
|
|
! SHIPPING_ENABLED || ! useShippingAddressAsBilling;
|
|
|
|
|
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-10 10:55:19 +00:00
|
|
|
const {
|
|
|
|
shippingRatesLoading,
|
|
|
|
shippingAddress: shippingFields,
|
|
|
|
setShippingAddress: setShippingFields,
|
2020-03-13 19:04:03 +00:00
|
|
|
} = useShippingRates();
|
2020-03-10 10:55:19 +00:00
|
|
|
|
2019-12-03 14:12:46 +00:00
|
|
|
return (
|
2020-03-17 11:45:33 +00:00
|
|
|
<ValidationContextProvider>
|
|
|
|
<CheckoutProvider isEditor={ isEditor }>
|
|
|
|
<SidebarLayout className="wc-block-checkout">
|
|
|
|
<Main>
|
|
|
|
<ExpressCheckoutFormControl />
|
|
|
|
<CheckoutForm>
|
2020-03-12 09:41:35 +00:00
|
|
|
<FormStep
|
2020-03-17 11:45:33 +00:00
|
|
|
id="contact-fields"
|
|
|
|
className="wc-block-checkout__contact-fields"
|
2020-03-12 09:41:35 +00:00
|
|
|
title={ __(
|
2020-03-17 11:45:33 +00:00
|
|
|
'Contact information',
|
2020-03-12 09:41:35 +00:00
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
description={ __(
|
2020-03-17 11:45:33 +00:00
|
|
|
"We'll use this email to send you details and updates about your order.",
|
2020-03-12 09:41:35 +00:00
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
2020-03-17 11:45:33 +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-12 09:41:35 +00:00
|
|
|
>
|
2020-03-17 11:45:33 +00:00
|
|
|
<TextInput
|
|
|
|
type="email"
|
|
|
|
label={ __(
|
|
|
|
'Email address',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
value={ contactFields.email }
|
|
|
|
autoComplete="email"
|
|
|
|
onChange={ ( newValue ) =>
|
|
|
|
setContactFields( {
|
|
|
|
...contactFields,
|
|
|
|
email: newValue,
|
|
|
|
} )
|
|
|
|
}
|
|
|
|
required={ true }
|
2020-03-12 09:41:35 +00:00
|
|
|
/>
|
|
|
|
<CheckboxControl
|
2020-03-17 11:45:33 +00:00
|
|
|
className="wc-block-checkout__keep-updated"
|
2020-03-12 09:41:35 +00:00
|
|
|
label={ __(
|
2020-03-17 11:45:33 +00:00
|
|
|
'Keep me up to date on news and exclusive offers',
|
2020-03-12 09:41:35 +00:00
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
2020-03-17 11:45:33 +00:00
|
|
|
checked={ contactFields.keepUpdated }
|
|
|
|
onChange={ () =>
|
|
|
|
setContactFields( {
|
|
|
|
...contactFields,
|
|
|
|
keepUpdated: ! contactFields.keepUpdated,
|
|
|
|
} )
|
2020-03-12 09:41:35 +00:00
|
|
|
}
|
|
|
|
/>
|
|
|
|
</FormStep>
|
2020-03-17 11:45:33 +00:00
|
|
|
{ SHIPPING_ENABLED && (
|
2020-03-12 09:41:35 +00:00
|
|
|
<FormStep
|
2020-03-17 11:45:33 +00:00
|
|
|
id="shipping-fields"
|
|
|
|
className="wc-block-checkout__shipping-fields"
|
2020-03-12 09:41:35 +00:00
|
|
|
title={ __(
|
2020-03-17 11:45:33 +00:00
|
|
|
'Shipping address',
|
2020-03-12 09:41:35 +00:00
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
description={ __(
|
2020-03-17 11:45:33 +00:00
|
|
|
'Enter the physical address where you want us to deliver your order.',
|
2020-03-12 09:41:35 +00:00
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
>
|
2020-03-17 11:45:33 +00:00
|
|
|
<AddressForm
|
|
|
|
onChange={ setShippingFields }
|
|
|
|
values={ shippingFields }
|
|
|
|
fields={ Object.keys( addressFields ) }
|
|
|
|
fieldConfig={ addressFields }
|
2020-03-12 09:41:35 +00:00
|
|
|
/>
|
2020-03-17 11:45:33 +00:00
|
|
|
{ attributes.showPhoneField && (
|
|
|
|
<TextInput
|
|
|
|
type="tel"
|
|
|
|
label={
|
|
|
|
attributes.requirePhoneField
|
|
|
|
? __(
|
|
|
|
'Phone',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
)
|
|
|
|
: __(
|
|
|
|
'Phone (optional)',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
value={ shippingFields.phone }
|
|
|
|
autoComplete="tel"
|
|
|
|
onChange={ ( newValue ) =>
|
|
|
|
setShippingFields( {
|
|
|
|
...shippingFields,
|
|
|
|
phone: newValue,
|
|
|
|
} )
|
|
|
|
}
|
|
|
|
required={
|
|
|
|
attributes.requirePhoneField
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
) }
|
2020-03-12 09:41:35 +00:00
|
|
|
<CheckboxControl
|
2020-03-17 11:45:33 +00:00
|
|
|
className="wc-block-checkout__use-address-for-billing"
|
|
|
|
label={ __(
|
|
|
|
'Use same address for billing',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
checked={ useShippingAddressAsBilling }
|
|
|
|
onChange={ ( isChecked ) =>
|
|
|
|
setUseShippingAsBilling( isChecked )
|
2020-03-12 09:41:35 +00:00
|
|
|
}
|
|
|
|
/>
|
|
|
|
</FormStep>
|
2020-02-21 16:40:25 +00:00
|
|
|
) }
|
2020-03-17 11:45:33 +00:00
|
|
|
{ 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
|
|
|
|
onChange={ setBillingFields }
|
|
|
|
type="billing"
|
|
|
|
values={ billingFields }
|
|
|
|
fields={ Object.keys( addressFields ) }
|
|
|
|
fieldConfig={ addressFields }
|
|
|
|
/>
|
|
|
|
</FormStep>
|
2020-02-21 16:40:25 +00:00
|
|
|
) }
|
2020-03-20 16:48:11 +00:00
|
|
|
{ SHIPPING_ENABLED && (
|
|
|
|
<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 />
|
|
|
|
) : (
|
2020-03-17 11:45:33 +00:00
|
|
|
<ShippingRatesControl
|
|
|
|
address={
|
|
|
|
shippingFields.country
|
|
|
|
? {
|
|
|
|
address_1:
|
|
|
|
shippingFields.address_1,
|
|
|
|
address_2:
|
|
|
|
shippingFields.apartment,
|
|
|
|
city:
|
|
|
|
shippingFields.city,
|
|
|
|
state:
|
|
|
|
shippingFields.state,
|
|
|
|
postcode:
|
|
|
|
shippingFields.postcode,
|
|
|
|
country:
|
|
|
|
shippingFields.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
|
|
|
|
}
|
|
|
|
/>
|
2020-03-20 16:48:11 +00:00
|
|
|
) }
|
|
|
|
<CheckboxControl
|
|
|
|
className="wc-block-checkout__add-note"
|
|
|
|
label="Add order notes?"
|
|
|
|
checked={
|
|
|
|
selectedShippingRate.orderNote
|
|
|
|
}
|
|
|
|
onChange={ () =>
|
|
|
|
setSelectedShippingRate( {
|
|
|
|
...selectedShippingRate,
|
|
|
|
orderNote: ! selectedShippingRate.orderNote,
|
|
|
|
} )
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</FormStep>
|
|
|
|
) }
|
2020-03-17 11:45:33 +00:00
|
|
|
<FormStep
|
|
|
|
id="payment-method"
|
|
|
|
className="wc-block-checkout__payment-method"
|
|
|
|
title={ __(
|
|
|
|
'Payment method',
|
2020-03-05 19:54:05 +00:00
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
2020-03-17 11:45:33 +00:00
|
|
|
description={ __(
|
|
|
|
'Select a payment method below.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
>
|
|
|
|
<PaymentMethods />
|
|
|
|
{ /*@todo this should be something the payment method controls*/ }
|
|
|
|
<CheckboxControl
|
|
|
|
className="wc-block-checkout__save-card-info"
|
|
|
|
label={ __(
|
|
|
|
'Save payment information to my account for future purchases.',
|
|
|
|
'woo-gutenberg-products-block'
|
2020-03-17 10:30:52 +00:00
|
|
|
) }
|
2020-03-17 11:45:33 +00:00
|
|
|
checked={ shouldSavePayment }
|
|
|
|
onChange={ () =>
|
|
|
|
setShouldSavePayment(
|
|
|
|
! shouldSavePayment
|
|
|
|
)
|
|
|
|
}
|
2020-03-16 16:38:24 +00:00
|
|
|
/>
|
2020-03-17 11:45:33 +00:00
|
|
|
</FormStep>
|
|
|
|
<div className="wc-block-checkout__actions">
|
|
|
|
{ attributes.showReturnToCart && (
|
|
|
|
<ReturnToCartButton
|
|
|
|
link={ getSetting(
|
|
|
|
'page-' + attributes?.cartPageId,
|
|
|
|
false
|
|
|
|
) }
|
|
|
|
/>
|
|
|
|
) }
|
|
|
|
<PlaceOrderButton />
|
|
|
|
</div>
|
|
|
|
{ attributes.showPolicyLinks && <Policies /> }
|
|
|
|
</CheckoutForm>
|
|
|
|
</Main>
|
|
|
|
<Sidebar className="wc-block-checkout__sidebar">
|
|
|
|
<CheckoutSidebar
|
|
|
|
cartCoupons={ cartCoupons }
|
|
|
|
cartItems={ cartItems }
|
|
|
|
cartTotals={ cartTotals }
|
|
|
|
/>
|
|
|
|
</Sidebar>
|
|
|
|
</SidebarLayout>
|
|
|
|
</CheckoutProvider>
|
|
|
|
</ValidationContextProvider>
|
2019-12-03 14:12:46 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Block;
|