/** * 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 (
{ __( 'Product variables are blank on shop and category pages, where no single product is in scope.', 'aureon' ) }
> ) } />{ 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' ) }{ __( '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' ) }
> ) } > ) }{ __( 'Leave blank for the default wording.', 'aureon' ) }
> ) }{ __( 'This setting hides purchase-related elements only. Products will remain visible to all visitors.', 'aureon' ) }