2019-12-03 14:12:46 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2020-08-14 11:08:16 +00:00
|
|
|
import classnames from 'classnames';
|
2020-08-31 10:17:42 +00:00
|
|
|
import { useEffect } from '@wordpress/element';
|
2019-12-06 13:18:55 +00:00
|
|
|
import { __ } from '@wordpress/i18n';
|
2020-03-06 12:20:17 +00:00
|
|
|
import {
|
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,
|
|
|
|
} from '@woocommerce/base-components/cart-checkout';
|
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-04-02 09:27:54 +00:00
|
|
|
useValidationContext,
|
2020-03-17 11:45:33 +00:00
|
|
|
} from '@woocommerce/base-context';
|
2020-08-31 10:17:42 +00:00
|
|
|
import { useStoreCart, useStoreNotices } from '@woocommerce/base-hooks';
|
|
|
|
import { CheckoutExpressPayment } from '@woocommerce/base-components/payment-methods';
|
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-08-31 10:17:42 +00:00
|
|
|
import { CHECKOUT_ALLOWS_GUEST } from '@woocommerce/block-settings';
|
2020-01-06 22:28:09 +00:00
|
|
|
|
2019-12-03 14:12:46 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2020-08-31 10:17:42 +00:00
|
|
|
import CheckoutForm from './form';
|
2020-04-06 15:35:09 +00:00
|
|
|
import CheckoutSidebar from './sidebar';
|
|
|
|
import CheckoutOrderError from './checkout-order-error';
|
2020-08-31 10:17:42 +00:00
|
|
|
import { LOGIN_TO_CHECKOUT_URL } from './utils';
|
2019-12-03 14:12:46 +00:00
|
|
|
import './style.scss';
|
|
|
|
|
2020-05-06 10:21:30 +00:00
|
|
|
/**
|
|
|
|
* Renders the Checkout block wrapped within the CheckoutProvider.
|
|
|
|
*
|
|
|
|
* @param {Object} props Component props.
|
|
|
|
* @return {*} The component.
|
|
|
|
*/
|
2020-04-08 15:03:39 +00:00
|
|
|
const Block = ( props ) => {
|
|
|
|
return (
|
|
|
|
<CheckoutProvider>
|
|
|
|
<Checkout { ...props } />
|
|
|
|
</CheckoutProvider>
|
|
|
|
);
|
|
|
|
};
|
2020-03-23 20:13:41 +00:00
|
|
|
|
2020-05-06 10:21:30 +00:00
|
|
|
/**
|
|
|
|
* Main Checkout Component.
|
|
|
|
*
|
|
|
|
* @param {Object} props Component props.
|
2020-09-20 23:54:08 +00:00
|
|
|
* @param {Object} props.attributes Incoming block attributes.
|
|
|
|
* @param {function(any):any} props.scrollToTop Function for scrolling to top.
|
2020-05-06 10:21:30 +00:00
|
|
|
*/
|
2020-04-07 15:41:22 +00:00
|
|
|
const Checkout = ( { attributes, scrollToTop } ) => {
|
2020-04-01 09:27:53 +00:00
|
|
|
const { isEditor } = useEditorContext();
|
2020-04-09 14:01:11 +00:00
|
|
|
const {
|
|
|
|
cartItems,
|
|
|
|
cartTotals,
|
|
|
|
cartCoupons,
|
|
|
|
cartNeedsPayment,
|
|
|
|
} = useStoreCart();
|
2020-04-04 17:36:46 +00:00
|
|
|
const {
|
|
|
|
hasOrder,
|
|
|
|
hasError: checkoutHasError,
|
2020-04-15 15:43:03 +00:00
|
|
|
isIdle: checkoutIsIdle,
|
2020-05-04 19:40:09 +00:00
|
|
|
customerId,
|
2020-04-04 17:36:46 +00:00
|
|
|
} = useCheckoutContext();
|
2020-04-20 10:11:32 +00:00
|
|
|
const {
|
|
|
|
hasValidationErrors,
|
|
|
|
showAllValidationErrors,
|
|
|
|
} = useValidationContext();
|
|
|
|
const { hasNoticesOfType } = useStoreNotices();
|
2020-03-30 14:32:23 +00:00
|
|
|
|
2020-04-20 10:11:32 +00:00
|
|
|
const hasErrorsToDisplay =
|
|
|
|
checkoutIsIdle &&
|
|
|
|
checkoutHasError &&
|
|
|
|
( hasValidationErrors || hasNoticesOfType( 'default' ) );
|
2020-05-06 10:21:30 +00:00
|
|
|
|
2020-04-02 09:27:54 +00:00
|
|
|
useEffect( () => {
|
2020-04-20 10:11:32 +00:00
|
|
|
if ( hasErrorsToDisplay ) {
|
2020-04-02 09:27:54 +00:00
|
|
|
showAllValidationErrors();
|
|
|
|
scrollToTop( { focusableSelector: 'input:invalid' } );
|
2020-04-04 17:36:46 +00:00
|
|
|
}
|
2020-07-31 15:17:01 +00:00
|
|
|
}, [ hasErrorsToDisplay, scrollToTop, showAllValidationErrors ] );
|
2020-04-02 09:27:54 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-08-05 08:35:38 +00:00
|
|
|
if ( ! isEditor && ! customerId && ! CHECKOUT_ALLOWS_GUEST ) {
|
2020-05-04 19:40:09 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{ __(
|
|
|
|
'You must be logged in to checkout. ',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
2020-08-31 10:17:42 +00:00
|
|
|
<a href={ LOGIN_TO_CHECKOUT_URL }>
|
2020-05-04 19:40:09 +00:00
|
|
|
{ __(
|
|
|
|
'Click here to log in.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
</a>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-08-14 11:08:16 +00:00
|
|
|
const checkoutClassName = classnames( 'wc-block-checkout', {
|
|
|
|
'has-dark-controls': attributes.hasDarkControls,
|
|
|
|
} );
|
2019-12-03 14:12:46 +00:00
|
|
|
return (
|
2020-03-30 12:43:42 +00:00
|
|
|
<>
|
2020-08-14 11:08:16 +00:00
|
|
|
<SidebarLayout className={ checkoutClassName }>
|
2020-04-03 13:17:09 +00:00
|
|
|
<Main className="wc-block-checkout__main">
|
2020-08-20 14:14:12 +00:00
|
|
|
{ cartNeedsPayment && <CheckoutExpressPayment /> }
|
2020-08-31 10:17:42 +00:00
|
|
|
<CheckoutForm
|
|
|
|
showApartmentField={ attributes.showApartmentField }
|
|
|
|
showCompanyField={ attributes.showCompanyField }
|
|
|
|
showOrderNotes={ attributes.showOrderNotes }
|
|
|
|
showPhoneField={ attributes.showPhoneField }
|
|
|
|
requireCompanyField={ attributes.requireCompanyField }
|
|
|
|
requirePhoneField={ attributes.requirePhoneField }
|
|
|
|
/>
|
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>
|
2020-05-05 13:19:47 +00:00
|
|
|
<Sidebar className="wc-block-checkout__sidebar">
|
|
|
|
<CheckoutSidebar
|
|
|
|
cartCoupons={ cartCoupons }
|
|
|
|
cartItems={ cartItems }
|
|
|
|
cartTotals={ cartTotals }
|
|
|
|
/>
|
|
|
|
</Sidebar>
|
2020-03-30 12:43:42 +00:00
|
|
|
</SidebarLayout>
|
|
|
|
</>
|
2019-12-03 14:12:46 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2020-03-23 11:22:00 +00:00
|
|
|
export default withScrollToTop( Block );
|