← Back
Editing: add-to-cart-form.js
(()=>{var t,e,o,i={772:(t,e,o)=>{"use strict";const i=window.wp.blocks,n=window.wp.data,r="core/editor",s=window.wc.wcTypes;class c{blocks=new Map;initialized=!1;attemptedRegisteredBlocks=new Set;constructor(){this.initializeSubscriptions()}static getInstance(){return c.instance||(c.instance=new c),c.instance}initializeSubscriptions(){if(this.initialized)return;const t=(0,n.subscribe)(()=>{const e=(0,n.select)(r);if(!e)return;const o=e.getCurrentPostType();if(o)if("post"===o||"page"===o)t(),this.blocks.forEach(t=>{if(t.isAvailableOnPostEditor){const e=t.variationName||t.metadata.name;this.hasAttemptedRegistration(e)||this.registerBlock(t)}}),this.initialized=!0;else{t();const o=String(e.getEditedPostSlug());this.currentTemplateId=o,this.handleTemplateChange(void 0),(0,n.subscribe)(()=>{const t=this.currentTemplateId;this.currentTemplateId=String(e.getEditedPostSlug()),t!==this.currentTemplateId&&this.handleTemplateChange(t)},r),this.initialized=!0}})}handleTemplateChange(t){(this.currentTemplateId?.includes("single-product")||t?.includes("single-product"))&&this.blocks.forEach(t=>{this.unregisterBlock(t),this.registerBlock(t)})}hasAttemptedRegistration(t){return this.attemptedRegisteredBlocks.has(t)}unregisterBlock(t){const{metadata:e,isVariationBlock:o,variationName:n}=t;try{o&&n?((0,i.unregisterBlockVariation)(e.name,n),this.attemptedRegisteredBlocks.delete(n)):((0,i.unregisterBlockType)(e.name),this.attemptedRegisteredBlocks.delete(e.name))}catch(t){console.debug(`Failed to unregister block ${e.name}:`,t)}}registerBlock(t){const{metadata:e,isVariationBlock:o,variationName:r,isAvailableOnPostEditor:c}=t;try{const a=r||e.name;if(this.hasAttemptedRegistration(a))return;const l=(0,n.select)("core/edit-site");if(!l&&!c)return;if(o)(0,i.registerBlockVariation)(e.name,{...e,name:r||e.name});else{const o=(0,s.isEmpty)(e?.ancestor)?["woocommerce/single-product"]:e?.ancestor,n=l&&this.currentTemplateId?.includes("single-product"),{title:r,description:c,keywords:d,styles:u,variations:p,...m}=e,h=(0,i.registerBlockType)(e,{...m,ancestor:n?void 0:o});this.blocks.set(a,{...t,metadata:{...e,title:h?.title??e.title,description:h?.description??e.description}})}this.attemptedRegisteredBlocks.add(a)}catch(t){console.error(`Failed to register block ${e.name}:`,t)}}registerBlockConfig(t){const e=t.variationName||t.metadata.name;this.blocks.set(e,t),this.registerBlock(t)}}var a=o(11789),l=o(11003);const d=JSON.parse('{"name":"woocommerce/add-to-cart-form","title":"Add to Cart with Options","description":"Display a button that lets customers add a product to their cart. Use the added options to optimize for different product types.","category":"woocommerce-product-elements","attributes":{"quantitySelectorStyle":{"type":"string","enum":["input","stepper"],"default":"input"}},"keywords":["WooCommerce"],"usesContext":["postId"],"textdomain":"woocommerce","supports":{"interactivity":true},"apiVersion":3,"$schema":"https://schemas.wp.org/trunk/block.json","viewScriptModule":"woocommerce/add-to-cart-form","style":"file:../woocommerce/add-to-cart-form-style.css","editorStyle":"file:../woocommerce/add-to-cart-form-editor.css"}'),u=window.wp.blockEditor;var p=o(27723),m=o(4921),h=(o(42405),o(10790));const b=({tag:t="div",width:e="100%",height:o="8px",maxWidth:i="",className:n="",borderRadius:r="",isStatic:s=!1,ariaMessage:c})=>(0,h.jsx)(t,{className:(0,m.A)("wc-block-components-skeleton__element",{"wc-block-components-skeleton__element--static":s},n),...c?{"aria-live":"polite","aria-label":c}:{"aria-hidden":"true"},style:{width:e,height:o,borderRadius:r,maxWidth:i}}),w=({isStatic:t=!1})=>(0,h.jsxs)("div",{className:"wc-block-components-skeleton",children:[(0,h.jsx)(b,{height:"16px",isStatic:t}),(0,h.jsx)(b,{height:"16px",isStatic:t}),(0,h.jsx)(b,{height:"16px",width:"80%",isStatic:t})]}),g=window.wp.components,k=window.wc.wcSettings;let f=function(t){return t.Input="input",t.Stepper="stepper",t}({});const y=t=>t===f.Input?(0,p.__)("Shoppers can enter a number of items to add to cart.","woocommerce"):t===f.Stepper?(0,p.__)("Shoppers can use buttons to change the number of items to add to cart.","woocommerce"):void 0,_=({quantitySelectorStyle:t,setAttributes:e})=>(0,h.jsx)(u.InspectorControls,{children:(0,h.jsx)(g.PanelBody,{title:(0,p.__)("Quantity Selector","woocommerce"),children:(0,h.jsxs)(g.__experimentalToggleGroupControl,{__nextHasNoMarginBottom:!0,value:t,isBlock:!0,onChange:t=>{e({quantitySelectorStyle:t})},help:y(t),children:[(0,h.jsx)(g.__experimentalToggleGroupControlOption,{label:(0,p.__)("Input","woocommerce"),value:f.Input}),(0,h.jsx)(g.__experimentalToggleGroupControlOption,{label:(0,p.__)("Stepper","woocommerce"),value:f.Stepper})]})})});var x=o(86087),v=o(51512),j=o.n(v);j()("wc-admin:tracks:stats");const S=j()("wc-admin:tracks");function B({children:t,className:e,actionLabel:o,onActionClick:i,status:n="info",...r}){return(0,h.jsx)(g.Notice,{...r,status:n,politeness:"polite",className:(0,m.$)("wc-block-editor-components-upgrade-downgrade-notice",e),actions:[{label:o,onClick:i,noDefaultClasses:!0,variant:"link"}],children:(0,h.jsx)("div",{className:"wc-block-editor-components-upgrade-downgrade-notice__text",children:t})})}o(49969);const O=({blocks:t,findCondition:e})=>{for(const o of t){if(e(o))return o;if(o.innerBlocks){const t=O({blocks:o.innerBlocks,findCondition:e});if(t)return t}}},T=({blockClientId:t})=>{const e=(0,x.createInterpolateElement)((0,p.__)("Upgrade to the <strongText /> for more flexibility. You can switch back anytime.","woocommerce"),{strongText:(0,h.jsx)("strong",{children:(0,p.__)("Add to Cart + Options block","woocommerce")})}),o=(0,p.__)("Use the Add to Cart + Options block","woocommerce");return(0,h.jsx)(B,{isDismissible:!1,actionLabel:o,onActionClick:async()=>{const e=await(async t=>{const e=(0,n.select)("core/block-editor").getBlocks(),o=O({blocks:e,findCondition:e=>e.name===d.name&&e.clientId===t});if(!o)return!1;const r=(0,i.createBlock)("woocommerce/add-to-cart-with-options");return(0,n.dispatch)("core/block-editor").replaceBlock(o.clientId,r),!0})(t);e&&function(t,e){if(S("recordevent %s %o","wcadmin_"+t,e,{_tqk:window._tkq,shouldRecord:!!window._tkq&&!!window.wcTracks&&!!window.wcTracks.isEnabled}),!window.wcTracks||"function"!=typeof window.wcTracks.recordEvent)return!1;window.wcTracks.recordEvent(t,e)}("blocks_add_to_cart_with_options_migration",{transform_to:"blockified"})},children:e})};((t,e)=>{if(!(t=>"string"==typeof t?.name)(t))return void console.error("registerProductBlockType: Block name is required for registration");const o={metadata:t,isVariationBlock:e?.isVariationBlock??!1,variationName:e?.variationName??void 0,isAvailableOnPostEditor:e?.isAvailableOnPostEditor??!1};c.getInstance().registerBlockConfig(o)})({...d,edit:t=>{const{setAttributes:e}=t,o=t.attributes.quantitySelectorStyle===f.Input?"wc-block-add-to-cart-form--input":"wc-block-add-to-cart-form--stepper",i=(0,u.useBlockProps)({className:`wc-block-add-to-cart-form ${o}`}),s=(()=>{const t=(0,n.select)(r),e=t?.getCurrentPostType?.();return"wp_template"===e||"wp_template_part"===e})(),c=(0,k.getSetting)("isBlockTheme",!1),a=c?"":"wp-block-button",l=c?"":"wp-block-button__link wc-block-components-button";return(0,h.jsxs)(h.Fragment,{children:[c&&(0,h.jsx)(u.InspectorControls,{children:(0,h.jsx)(T,{blockClientId:t.clientId})}),(0,h.jsx)(_,{quantitySelectorStyle:t.attributes.quantitySelectorStyle,setAttributes:e}),(0,h.jsx)("div",{...i,children:(0,h.jsx)(g.Tooltip,{text:(0,p.__)("Customer will see product add-to-cart options in this space, dependent on the product type.","woocommerce"),position:"bottom right",children:(0,h.jsxs)("div",{className:"wc-block-editor-add-to-cart-form-container",children:[(0,h.jsx)(w,{isStatic:!0}),(0,h.jsxs)(g.Disabled,{children:[t.attributes.quantitySelectorStyle===f.Input&&(0,h.jsxs)(h.Fragment,{children:[(0,h.jsx)("div",{className:"quantity",children:(0,h.jsx)("input",{style:s?{}:{backgroundColor:"#ffffff",lineHeight:"normal",minHeight:"unset",boxSizing:"unset",borderRadius:"unset"},type:"number",value:"1",className:"input-text qty text",readOnly:!0})}),(0,h.jsx)("div",{className:a,children:(0,h.jsx)("button",{className:`single_add_to_cart_button alt wp-element-button ${l}`,children:(0,p.__)("Add to cart","woocommerce")})})]}),t.attributes.quantitySelectorStyle===f.Stepper&&(0,h.jsxs)(h.Fragment,{children:[(0,h.jsxs)("div",{className:"quantity wc-block-components-quantity-selector",children:[(0,h.jsx)("button",{className:"wc-block-components-quantity-selector__button wc-block-components-quantity-selector__button--minus",children:"−"}),(0,h.jsx)("input",{style:s?{}:{backgroundColor:"#ffffff",lineHeight:"normal",minHeight:"unset",boxSizing:"unset",borderRadius:"unset"},type:"number",value:"1",className:"input-text qty text",readOnly:!0}),(0,h.jsx)("button",{className:"wc-block-components-quantity-selector__button wc-block-components-quantity-selector__button--plus",children:"+"})]}),(0,h.jsx)("div",{className:a,children:(0,h.jsx)("button",{className:`single_add_to_cart_button alt wp-element-button ${l}`,children:(0,p.__)("Add to cart","woocommerce")})})]})]})]})})})]})},icon:{src:(0,h.jsx)(a.A,{icon:l.A,className:"wc-block-editor-components-block-icon"})},ancestor:["woocommerce/single-product"],transforms:{to:[{type:"block",blocks:["woocommerce/add-to-cart-with-options"],transform:()=>(0,i.createBlock)("woocommerce/add-to-cart-with-options")}]},save:()=>null},{isAvailableOnPostEditor:!0})},42405:()=>{},49969:()=>{},10790:t=>{"use strict";t.exports=window.ReactJSXRuntime},86087:t=>{"use strict";t.exports=window.wp.element},27723:t=>{"use strict";t.exports=window.wp.i18n},5573:t=>{"use strict";t.exports=window.wp.primitives}},n={};function r(t){var e=n[t];if(void 0!==e)return e.exports;var o=n[t]={exports:{}};return i[t](o,o.exports,r),o.exports}r.m=i,t=[],r.O=(e,o,i,n)=>{if(!o){var s=1/0;for(d=0;d<t.length;d++){for(var[o,i,n]=t[d],c=!0,a=0;a<o.length;a++)(!1&n||s>=n)&&Object.keys(r.O).every(t=>r.O[t](o[a]))?o.splice(a--,1):(c=!1,n<s&&(s=n));if(c){t.splice(d--,1);var l=i();void 0!==l&&(e=l)}}return e}n=n||0;for(var d=t.length;d>0&&t[d-1][2]>n;d--)t[d]=t[d-1];t[d]=[o,i,n]},r.n=t=>{var e=t&&t.__esModule?()=>t.default:()=>t;return r.d(e,{a:e}),e},o=Object.getPrototypeOf?t=>Object.getPrototypeOf(t):t=>t.__proto__,r.t=function(t,i){if(1&i&&(t=this(t)),8&i)return t;if("object"==typeof t&&t){if(4&i&&t.__esModule)return t;if(16&i&&"function"==typeof t.then)return t}var n=Object.create(null);r.r(n);var s={};e=e||[null,o({}),o([]),o(o)];for(var c=2&i&&t;"object"==typeof c&&!~e.indexOf(c);c=o(c))Object.getOwnPropertyNames(c).forEach(e=>s[e]=()=>t[e]);return s.default=()=>t,r.d(n,s),n},r.d=(t,e)=>{for(var o in e)r.o(e,o)&&!r.o(t,o)&&Object.defineProperty(t,o,{enumerable:!0,get:e[o]})},r.g=function(){if("object"==typeof globalThis)return globalThis;try{return this||new Function("return this")()}catch(t){if("object"==typeof window)return window}}(),r.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e),r.r=t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},r.j=2100,(()=>{var t={2100:0};r.O.j=e=>0===t[e];var e=(e,o)=>{var i,n,[s,c,a]=o,l=0;if(s.some(e=>0!==t[e])){for(i in c)r.o(c,i)&&(r.m[i]=c[i]);if(a)var d=a(r)}for(e&&e(o);l<s.length;l++)n=s[l],r.o(t,n)&&t[n]&&t[n][0](),t[n]=0;return r.O(d)},o=globalThis.webpackChunkwebpackWcBlocksMainJsonp=globalThis.webpackChunkwebpackWcBlocksMainJsonp||[];o.forEach(e.bind(null,0)),o.push=e.bind(null,o.push.bind(o))})();var s=r.O(void 0,[94],()=>r(772));s=r.O(s),((this.wc=this.wc||{}).blocks=this.wc.blocks||{})["add-to-cart-form"]=s})();
Save File
Cancel