/** * Checkout — one of WooCommerce's four store views (see `../index.tsx` for * the top-level hub). Its two tabs (Layout / Form) are two views of the same * settings, not separate topics — same call as Product Catalog's own * General/Product Card split — so both render inline behind one segmented * toggle (see Tabs' `segmented` variant) rather than each drilling into a * `WP_Customize_Section` of its own. That's true on both hosts: the * Customizer mounts this whole component into one section (WooCommerce's own * "Checkout", merged via `Customizer::WOOCOMMERCE_CORE_SECTION_MERGE`) same * as the standalone admin page mounts it inline. * * Core registered a handful of its own controls straight into that same * "Checkout" section — Company name/Address line 2/Phone field visibility, * the required-fields asterisk, and the Privacy Policy/Terms pages (see * `WC_Shop_Customizer::add_checkout_section()`) — as siblings of this mount, * rendered above it in the section regardless of which of this theme's own * tabs is open. None of those are React's to place, so `NativeFieldGroup` * below moves their actual DOM nodes into the Form tab once each one exists, * rather than only toggling a visibility flag on them where they already sit * — the fields end up inside the tab that's the right conceptual home for * them, not merely hidden/shown alongside it. * * They land in two cards, not one: the field-visibility controls answer "what * does this form collect", the privacy/terms ones "what does it commit the * customer to", and those are different questions. So each card gets its own * container and its own list of control ids, and `CheckoutFormFields` takes * them as `slots` — it decides which card they sit in, this file does the DOM * work of getting them there. */ import { __ } from '@wordpress/i18n'; import { useEffect, useRef, useState } from '@wordpress/element'; import { CreditCard } from 'lucide-react'; import Tabs, { TabPanel } from '../../../controls/tabs'; import { isCustomizerHost, onControlEmbedded } from '../../../customize-bridge'; import type { WorkingSettings, SetPath, SectionExtra } from '../../../types'; import type { WooSchema } from '../types'; import { CHECKOUT_TABS } from '../constants'; import { BlockCheckoutNotice, CheckoutLayoutFields, CheckoutFormFields, } from './fields'; import SectionIntro from '../../../controls/section-intro'; const ID_PREFIX = 'aureon-woocommerce-checkout'; // Core's own controls living inside the same "Checkout" section as this // mount — see the class docblock above. Each list is in the order those // controls should appear once moved into their card. const CORE_FIELD_CONTROLS = [ 'woocommerce_checkout_company_field', 'woocommerce_checkout_address_2_field', 'woocommerce_checkout_phone_field', 'woocommerce_checkout_highlight_required_fields', ]; const CORE_POLICY_CONTROLS = [ 'wp_page_for_privacy_policy', 'woocommerce_terms_page_id', 'woocommerce_checkout_privacy_policy_text', 'woocommerce_checkout_terms_and_conditions_checkbox_text', ]; /** * A container that adopts one named set of core's own checkout controls. * Rendered on the Customizer host only — the standalone admin page has no * `wp.customize` and so nothing to move. * * A plain container ref, not a React tree: these are core's own live * `WP_Customize_Control` DOM nodes, complete with their own event listeners * and `wp.customize` bindings — moving the actual element via `appendChild` * keeps every one of those intact, where re-rendering an equivalent node * from scratch would not. `TabPanel`'s `keepMounted` on the Form panel below * is what makes this safe: switching to Layout hides these containers rather * than unmounting them, so the moved nodes stay put instead of being torn * down and lost the moment their new parent disappeared. * * A single move isn't enough on its own, though: core's own * `reflowPaneContents()` periodically re-sorts every section's controls back * into that section's native DOM position by priority — on a new control * embedding, a window resize, and other points this file has no hook into — * which would silently undo the move made below. Rather than trying to * out-race that (unwinnable — core's reflow runs whenever it wants, not on * a schedule this effect can wait out), a `MutationObserver` on the section * they originally lived in re-applies the move every time core relocates * them back there. * * Two of these run side by side, each watching the same native section for * its own subset. That's deliberate rather than wasteful: a reflow returns * every control at once, so both observers firing on the same mutation is * exactly the behaviour wanted — each simply re-adopts its own list. * * @param root0 * @param root0.controls Ids of the core controls this container adopts. */ function NativeFieldGroup( { controls }: { controls: string[] } ) { const containerRef = useRef< HTMLDivElement >( null ); useEffect( () => { if ( ! isCustomizerHost() ) { return; } let nativeSection: Element | null = null; let observer: MutationObserver | undefined; const relocate = () => { controls.forEach( ( id ) => { const control = document.getElementById( `customize-control-${ id }` ); if ( control && control.parentElement !== containerRef.current ) { containerRef.current?.appendChild( control ); } } ); }; controls.forEach( ( id ) => onControlEmbedded( id, () => { // Captured from whichever control's callback fires first, // before that control's own move below — every one of these // starts out as a sibling in the same native section, so any // of them names the right element to watch. nativeSection ??= document.getElementById( `customize-control-${ id }` ) ?.parentElement ?? null; relocate(); if ( ! observer && nativeSection ) { observer = new MutationObserver( relocate ); observer.observe( nativeSection, { childList: true } ); } } ) ); return () => observer?.disconnect(); }, [ controls ] ); return (
); } interface CheckoutSectionProps { schema: WooSchema; working: WorkingSettings; setPath: SetPath; extras?: SectionExtra[]; onViewChange?: ( context: string ) => void; /** * See the same prop on `ShopSection` — avoids a duplicate header when * the top hub embeds this inline for the standalone page. */ showHeader?: boolean; } export default function CheckoutSection( { schema, working, setPath, extras, onViewChange, showHeader = true, }: CheckoutSectionProps ) { useEffect( () => { onViewChange?.( 'checkout' ); }, [ onViewChange ] ); const [ activeTab, setActiveTab ] = useState( 'layout' ); // Off the Customizer there are no core controls to adopt, so the two cards // that would hold them aren't rendered at all rather than left empty. const slots = isCustomizerHost() ? { fields: , policy: , } : undefined; return (
{ showHeader && (

{ __( 'Checkout', 'aureon' ) }

{ __( 'The checkout form.', 'aureon' ) }

) } { /* Above the tabs, not inside one: the block checkout greys out controls in both of them. */ }
); }