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-03 10:46:53 +00:00
|
|
|
import AddressForm from '@woocommerce/base-components/address-form';
|
2019-12-06 13:18:55 +00:00
|
|
|
import FormStep from '@woocommerce/base-components/checkout/form-step';
|
|
|
|
import CheckoutForm from '@woocommerce/base-components/checkout/form';
|
2019-12-16 22:13:41 +00:00
|
|
|
import NoShipping from '@woocommerce/base-components/checkout/no-shipping';
|
|
|
|
import TextInput from '@woocommerce/base-components/text-input';
|
2020-02-26 15:50:53 +00:00
|
|
|
import ShippingRatesControl, {
|
|
|
|
Packages,
|
|
|
|
} from '@woocommerce/base-components/shipping-rates-control';
|
2020-01-06 22:28:09 +00:00
|
|
|
import { CheckboxControl } from '@wordpress/components';
|
2020-02-14 03:43:13 +00:00
|
|
|
import { getCurrencyFromPriceResponse } from '@woocommerce/base-utils';
|
|
|
|
import FormattedMonetaryAmount from '@woocommerce/base-components/formatted-monetary-amount';
|
2020-01-06 22:28:09 +00:00
|
|
|
import CheckoutProvider from '@woocommerce/base-context/checkout-context';
|
|
|
|
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-01-06 22:28:09 +00:00
|
|
|
|
2019-12-03 14:12:46 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
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-04 15:13:38 +00:00
|
|
|
const Block = ( { attributes, 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 );
|
|
|
|
const [ shippingFields, setShippingFields ] = useState( {} );
|
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;
|
|
|
|
|
2019-12-03 14:12:46 +00:00
|
|
|
return (
|
2020-01-06 22:28:09 +00:00
|
|
|
<CheckoutProvider>
|
|
|
|
<ExpressCheckoutFormControl />
|
|
|
|
<CheckoutForm>
|
2019-12-16 22:13:41 +00:00
|
|
|
<FormStep
|
2020-03-04 15:13:38 +00:00
|
|
|
id="contact-fields"
|
|
|
|
className="wc-block-checkout__contact-fields"
|
2019-12-16 22:13:41 +00:00
|
|
|
title={ __(
|
2020-01-06 22:28:09 +00:00
|
|
|
'Contact information',
|
2019-12-16 22:13:41 +00:00
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
description={ __(
|
2020-01-06 22:28:09 +00:00
|
|
|
"We'll use this email to send you details and updates about your order.",
|
2019-12-16 22:13:41 +00:00
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
2020-01-06 22:28:09 +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>
|
|
|
|
) }
|
2019-12-16 22:13:41 +00:00
|
|
|
>
|
2020-01-06 22:28:09 +00:00
|
|
|
<TextInput
|
|
|
|
type="email"
|
|
|
|
label={ __(
|
|
|
|
'Email address',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
value={ contactFields.email }
|
2020-02-19 15:10:26 +00:00
|
|
|
autoComplete="email"
|
2020-01-06 22:28:09 +00:00
|
|
|
onChange={ ( newValue ) =>
|
|
|
|
setContactFields( {
|
|
|
|
...contactFields,
|
|
|
|
email: newValue,
|
|
|
|
} )
|
|
|
|
}
|
2020-03-03 10:46:53 +00:00
|
|
|
required={ true }
|
2020-01-06 22:28:09 +00:00
|
|
|
/>
|
|
|
|
<CheckboxControl
|
|
|
|
className="wc-block-checkout__keep-updated"
|
|
|
|
label={ __(
|
|
|
|
'Keep me up to date on news and exclusive offers',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
checked={ contactFields.keepUpdated }
|
|
|
|
onChange={ () =>
|
|
|
|
setContactFields( {
|
|
|
|
...contactFields,
|
|
|
|
keepUpdated: ! contactFields.keepUpdated,
|
|
|
|
} )
|
|
|
|
}
|
|
|
|
/>
|
2019-12-16 22:13:41 +00:00
|
|
|
</FormStep>
|
2020-02-21 16:40:25 +00:00
|
|
|
{ SHIPPING_ENABLED && (
|
|
|
|
<FormStep
|
|
|
|
id="shipping-fields"
|
|
|
|
className="wc-block-checkout__shipping-fields"
|
|
|
|
title={ __(
|
|
|
|
'Shipping address',
|
2019-12-16 22:13:41 +00:00
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
2020-02-21 16:40:25 +00:00
|
|
|
description={ __(
|
|
|
|
'Enter the physical address where you want us to deliver your order.',
|
2019-12-16 22:13:41 +00:00
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
2020-02-21 16:40:25 +00:00
|
|
|
>
|
2020-03-03 10:46:53 +00:00
|
|
|
<AddressForm
|
|
|
|
onChange={ setShippingFields }
|
|
|
|
values={ shippingFields }
|
2020-02-14 03:43:13 +00:00
|
|
|
/>
|
2020-02-21 16:40:25 +00:00
|
|
|
<TextInput
|
|
|
|
type="tel"
|
2020-02-14 03:43:13 +00:00
|
|
|
label={ __(
|
2020-02-21 16:40:25 +00:00
|
|
|
'Phone',
|
2019-12-16 22:13:41 +00:00
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
2020-02-21 16:40:25 +00:00
|
|
|
value={ shippingFields.phone }
|
|
|
|
autoComplete="tel"
|
2020-02-14 03:43:13 +00:00
|
|
|
onChange={ ( newValue ) =>
|
|
|
|
setShippingFields( {
|
|
|
|
...shippingFields,
|
2020-02-21 16:40:25 +00:00
|
|
|
phone: newValue,
|
2020-02-14 03:43:13 +00:00
|
|
|
} )
|
|
|
|
}
|
2020-03-03 10:46:53 +00:00
|
|
|
required={ true }
|
2020-02-14 03:43:13 +00:00
|
|
|
/>
|
2020-02-21 16:40:25 +00:00
|
|
|
<CheckboxControl
|
|
|
|
className="wc-block-checkout__use-address-for-billing"
|
2020-02-14 03:43:13 +00:00
|
|
|
label={ __(
|
2020-02-21 16:40:25 +00:00
|
|
|
'Use same address for billing',
|
2019-12-16 22:13:41 +00:00
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
2020-03-04 15:13:38 +00:00
|
|
|
checked={ useShippingAddressAsBilling }
|
|
|
|
onChange={ ( isChecked ) =>
|
|
|
|
setUseShippingAsBilling( isChecked )
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</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
|
|
|
|
onChange={ setBillingFields }
|
|
|
|
type="billing"
|
|
|
|
values={ billingFields }
|
|
|
|
/>
|
|
|
|
<TextInput
|
|
|
|
type="tel"
|
|
|
|
label={ __(
|
|
|
|
'Phone',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
value={ billingFields.phone }
|
|
|
|
autoComplete="tel"
|
|
|
|
onChange={ ( newValue ) =>
|
|
|
|
setBillingFields( {
|
|
|
|
...billingFields,
|
|
|
|
phone: newValue,
|
2020-02-14 03:43:13 +00:00
|
|
|
} )
|
|
|
|
}
|
2020-03-04 15:13:38 +00:00
|
|
|
required={ true }
|
2020-02-14 03:43:13 +00:00
|
|
|
/>
|
|
|
|
</FormStep>
|
2019-12-06 13:18:55 +00:00
|
|
|
) }
|
2020-02-21 16:40:25 +00:00
|
|
|
{ SHIPPING_ENABLED &&
|
2020-02-26 15:50:53 +00:00
|
|
|
( shippingRates.length === 0 && isEditor ? (
|
2020-02-21 16:40:25 +00:00
|
|
|
<NoShipping />
|
|
|
|
) : (
|
|
|
|
<FormStep
|
|
|
|
id="shipping-option"
|
|
|
|
className="wc-block-checkout__shipping-option"
|
|
|
|
title={ __(
|
|
|
|
'Shipping options',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
description={ __(
|
|
|
|
'Select your shipping method below.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
>
|
2020-02-26 15:50:53 +00:00
|
|
|
{ shippingRates.length > 0 ? (
|
|
|
|
<Packages
|
|
|
|
renderOption={
|
|
|
|
renderShippingRatesControlOption
|
|
|
|
}
|
|
|
|
shippingRates={ shippingRates }
|
|
|
|
selected={ [
|
2020-02-27 18:28:36 +00:00
|
|
|
// This is only rendered in the editor, with placeholder
|
|
|
|
// shippingRates, so we can safely fallback to set the
|
|
|
|
// first shipping rate as selected and ignore setting
|
|
|
|
// an onChange prop.
|
2020-02-26 15:50:53 +00:00
|
|
|
shippingRates[ 0 ].shipping_rates[ 0 ]
|
|
|
|
.rate_id,
|
|
|
|
] }
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<ShippingRatesControl
|
|
|
|
address={
|
|
|
|
shippingFields.country
|
|
|
|
? {
|
|
|
|
address_1:
|
2020-03-03 10:46:53 +00:00
|
|
|
shippingFields.address_1,
|
2020-02-26 15:50:53 +00:00
|
|
|
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'
|
|
|
|
) }
|
|
|
|
selected={ selectedShippingRate.methods }
|
|
|
|
renderOption={
|
|
|
|
renderShippingRatesControlOption
|
|
|
|
}
|
|
|
|
onChange={ ( newMethods ) =>
|
|
|
|
setSelectedShippingRate( {
|
|
|
|
...selectedShippingRate,
|
|
|
|
methods: newMethods,
|
|
|
|
} )
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
) }
|
2020-02-21 16:40:25 +00:00
|
|
|
<CheckboxControl
|
|
|
|
className="wc-block-checkout__add-note"
|
|
|
|
label="Add order notes?"
|
2020-02-26 15:50:53 +00:00
|
|
|
checked={ selectedShippingRate.orderNote }
|
2020-02-21 16:40:25 +00:00
|
|
|
onChange={ () =>
|
2020-02-26 15:50:53 +00:00
|
|
|
setSelectedShippingRate( {
|
|
|
|
...selectedShippingRate,
|
|
|
|
orderNote: ! selectedShippingRate.orderNote,
|
2020-02-21 16:40:25 +00:00
|
|
|
} )
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</FormStep>
|
|
|
|
) ) }
|
2020-01-06 22:28:09 +00:00
|
|
|
<FormStep
|
|
|
|
id="payment-method"
|
|
|
|
className="wc-block-checkout__payment-method"
|
|
|
|
title={ __(
|
|
|
|
'Payment method',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
description={ __(
|
|
|
|
'Select a payment method below.',
|
2019-12-16 22:13:41 +00:00
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
2020-01-06 22:28:09 +00:00
|
|
|
>
|
|
|
|
<PaymentMethods />
|
|
|
|
{ /*@todo this should be something the payment method controls*/ }
|
|
|
|
<CheckboxControl
|
2020-01-10 09:42:53 +00:00
|
|
|
className="wc-block-checkout__save-card-info"
|
2020-01-06 22:28:09 +00:00
|
|
|
label={ __(
|
|
|
|
'Save payment information to my account for future purchases.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
checked={ shouldSavePayment }
|
|
|
|
onChange={ () =>
|
|
|
|
setShouldSavePayment( ! shouldSavePayment )
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</FormStep>
|
|
|
|
</CheckoutForm>
|
|
|
|
</CheckoutProvider>
|
2019-12-03 14:12:46 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Block;
|