/** * Catalog Mode — the fields behind WooCommerce ▸ Catalog Mode. * * Split out of `shop/fields.tsx`, where the whole section was the one switch * that turned buying off. That switch is still the first thing here, but it now * has the rest of the decision under it: *what* goes (prices, the buying row, * the quantity picker, the related rail), what stands in for the button once it * has gone, whether shoppers are told why, and how much of the store any of it * reaches — a wholesale catalogue and a showroom want opposite answers, and * neither is served by hiding everything everywhere. * * Every card below stays live while the switch is off: these are the terms * catalog mode will run on, and they are worth setting *before* a shop stops * selling rather than after. */ import { __, sprintf } from '@wordpress/i18n'; import { Button, CheckboxControl, Dropdown, MenuGroup, MenuItem, TextControl, TextareaControl, ToggleControl, Tooltip, } from '@wordpress/components'; import { useRef } from '@wordpress/element'; import type { ReactNode } from 'react'; import { Braces, EyeOff, Globe, Info, MessageSquare, MousePointerClick, Undo2, } from 'lucide-react'; import FieldGroup from '../../../controls/field-group'; import SelectField from '../../../controls/select-field'; import type { WorkingSettings, SetPath } from '../../../types'; import type { WooSchema, TokenOption } from '../types'; interface CatalogModeFieldsProps { schema: WooSchema; working: WorkingSettings; setPath: SetPath; } /** * Every key this section owns, with the value `Settings::defaults()` gives it. * Paths are relative to the `woocommerce` branch — the switch sits at its root, * everything it governs one level down under `catalog`. */ const DEFAULTS: Record< string, string | boolean > = { catalog_mode: false, 'catalog.hide_price': true, 'catalog.hide_add_to_cart': true, 'catalog.hide_quantity': true, 'catalog.hide_related': false, 'catalog.replace': 'none', 'catalog.button_text': '', 'catalog.button_url': '', 'catalog.notice': true, 'catalog.notice_text': '', 'catalog.scope': 'store', }; /** * What the front end prints when the stand-in's field is left blank — one per * kind, because they are different things: a button carries an instruction, a * line of text carries an explanation. Mirrors * `WooCommerce::catalog_default_label()`. * * @param replace Current replacement kind. */ const standInPlaceholder = ( replace: string ) => 'text' === replace ? __( 'Price on request', 'aureon' ) : __( 'View product', 'aureon' ); // Ditto for the notice — see `render_catalog_notice()`. const NOTICE_PLACEHOLDER = __( 'Our products are for display only. Please contact us for pricing and availability.', 'aureon' ); const wooOf = ( working: WorkingSettings ) => ( working?.woocommerce ?? {} ) as Record< string, any >; /** * A text field with a variable picker beside it: the `{}` button opens the * list of everything this wording can carry, grouped by what each one needs to * resolve, and picking one drops it in at the caret — not at the end, since * the common edit is wrapping a variable in words ("Enquire about {product}"). * * The list is the server's ({@see WooSchema.catalog_tokens}), so the picker can * only ever offer what `WooCommerce::catalog_text()` actually resolves. * * @param root0 * @param root0.tokens Every variable the server offers. * @param root0.field Which kind of field this is, which decides how much of * that list applies to it. * @param root0.value Current value. * @param root0.onChange Receives the new value. * @param root0.children The control itself, which must render one input or * textarea inside this wrapper. */ function TokenField( { tokens, field, value, onChange, children, }: { tokens: TokenOption[]; field: 'text' | 'url'; value: string; onChange: ( value: string ) => void; children: ReactNode; } ) { const wrap = useRef< HTMLDivElement >( null ); const offered = tokens.filter( ( token ) => token.fields.includes( field ) ); const insert = ( token: string ) => { const input = wrap.current?.querySelector< HTMLInputElement | HTMLTextAreaElement >( 'input, textarea' ); const at = input?.selectionStart ?? value.length; const caret = at + token.length; onChange( value.slice( 0, at ) + token + value.slice( at ) ); // The value round-trips through the settings tree, so the field is // re-rendered before the caret can be restored. window.requestAnimationFrame( () => { input?.focus(); input?.setSelectionRange( caret, caret ); } ); }; const label = __( 'Insert a variable', 'aureon' ); const groups: { scope: TokenOption[ 'scope' ]; title: string }[] = [ { scope: 'product', title: __( 'Product', 'aureon' ) }, { scope: 'store', title: __( 'Store', 'aureon' ) }, ]; return (
{ children } (
); } export function CatalogModeFields( { schema, working, setPath, }: CatalogModeFieldsProps ) { const woo = wooOf( working ); const catalog = ( woo.catalog ?? {} ) as Record< string, any >; const enabled = !! woo.catalog_mode; const replace = ( catalog.replace ?? DEFAULTS[ 'catalog.replace' ] ) as string; const buttonText = ( catalog.button_text ?? '' ) as string; const buttonUrl = ( catalog.button_url ?? '' ) as string; const notice = ( catalog.notice ?? DEFAULTS[ 'catalog.notice' ] ) as boolean; const noticeText = ( catalog.notice_text ?? '' ) as string; const scope = ( catalog.scope ?? DEFAULTS[ 'catalog.scope' ] ) as string; const tokens = schema.catalog_tokens ?? []; // A button is labelled, a message is written — so the field says which of // the two it is holding. const standInLabel = 'text' === replace ? __( 'Message text', 'aureon' ) : __( 'Button text', 'aureon' ); /** * Read one of DEFAULTS' paths out of the `woocommerce` branch. * * @param path Dot path relative to that branch. */ const valueAt = ( path: string ) => { const value = path .split( '.' ) .reduce< any >( ( node, key ) => node?.[ key ], woo ); return undefined === value || null === value ? DEFAULTS[ path ] : value; }; const hides = ( key: string ) => !! valueAt( `catalog.hide_${ key }` ); const setHide = ( key: string, checked: boolean ) => setPath( `woocommerce.catalog.hide_${ key }`, checked ); const isDefault = Object.keys( DEFAULTS ).every( ( path ) => valueAt( path ) === DEFAULTS[ path ] ); const resetAll = () => Object.keys( DEFAULTS ).forEach( ( path ) => setPath( `woocommerce.${ path }`, DEFAULTS[ path ] ) ); return (
{ /* Boxed by `.components-toggle-control` itself (see style.scss), so it needs no card of its own here. */ } setPath( 'woocommerce.catalog_mode', next ) } /> setHide( 'price', checked ) } /> setHide( 'add_to_cart', checked ) } /> setHide( 'quantity', checked ) } /> setHide( 'related', checked ) } /> setPath( 'woocommerce.catalog.replace', value ) } /> { 'none' !== replace && ( <> { standInLabel } setPath( 'woocommerce.catalog.button_text', value ) } > setPath( 'woocommerce.catalog.button_text', value ) } />

{ sprintf( /* translators: %s: the default wording, e.g. “View product”. */ __( 'Leave blank to use “%s”.', 'aureon' ), standInPlaceholder( replace ) ) }

{ /* Only a button goes anywhere — a line of text has nowhere to take the shopper. */ } { 'button' === replace && ( <> { __( 'Button link', 'aureon' ) } setPath( 'woocommerce.catalog.button_url', value ) } > setPath( 'woocommerce.catalog.button_url', value ) } />

{ __( 'Leave blank to link to the product page. A contact page or a mailto: address works too — e.g. mailto:{email}?subject=Enquiry about {product}.', 'aureon' ) }

) } ) }
setPath( 'woocommerce.catalog.notice', next ) } /> { notice && ( <> { __( 'Notice text', 'aureon' ) } setPath( 'woocommerce.catalog.notice_text', value ) } > setPath( 'woocommerce.catalog.notice_text', value ) } />

{ __( 'Leave blank for the default wording.', 'aureon' ) }

) }
setPath( 'woocommerce.catalog.scope', value ) } />

{ ! isDefault && ( ) }
); } export default CatalogModeFields;