← Back
Editing: add-to-cart-with-options.js
(()=>{var e,t,o,s={35338:(e,t,o)=>{"use strict";var s=o(11003);const i=window.wp.plugins,r=window.wp.blocks,n=window.wp.data,c="core/editor",a=window.wc.wcTypes;class l{blocks=new Map;initialized=!1;attemptedRegisteredBlocks=new Set;constructor(){this.initializeSubscriptions()}static getInstance(){return l.instance||(l.instance=new l),l.instance}initializeSubscriptions(){if(this.initialized)return;const e=(0,n.subscribe)(()=>{const t=(0,n.select)(c);if(!t)return;const o=t.getCurrentPostType();if(o)if("post"===o||"page"===o)e(),this.blocks.forEach(e=>{if(e.isAvailableOnPostEditor){const t=e.variationName||e.metadata.name;this.hasAttemptedRegistration(t)||this.registerBlock(e)}}),this.initialized=!0;else{e();const o=String(t.getEditedPostSlug());this.currentTemplateId=o,this.handleTemplateChange(void 0),(0,n.subscribe)(()=>{const e=this.currentTemplateId;this.currentTemplateId=String(t.getEditedPostSlug()),e!==this.currentTemplateId&&this.handleTemplateChange(e)},c),this.initialized=!0}})}handleTemplateChange(e){(this.currentTemplateId?.includes("single-product")||e?.includes("single-product"))&&this.blocks.forEach(e=>{this.unregisterBlock(e),this.registerBlock(e)})}hasAttemptedRegistration(e){return this.attemptedRegisteredBlocks.has(e)}unregisterBlock(e){const{metadata:t,isVariationBlock:o,variationName:s}=e;try{o&&s?((0,r.unregisterBlockVariation)(t.name,s),this.attemptedRegisteredBlocks.delete(s)):((0,r.unregisterBlockType)(t.name),this.attemptedRegisteredBlocks.delete(t.name))}catch(e){console.debug(`Failed to unregister block ${t.name}:`,e)}}registerBlock(e){const{metadata:t,isVariationBlock:o,variationName:s,isAvailableOnPostEditor:i}=e;try{const c=s||t.name;if(this.hasAttemptedRegistration(c))return;const l=(0,n.select)("core/edit-site");if(!l&&!i)return;if(o)(0,r.registerBlockVariation)(t.name,{...t,name:s||t.name});else{const o=(0,a.isEmpty)(t?.ancestor)?["woocommerce/single-product"]:t?.ancestor,s=l&&this.currentTemplateId?.includes("single-product"),{title:i,description:n,keywords:d,styles:p,variations:u,...m}=t,w=(0,r.registerBlockType)(t,{...m,ancestor:s?void 0:o});this.blocks.set(c,{...e,metadata:{...t,title:w?.title??t.title,description:w?.description??t.description}})}this.attemptedRegisteredBlocks.add(c)}catch(e){console.error(`Failed to register block ${t.name}:`,e)}}registerBlockConfig(e){const t=e.variationName||e.metadata.name;this.blocks.set(t,e),this.registerBlock(e)}}var d=o(27723);const p=window.wp.components;var u=o(51512),m=o.n(u);m()("wc-admin:tracks:stats");const w=m()("wc-admin:tracks");function h(e,t){if(w("recordevent %s %o","wcadmin_"+e,t,{_tqk:window._tkq,shouldRecord:!!window._tkq&&!!window.wcTracks&&!!window.wcTracks.isEnabled}),!window.wcTracks||"function"!=typeof window.wcTracks.recordEvent)return!1;window.wcTracks.recordEvent(e,t)}const g=window.wp.editor,b="woocommerce/product-type-template-state",k="SWITCH_PRODUCT_TYPE",y="SET_PRODUCT_TYPES",f="REGISTER_LISTENER",_="UNREGISTER_LISTENER",T=window.wc.wcSettings,x=(0,T.getSetting)("productTypes",{}),v=Object.keys(x).map(e=>({slug:e,label:x[e]})),j={productTypes:{list:v,current:v[0]?.slug},listeners:[]},S={switchProductType:e=>({type:k,current:e}),setProductTypes:e=>({type:y,productTypes:e}),registerListener:e=>({type:f,listener:e}),unregisterListener:e=>({type:_,listener:e})},P=(0,n.createReduxStore)(b,{reducer:(e=j,t)=>{switch(t.type){case y:return{...e,productTypes:{...e.productTypes,list:t.productTypes||[]}};case k:return{...e,productTypes:{...e.productTypes,current:t.current}};case f:return{...e,listeners:[...e.listeners,t.listener||""]};case _:return{...e,listeners:e.listeners.filter(e=>e!==t.listener)};default:return e}},actions:S,selectors:{getProductTypes:e=>e.productTypes.list,getCurrentProductType:e=>e.productTypes.list.find(t=>t.slug===e.productTypes.current),getRegisteredListeners:e=>e.listeners}});function C(){const{productTypes:e,current:t,registeredListeners:o}=(0,n.useSelect)(e=>{const{getProductTypes:t,getCurrentProductType:o,getRegisteredListeners:s}=e(P);return{productTypes:t(),current:o(),registeredListeners:s()}},[]),{switchProductType:s,registerListener:i,unregisterListener:r}=(0,n.useDispatch)(P);return{productTypes:e,current:t,set:s,registeredListeners:o,registerListener:i,unregisterListener:r}}(0,n.select)(b)||(0,n.register)(P);var B=o(10790);function O(){const{productTypes:e,current:t,set:o}=C();return(0,B.jsx)(p.SelectControl,{label:(0,d.__)("Type switcher","woocommerce"),value:t?.slug,options:e.map(e=>({label:e.label,value:e.slug})),onChange:e=>{o(e),h("blocks_add_to_cart_with_options_product_type_switched",{context:"inspector",from:t?.slug,to:e})},help:(0,d.__)("Switch product type to see how the template adapts to each one.","woocommerce")})}const E=JSON.parse('{"name":"woocommerce/add-to-cart-with-options","title":"Add to Cart + Options (Beta)","description":"Use blocks to create an \\"Add to cart\\" area that\'s customized for different product types, such as variable and grouped. ","category":"woocommerce-product-elements","attributes":{"isDescendantOfAddToCartWithOptions":{"type":"boolean","default":true}},"usesContext":["postId"],"providesContext":{"woocommerce/isDescendantOfAddToCartWithOptions":"isDescendantOfAddToCartWithOptions"},"textdomain":"woocommerce","supports":{"interactivity":true},"apiVersion":3,"$schema":"https://schemas.wp.org/trunk/block.json","style":"file:../woocommerce/add-to-cart-with-options-style.css","editorStyle":"file:../woocommerce/add-to-cart-with-options-editor.css"}');var A=o(86087);const I=window.wp.blockEditor,L=window.wc.wcEntities;var N=o(5573);const R=(0,B.jsxs)(N.SVG,{xmlns:"http://www.w3.org/2000/svg",viewBox:"0 0 24 24",children:[(0,B.jsx)("path",{fill:"none",d:"M0 0h24v24H0V0z"}),(0,B.jsx)("path",{d:"M12 6a9.77 9.77 0 0 1 8.82 5.5C19.17 14.87 15.79 17 12 17s-7.17-2.13-8.82-5.5A9.77 9.77 0 0 1 12 6m0-2C7 4 2.73 7.11 1 11.5 2.73 15.89 7 19 12 19s9.27-3.11 11-7.5C21.27 7.11 17 4 12 4zm0 5a2.5 2.5 0 0 1 0 5 2.5 2.5 0 0 1 0-5m0-2c-2.48 0-4.5 2.02-4.5 4.5S9.52 16 12 16s4.5-2.02 4.5-4.5S14.48 7 12 7z"})]}),D=window.wc.wcBlocksSharedContext;function W(){const{current:e,productTypes:t,set:o}=C(),{product:s}=(0,D.useProductDataContext)();return s?.id||t?.length<2?null:(0,B.jsx)(p.ToolbarGroup,{children:(0,B.jsx)(p.ToolbarDropdownMenu,{icon:(0,B.jsx)(p.Icon,{icon:R}),text:e?.label||(0,d.__)("Switch product type","woocommerce"),label:(0,d.__)("Switch product type","woocommerce"),value:e?.slug,controls:t.map(t=>({title:t.label,onClick:()=>{o(t.slug),e?.slug!==t.slug&&h("blocks_add_to_cart_with_options_product_type_switched",{context:"toolbar",from:e?.slug,to:t.slug})}}))})})}var z=o(4921);function M({children:e,className:t,actionLabel:o,onActionClick:s,status:i="info",...r}){return(0,B.jsx)(p.Notice,{...r,status:i,politeness:"polite",className:(0,z.$)("wc-block-editor-components-upgrade-downgrade-notice",t),actions:[{label:o,onClick:s,noDefaultClasses:!0,variant:"link"}],children:(0,B.jsx)("div",{className:"wc-block-editor-components-upgrade-downgrade-notice__text",children:e})})}o(49969);const V=({blocks:e,findCondition:t})=>{for(const o of e){if(t(o))return o;if(o.innerBlocks){const e=V({blocks:o.innerBlocks,findCondition:t});if(e)return e}}},F=({blockClientId:e})=>{const t=(0,d.__)("Facing compatibility issues with extensions? You can switch back to the classic Add to Cart with Options block.","woocommerce"),o=(0,d.__)("Switch back","woocommerce");return(0,B.jsx)(M,{isDismissible:!1,actionLabel:o,onActionClick:async()=>{const t=await(e=>{const t=(0,n.select)("core/block-editor").getBlocks(),o=V({blocks:t,findCondition:t=>t.name===E.name&&t.clientId===e});if(!o)return!1;const s=(0,r.createBlock)("woocommerce/add-to-cart-form",{quantitySelectorStyle:"input"});return(0,n.dispatch)("core/block-editor").replaceBlock(o.clientId,s),!0})(e);t&&h("blocks_add_to_cart_with_options_migration",{transform_to:"legacy"})},children:t})},G=JSON.parse('{"name":"woocommerce/product-image-gallery"}'),U=()=>{const[e,t]=(0,A.useState)(null);if((0,A.useEffect)(()=>{const e=V({blocks:(0,n.select)("core/block-editor").getBlocks(),findCondition:e=>"woocommerce/product-image-gallery"===e.name});t(e||null)},[t]),!e)return null;const o=(0,d.__)("This template contains the classic Product Image Gallery block which is not compatible with the Add to Cart + Options block. Switch to the new Product Gallery block for a better experience.","woocommerce"),s=(0,d.__)("Upgrade to the Product Gallery block","woocommerce");return(0,B.jsx)(M,{isDismissible:!1,actionLabel:s,onActionClick:()=>{(e=>{const t=(0,n.select)("core/block-editor").getBlocks();if(V({blocks:t,findCondition:t=>t.name===G.name&&t.clientId===e})){const t=(0,r.createBlock)("woocommerce/product-gallery");return(0,n.dispatch)("core/block-editor").replaceBlock(e,t),!0}return!1})(e.clientId)&&t(null)},status:"warning",children:o})},q=window.wp.coreData;var J=o(11789),$=o(98203);o(42405);const Y=({tag:e="div",width:t="100%",height:o="8px",maxWidth:s="",className:i="",borderRadius:r="",isStatic:n=!1,ariaMessage:c})=>(0,B.jsx)(e,{className:(0,z.A)("wc-block-components-skeleton__element",{"wc-block-components-skeleton__element--static":n},i),...c?{"aria-live":"polite","aria-label":c}:{"aria-hidden":"true"},style:{width:t,height:o,borderRadius:r,maxWidth:s}}),H=({isStatic:e=!1})=>(0,B.jsxs)("div",{className:"wc-block-components-skeleton",children:[(0,B.jsx)(Y,{height:"16px",isStatic:e}),(0,B.jsx)(Y,{height:"16px",isStatic:e}),(0,B.jsx)(Y,{height:"16px",width:"80%",isStatic:e})]}),X=({buttonText:e,productType:t,isLoading:o=!1})=>(0,B.jsxs)("div",{"aria-label":o?(0,d.__)("Loading the Add to Cart + Options template part","woocommerce"):(0,d.__)("Add to Cart + Options form","woocommerce"),children:[(0,B.jsx)("div",{className:"wp-block-woocommerce-add-to-cart-with-options__skeleton-wrapper",children:(0,B.jsx)(H,{isStatic:!o})}),(0,B.jsx)(p.Disabled,{children:(0,B.jsx)("button",{className:`alt wp-element-button ${t||"simple"}_add_to_cart_button`,children:e||(0,d.__)("Add to cart","woocommerce")})})]}),K=()=>(0,B.jsx)("div",{className:"wc-block-add-to-wishlist-button",children:(0,B.jsxs)("button",{type:"button",className:"wc-block-add-to-wishlist-button__toggle",disabled:!0,children:[(0,B.jsx)("span",{className:"wc-block-add-to-wishlist-button__icon wc-block-add-to-wishlist-button__icon--empty",children:(0,B.jsx)(J.A,{icon:$.A,size:24})}),(0,B.jsx)("span",{className:"wc-block-add-to-wishlist-button__label",children:(0,d.__)("Add to wishlist","woocommerce")})]})}),Q=({blockProps:e,productType:t,templatePartId:o,showAddToWishlist:s})=>{const[i,r,c]=(0,q.useEntityBlockEditor)("postType","wp_template_part",{id:o}),{isLoading:a}=(0,n.useSelect)(e=>{const{hasFinishedResolution:t}=e(q.store);return{isLoading:!t("getEditedEntityRecord",["postType","wp_template_part",o])}},[o]),l=(0,I.useInnerBlocksProps)(e,{value:i,onInput:r,onChange:c,renderAppender:a||0!==i.length?null:I.InnerBlocks.ButtonBlockAppender});if(a)return(0,B.jsx)("div",{...e,children:(0,B.jsx)(X,{productType:t,isLoading:!0})});const{children:d,...p}=l;return(0,B.jsxs)("div",{...p,children:[d,s&&(0,B.jsx)(K,{})]})},Z=({productType:e,showAddToWishlist:t})=>{const o=(0,T.getSetting)("addToCartWithOptionsTemplatePartIds",{}),s=o?.[e],i=(0,I.useBlockProps)({className:"wc-block-add-to-cart-with-options"}),{canEditTemplatePart:r,isLoading:c}=(0,n.useSelect)(e=>{if(!s)return{canEditTemplatePart:!1,isLoading:!1};const{canUser:t,hasFinishedResolution:o}=e(q.store);return{canEditTemplatePart:t("update",{kind:"postType",name:"wp_template_part",id:s}),isLoading:!o("canUser",["update",{kind:"postType",name:"wp_template_part",id:s}])}},[s]);return s&&r?(0,B.jsx)(Q,{blockProps:i,productType:e,templatePartId:s,showAddToWishlist:t}):(0,B.jsxs)("div",{...i,children:[(0,B.jsx)(X,{productType:e,isLoading:c}),t&&(0,B.jsx)(K,{})]})};o(79959);const ee="document-settings-template-selector-pane";(0,i.getPlugin)(ee)||(0,i.registerPlugin)(ee,{render:function(){const{slug:e,type:t}=(0,n.useSelect)(e=>{const t=e("core/editor");if(!t)return{slug:"",type:""};const{slug:o,type:s}=t.getCurrentPost();return{slug:o,type:s}},[]),{registeredListeners:o}=C();return"wp_template"===t&&"single-product"===e&&o.length>0?(0,B.jsx)(g.PluginDocumentSettingPanel,{name:"woocommerce/product-type-selector",title:(0,d.__)("Product Type","woocommerce"),children:(0,B.jsx)(O,{})}):null}}),((e,t)=>{if(!(e=>"string"==typeof e?.name)(e))return void console.error("registerProductBlockType: Block name is required for registration");const o={metadata:e,isVariationBlock:t?.isVariationBlock??!1,variationName:t?.variationName??void 0,isAvailableOnPostEditor:t?.isAvailableOnPostEditor??!1};l.getInstance().registerBlockConfig(o)})({...E,icon:{src:({size:e})=>(0,B.jsx)("span",{className:"wp-block-woocommerce-add-to-cart-with-options__block-icon",style:{height:e,width:e},children:s.A})},edit:e=>{const t=(0,T.getSetting)("wishlistFeatureEnabled",!1),{product:o}=(0,L.useProduct)(e.context?.postId),s=(0,I.useBlockProps)({className:"wc-block-add-to-cart-with-options"}),i=s?.id,{current:r,registerListener:n,unregisterListener:c}=C();(0,A.useEffect)(()=>(n(i),()=>{c(i)}),[i,n,c]);const a=void 0===o?.id?r?.slug:o?.type,l=a&&["simple","variable","external","grouped"].includes(a);return(0,B.jsxs)(B.Fragment,{children:[(0,B.jsxs)(I.InspectorControls,{children:[(0,B.jsx)(U,{}),(0,B.jsx)(F,{blockClientId:e?.clientId})]}),(0,B.jsx)(I.BlockControls,{children:(0,B.jsx)(W,{})}),l?(0,B.jsx)(Z,{productType:a,showAddToWishlist:t}):(0,B.jsx)("div",{...s,children:(0,B.jsx)(X,{buttonText:o?.add_to_cart?.single_text,productType:a,isLoading:!1})})]})},save:()=>null,ancestor:["woocommerce/single-product"]},{isAvailableOnPostEditor:!0})},79959:()=>{},42405:()=>{},49969:()=>{},10790:e=>{"use strict";e.exports=window.ReactJSXRuntime},86087:e=>{"use strict";e.exports=window.wp.element},27723:e=>{"use strict";e.exports=window.wp.i18n},5573:e=>{"use strict";e.exports=window.wp.primitives}},i={};function r(e){var t=i[e];if(void 0!==t)return t.exports;var o=i[e]={exports:{}};return s[e](o,o.exports,r),o.exports}r.m=s,e=[],r.O=(t,o,s,i)=>{if(!o){var n=1/0;for(d=0;d<e.length;d++){for(var[o,s,i]=e[d],c=!0,a=0;a<o.length;a++)(!1&i||n>=i)&&Object.keys(r.O).every(e=>r.O[e](o[a]))?o.splice(a--,1):(c=!1,i<n&&(n=i));if(c){e.splice(d--,1);var l=s();void 0!==l&&(t=l)}}return t}i=i||0;for(var d=e.length;d>0&&e[d-1][2]>i;d--)e[d]=e[d-1];e[d]=[o,s,i]},r.n=e=>{var t=e&&e.__esModule?()=>e.default:()=>e;return r.d(t,{a:t}),t},o=Object.getPrototypeOf?e=>Object.getPrototypeOf(e):e=>e.__proto__,r.t=function(e,s){if(1&s&&(e=this(e)),8&s)return e;if("object"==typeof e&&e){if(4&s&&e.__esModule)return e;if(16&s&&"function"==typeof e.then)return e}var i=Object.create(null);r.r(i);var n={};t=t||[null,o({}),o([]),o(o)];for(var c=2&s&&e;"object"==typeof c&&!~t.indexOf(c);c=o(c))Object.getOwnPropertyNames(c).forEach(t=>n[t]=()=>e[t]);return n.default=()=>e,r.d(i,n),i},r.d=(e,t)=>{for(var o in t)r.o(t,o)&&!r.o(e,o)&&Object.defineProperty(e,o,{enumerable:!0,get:t[o]})},r.g=function(){if("object"==typeof globalThis)return globalThis;try{return this||new Function("return this")()}catch(e){if("object"==typeof window)return window}}(),r.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r.j=7435,(()=>{var e={7435:0};r.O.j=t=>0===e[t];var t=(t,o)=>{var s,i,[n,c,a]=o,l=0;if(n.some(t=>0!==e[t])){for(s in c)r.o(c,s)&&(r.m[s]=c[s]);if(a)var d=a(r)}for(t&&t(o);l<n.length;l++)i=n[l],r.o(e,i)&&e[i]&&e[i][0](),e[i]=0;return r.O(d)},o=globalThis.webpackChunkwebpackWcBlocksMainJsonp=globalThis.webpackChunkwebpackWcBlocksMainJsonp||[];o.forEach(t.bind(null,0)),o.push=t.bind(null,o.push.bind(o))})();var n=r.O(void 0,[94],()=>r(35338));n=r.O(n),((this.wc=this.wc||{}).blocks=this.wc.blocks||{})["add-to-cart-with-options"]=n})();
Save File
Cancel