JezK
Edit File: add-to-cart-with-options-variation-selector.js
import*as t from"@woocommerce/stores/woocommerce/cart";import*as e from"@woocommerce/stores/woocommerce/product-data";import*as o from"@woocommerce/stores/woocommerce/products";import*as r from"@wordpress/interactivity";var i={409:(t,e,o)=>{o.d(e,{QC:()=>a,TV:()=>n,go:()=>i,hf:()=>r});const r=t=>t.replace(/^attribute_(pa_)?/,"").replace(/-/g," ").toLowerCase(),i=(t,e)=>r(t)===r(e),n=(t,e)=>{const o=t.attributes.find((t=>i(e,t.name)));return o?.value},a=(t,e)=>t.variations?.length&&e?.length?t.variations.find((t=>t.attributes.every((t=>{const o=e.find((e=>i(t.name,e.attribute)));return null===t.value?void 0!==o&&null!==o.value:o?.value===t.value}))))??null:null},446:(t,e,o)=>{o.d(e,{E:()=>c});var r=o(2833),i=(o(4021),o(99),o(409));const n="I acknowledge that using a private store means my plugin will inevitably break on the next store release.",{state:a}=(0,r.store)("woocommerce/product-data",{},{lock:n}),{state:s}=(0,r.store)("woocommerce/products",{},{lock:n}),c=(t,e)=>{const o=s.products[t];if(!o)return null;let r=o;if("variable"===o.type&&e?.length>0){const t=(0,i.QC)(o,e);if(t){const e=s.productVariations[t.id];if(!e)return null;r=e}}const{add_to_cart:n}=r,a=n?.maximum??0;return{id:r.id,type:r.type,is_in_stock:r.is_purchasable&&r.is_in_stock,sold_individually:r.sold_individually,min:n?.minimum??1,max:a>0?a:Number.MAX_SAFE_INTEGER,step:n?.multiple_of??1}},{state:u}=(0,r.store)("woocommerce/add-to-cart-with-options",{},{lock:n}),{actions:l}=(0,r.store)("woocommerce/add-to-cart-with-options",{state:{noticeIds:[],get validationErrors(){const t=(0,r.getContext)();return t&&t.validationErrors?t.validationErrors:[]},get isFormValid(){return 0===u.validationErrors.length},get allowsAddingToCart(){const{productData:t}=u;return"grouped"===t?.type||(t?.is_in_stock??!0)},get quantity(){return(0,r.getContext)().quantity},get selectedAttributes(){return(0,r.getContext)().selectedAttributes||[]},get productData(){const{selectedAttributes:t}=(0,r.getContext)();return c(a.productId,t)}},actions:{validateQuantity(t,e){if(l.clearErrors("invalid-quantities"),"number"!=typeof e)return;const{selectedAttributes:o}=(0,r.getContext)(),i=c(t,o);if(0===e||i&&(e<i.min||e>i.max)){const{errorMessages:t}=(0,r.getConfig)();l.addError({code:"invalidQuantities",message:t?.invalidQuantities||"",group:"invalid-quantities"})}},setQuantity(t,e){const o=(0,r.getContext)(),i=(0,r.getContext)("woocommerce/add-to-cart-with-options-quantity-selector"),n=i?.inputElement,a=Number.isNaN(n?.valueAsNumber),c=s.products[t],d=c?.variations?.map((t=>t.id))??[];d.length>0?[t,...d].forEach((t=>{a&&(o.quantity[Number(t)]=null),o.quantity[Number(t)]=e})):(a&&(o.quantity={...o.quantity,[t]:null}),o.quantity={...o.quantity,[t]:e}),"grouped"===u.productData?.type?l.validateGroupedProductQuantity():l.validateQuantity(t,e),n&&(t=>{const e=new Event("change",{bubbles:!0});t.dispatchEvent(e)})(n)},addError:t=>{const{validationErrors:e}=u;return e.push(t),t.code},clearErrors:t=>{const{validationErrors:e}=u;if(t){const o=e.filter((e=>e.group!==t));e.splice(0,e.length,...o)}else e.length=0},addToCart:(0,r.withSyncEvent)((function*(t){t.preventDefault();const{isFormValid:e}=u;if(!e){yield Promise.resolve().then(o.bind(o,7908));const{actions:t}=(0,r.store)("woocommerce/store-notices",{},{lock:n}),{noticeIds:e,validationErrors:i}=u;e.forEach((e=>{t.removeNotice(e)})),e.splice(0,e.length);const a=i.map((e=>t.addNotice({notice:e.message,type:"error",dismissible:!0})));return void e.push(...a)}yield Promise.resolve().then(o.bind(o,1401));const{selectedAttributes:i}=(0,r.getContext)(),s=a.variationId||a.productId,d=a.variationId?"variation":c(s,i)?.type;if(!d)return;if("grouped"===d)return void(yield l.batchAddToCart());const{quantity:p}=(0,r.getContext)(),{actions:m}=(0,r.store)("woocommerce",{},{lock:n});yield m.addCartItem({id:s,quantityToAdd:p[s],variation:i,type:d},{showCartUpdatesNotices:!1})}))}},{lock:n})},7908:t=>{t.exports=import("@woocommerce/stores/store-notices")},1401:e=>{e.exports=t},4021:(t,e,o)=>{var r;t.exports=(r={},o.d(r,{}),r)},99:(t,e,o)=>{var r;t.exports=(r={},o.d(r,{}),r)},2833:(t,e,o)=>{var i,n;t.exports=(i={getConfig:()=>r.getConfig,getContext:()=>r.getContext,getElement:()=>r.getElement,store:()=>r.store,withSyncEvent:()=>r.withSyncEvent},n={},o.d(n,i),n)}},n={};function a(t){var e=n[t];if(void 0!==e)return e.exports;var o=n[t]={exports:{}};return i[t](o,o.exports,a),o.exports}a.d=(t,e)=>{for(var o in e)a.o(e,o)&&!a.o(t,o)&&Object.defineProperty(t,o,{enumerable:!0,get:e[o]})},a.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e);var s=a(2833),c=(a(99),a(446)),u=a(409);function l(t,e){if(!t)return null;const o=window.getComputedStyle(t)[e];if("rgba(0, 0, 0, 0)"!==o&&"transparent"!==o){const t=o.match(/\d+/g);if(!t||t.length<3)return null;const[e,r,i]=t.slice(0,3);return`rgb(${e}, ${r}, ${i})`}return l(t.parentElement,e)}!function(){const t=document.querySelector(".wc-block-add-to-cart-with-options-variation-selector-attribute-options__pills");if(!t)return;const e=document.createElement("style"),o=l(t,"backgroundColor")||"#fff",r=l(t,"color")||"#000";e.appendChild(document.createTextNode(`:where(.wc-block-add-to-cart-with-options-variation-selector-attribute-options__pill:has(.wc-block-add-to-cart-with-options-variation-selector-attribute-options__pill-input:checked)) {\n\t\t\t\t--pill-color: ${o};\n\t\t\t\t--pill-background-color: ${r};\n\t\t\t}`)),document.head.appendChild(e)}();const d="I acknowledge that using a private store means my plugin will inevitably break on the next store release.",{state:p}=(0,s.store)("woocommerce/product-data",{},{lock:d}),{state:m}=(0,s.store)("woocommerce/products",{},{lock:d}),g=({attributeName:t,attributeValue:e,selectedAttributes:o})=>{if(!t||!e||!Array.isArray(o))return!1;const r=o.some((e=>(0,u.go)(e.attribute,t)))?o.length-1:o.length,i=m.products[p.productId];return!!i?.variations?.length&&i.variations.some((i=>{const n=(0,u.TV)(i,t);return(n===e||null===n)&&o.filter((o=>{const r=(0,u.TV)(i,o.attribute);return r===o.value||!(null!==r||(0,u.go)(o.attribute,t)&&e!==o.value)})).length>=r}))},{actions:b,state:v}=(0,s.store)("woocommerce/add-to-cart-with-options",{state:{get selectedAttributes(){const t=(0,s.getContext)();return t?t.selectedAttributes:[]},get isOptionSelected(){const{selectedAttributes:t,option:e,name:o}=(0,s.getContext)();return t.some((t=>(0,u.go)(t.attribute,o)&&t.value===e.value))},get isOptionDisabled(){const{name:t,option:e,selectedAttributes:o}=(0,s.getContext)();return""!==e.value&&!g({attributeName:t,attributeValue:e.value,selectedAttributes:o})}},actions:{setAttribute(t,e){const{selectedAttributes:o}=(0,s.getContext)(),r=o.findIndex((e=>(0,u.go)(e.attribute,t)));""!==e?r>=0?o[r]={attribute:t,value:e}:o.push({attribute:t,value:e}):r>=0&&o.splice(r,1)},removeAttribute(t){const{selectedAttributes:e}=(0,s.getContext)(),o=e.findIndex((e=>(0,u.go)(e.attribute,t)));o>=0&&e.splice(o,1)},handlePillClick(){const t=(0,s.getContext)();v.isOptionSelected?t.selectedValue="":t.selectedValue=t.option.value,b.setAttribute(t.name,t.selectedValue),""!==t.selectedValue&&b.autoselectAttributes({excludedAttributes:[t.name]})},handleDropdownChange(t){const e=(0,s.getContext)();e.selectedValue=t.currentTarget.value,b.setAttribute(e.name,e.selectedValue),""!==e.selectedValue&&b.autoselectAttributes({excludedAttributes:[e.name]})},autoselectAttributes({includedAttributes:t=[],excludedAttributes:e=[]}={}){const{autoselect:o,selectedAttributes:r}=(0,s.getContext)();if(!o)return;const i=m.products[p.productId];if(!i)return;const n=t.map((t=>(0,u.hf)(t))),a=e.map((t=>(0,u.hf)(t))),c=(t=>{if(!t?.variations?.length)return{};const e={};return t.variations.forEach((t=>{t.attributes.forEach((t=>{Array.isArray(e[t.name])||(e[t.name]=[]),t.value&&!e[t.name].includes(t.value)&&e[t.name].push(t.value)}))})),e})(i);Object.entries(c).forEach((([e,o])=>{const i=(0,u.hf)(e);if(0!==n.length&&!n.includes(i))return;if(0!==a.length&&a.includes(i))return;const s=o.filter((t=>g({attributeName:e,attributeValue:t,selectedAttributes:r})));if(1===s.length){const o=s[0],r=t.find((t=>(0,u.hf)(t)===i))||e;b.setAttribute(r,o)}}))}},callbacks:{setDefaultSelectedAttribute(){const t=(0,s.getContext)();t.selectedValue&&b.setAttribute(t.name,t.selectedValue),b.autoselectAttributes({includedAttributes:[t.name]})},setSelectedVariationId:()=>{const t=m.products[p.productId];if(!t?.variations?.length)return;const{selectedAttributes:e}=(0,s.getContext)(),o=(0,u.QC)(t,e),{actions:r}=(0,s.store)("woocommerce/product-data",{},{lock:d});r.setVariationId(o?.id??null)},validateVariation(){b.clearErrors("variable-product");const t=m.products[p.productId];if(!t?.variations?.length)return;const{selectedAttributes:e}=(0,s.getContext)(),o=(0,u.QC)(t,e),{errorMessages:r}=(0,s.getConfig)();if(!o?.id)return void b.addError({code:"variableProductMissingAttributes",message:r?.variableProductMissingAttributes||"",group:"variable-product"});const i=m.productVariations[o.id];i&&(i.is_in_stock||b.addError({code:"variableProductOutOfStock",message:r?.variableProductOutOfStock||"",group:"variable-product"}))},watchQuantityConstraints(){const{ref:t}=(0,s.getElement)();if(!(t instanceof HTMLInputElement))return;if(t===document.activeElement)return;const{selectedAttributes:e}=(0,s.getContext)(),o=(0,c.E)(p.productId,e);if(o){const{quantity:e}=(0,s.getContext)(),r=e[o.id],{min:i,max:n}=o;let a=r;r<i?a=i:r>n&&(a=n),a===t.valueAsNumber&&a===r||b.setQuantity(p.productId,a)}}}},{lock:d});