/** * External dependencies */ import classnames from 'classnames'; import { useEffect } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; import { PlaceOrderButton, Policies, ReturnToCartButton, } from '@woocommerce/base-components/cart-checkout'; import { CheckoutProvider, useCheckoutContext, useEditorContext, useValidationContext, } from '@woocommerce/base-context'; import { useStoreCart, useStoreNotices } from '@woocommerce/base-hooks'; import { CheckoutExpressPayment } from '@woocommerce/base-components/payment-methods'; import { Sidebar, SidebarLayout, Main, } from '@woocommerce/base-components/sidebar-layout'; import { getSetting } from '@woocommerce/settings'; import withScrollToTop from '@woocommerce/base-hocs/with-scroll-to-top'; import { CHECKOUT_ALLOWS_GUEST } from '@woocommerce/block-settings'; /** * Internal dependencies */ import CheckoutForm from './form'; import CheckoutSidebar from './sidebar'; import CheckoutOrderError from './checkout-order-error'; import { LOGIN_TO_CHECKOUT_URL } from './utils'; import './style.scss'; /** * Renders the Checkout block wrapped within the CheckoutProvider. * * @param {Object} props Component props. * @return {*} The component. */ const Block = ( props ) => { return ( ); }; /** * Main Checkout Component. * * @param {Object} props Component props. * @return {*} The component. */ const Checkout = ( { attributes, scrollToTop } ) => { const { isEditor } = useEditorContext(); const { cartItems, cartTotals, cartCoupons, cartNeedsPayment, } = useStoreCart(); const { hasOrder, hasError: checkoutHasError, isIdle: checkoutIsIdle, customerId, } = useCheckoutContext(); const { hasValidationErrors, showAllValidationErrors, } = useValidationContext(); const { hasNoticesOfType } = useStoreNotices(); const hasErrorsToDisplay = checkoutIsIdle && checkoutHasError && ( hasValidationErrors || hasNoticesOfType( 'default' ) ); useEffect( () => { if ( hasErrorsToDisplay ) { showAllValidationErrors(); scrollToTop( { focusableSelector: 'input:invalid' } ); } }, [ hasErrorsToDisplay, scrollToTop, showAllValidationErrors ] ); if ( ! isEditor && ! hasOrder ) { return ; } if ( ! isEditor && ! customerId && ! CHECKOUT_ALLOWS_GUEST ) { return ( <> { __( 'You must be logged in to checkout. ', 'woo-gutenberg-products-block' ) } { __( 'Click here to log in.', 'woo-gutenberg-products-block' ) } ); } const checkoutClassName = classnames( 'wc-block-checkout', { 'has-dark-controls': attributes.hasDarkControls, } ); return ( <>
{ cartNeedsPayment && }
{ attributes.showReturnToCart && ( ) }
{ attributes.showPolicyLinks && }
); }; export default withScrollToTop( Block );