/** * External dependencies */ import { useMemo, useEffect } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; import { AddressForm, FormStep, CheckoutForm, PlaceOrderButton, Policies, ReturnToCartButton, ShippingRatesControl, } from '@woocommerce/base-components/cart-checkout'; import { ValidatedTextInput } from '@woocommerce/base-components/text-input'; import CheckboxControl from '@woocommerce/base-components/checkbox-control'; import { getCurrencyFromPriceResponse, getShippingRatesPackageCount, getShippingRatesRateCount, } from '@woocommerce/base-utils'; import FormattedMonetaryAmount from '@woocommerce/base-components/formatted-monetary-amount'; import { CheckoutProvider, useCheckoutContext, useEditorContext, useShippingDataContext, useValidationContext, StoreNoticesProvider, } from '@woocommerce/base-context'; import { useStoreCart, usePaymentMethods, useStoreNotices, useCheckoutAddress, } from '@woocommerce/base-hooks'; import { ExpressCheckoutFormControl, PaymentMethods, } from '@woocommerce/base-components/payment-methods'; import { decodeEntities } from '@wordpress/html-entities'; 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_SHOW_LOGIN_REMINDER, CHECKOUT_ALLOWS_GUEST, } from '@woocommerce/block-settings'; /** * Internal dependencies */ import CheckoutSidebar from './sidebar'; import CheckoutOrderError from './checkout-order-error'; import NoShippingPlaceholder from './no-shipping-placeholder'; import './style.scss'; /** * Renders the Checkout block wrapped within the CheckoutProvider. * * @param {Object} props Component props. * @return {*} The component. */ const Block = ( props ) => { return ( ); }; /** * Renders a shipping rate control option. * * @param {Object} option Shipping Rate. */ const renderShippingRatesControlOption = ( option ) => ( { label: decodeEntities( option.name ), value: option.rate_id, description: decodeEntities( option.description ), secondaryLabel: ( ), secondaryDescription: decodeEntities( option.delivery_time ), } ); /** * 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, isProcessing: checkoutIsProcessing, customerId, } = useCheckoutContext(); const { hasValidationErrors, showAllValidationErrors, } = useValidationContext(); const { shippingRates, shippingRatesLoading, needsShipping, } = useShippingDataContext(); const { paymentMethods } = usePaymentMethods(); const { hasNoticesOfType } = useStoreNotices(); const { defaultAddressFields, shippingFields, setShippingFields, billingFields, setBillingFields, setEmail, setPhone, shippingAsBilling, setShippingAsBilling, showBillingFields, } = useCheckoutAddress(); const addressFields = useMemo( () => { return { ...defaultAddressFields, company: { ...defaultAddressFields.company, hidden: ! attributes.showCompanyField, required: attributes.requireCompanyField, }, address_2: { ...defaultAddressFields.address_2, hidden: ! attributes.showApartmentField, }, }; }, [ defaultAddressFields, attributes ] ); const hasErrorsToDisplay = checkoutIsIdle && checkoutHasError && ( hasValidationErrors || hasNoticesOfType( 'default' ) ); useEffect( () => { if ( hasErrorsToDisplay ) { showAllValidationErrors(); scrollToTop( { focusableSelector: 'input:invalid' } ); } }, [ hasErrorsToDisplay ] ); if ( ! isEditor && ! hasOrder ) { return ; } const loginToCheckoutUrl = `/wp-login.php?redirect_to=${ encodeURIComponent( window.location.href ) }`; if ( ! 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 loginPrompt = () => CHECKOUT_SHOW_LOGIN_REMINDER && ! customerId && ( <> { __( 'Already have an account? ', 'woo-gutenberg-products-block' ) } { __( 'Log in.', 'woo-gutenberg-products-block' ) } > ); return ( <> { cartNeedsPayment && } { needsShipping && ( { attributes.showPhoneField && ( ) } setShippingAsBilling( isChecked ) } /> ) } { showBillingFields && ( ) } { needsShipping && ( 1 ? __( 'Select shipping options below.', 'woo-gutenberg-products-block' ) : '' } > { isEditor && ! getShippingRatesPackageCount( shippingRates ) ? ( ) : ( ) } ) } { cartNeedsPayment && ( 1 ? __( 'Select a payment method below.', 'woo-gutenberg-products-block' ) : '' } > ) } { attributes.showReturnToCart && ( ) } { attributes.showPolicyLinks && } > ); }; export default withScrollToTop( Block );