JezK
Edit File: add-to-cart-with-options.js
import*as t from"@woocommerce/stores/store-notices";import*as e from"@woocommerce/stores/woocommerce/cart";import*as o from"@wordpress/interactivity";import*as r from"@woocommerce/stores/woocommerce/products";var i={1167:e=>{e.exports=t},1401:t=>{t.exports=e}},n={};function s(t){var e=n[t];if(void 0!==e)return e.exports;var o=n[t]={exports:{}};return i[t](o,o.exports,s),o.exports}s.d=(t,e)=>{for(var o in e)s.o(e,o)&&!s.o(t,o)&&Object.defineProperty(t,o,{enumerable:!0,get:e[o]})},s.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e);const a=(c={getConfig:()=>o.getConfig,getContext:()=>o.getContext,store:()=>o.store,withSyncEvent:()=>o.withSyncEvent},d={},s.d(d,c),d);var c,d;s.d({},{});const u="I acknowledge that using a private store means my plugin will inevitably break on the next store release.",{state:l}=(0,a.store)("woocommerce/products",{},{lock:u}),{state:m}=(0,a.store)("woocommerce/add-to-cart-with-options",{},{lock:u}),{actions:p}=(0,a.store)("woocommerce/add-to-cart-with-options",{state:{noticeIds:[],get validationErrors(){const t=(0,a.getContext)();return t&&t.validationErrors?t.validationErrors:[]},get isFormValid(){return 0===m.validationErrors.length},get allowsAddingToCart(){const t=l.productInContext;return!!t&&("grouped"===t.type||t.is_purchasable&&t.is_in_stock)},get quantity(){return(0,a.getContext)().quantity},get selectedAttributes(){return(0,a.getContext)().selectedAttributes||[]}},actions:{validateQuantity(t,e){if(p.clearErrors("invalid-quantities"),"number"!=typeof e)return;const o=l.productInContext;if(0===e||o&&(e<o.add_to_cart.minimum||e>o.add_to_cart.maximum)){const{errorMessages:t}=(0,a.getConfig)();p.addError({code:"invalidQuantities",message:t?.invalidQuantities||"",group:"invalid-quantities"})}},setQuantity(t,e){const o=(0,a.getContext)(),r=(0,a.getContext)("woocommerce/add-to-cart-with-options-quantity-selector"),i=r?.inputElement,n=Number.isNaN(i?.valueAsNumber),{mainProductInContext:s}=l,c=s?.variations?.map(t=>t.id)??[];c.length>0?[t,...c].forEach(t=>{n&&(o.quantity[Number(t)]=NaN),o.quantity[Number(t)]=e}):(n&&(o.quantity={...o.quantity,[t]:NaN}),o.quantity={...o.quantity,[t]:e});const d=l.findProduct({id:l.productId,selectedAttributes:o.selectedAttributes});"grouped"===d?.type?p.validateGroupedProductQuantity():p.validateQuantity(t,e),i&&(t=>{const e=new Event("change",{bubbles:!0});t.dispatchEvent(e)})(i)},addError:t=>{const{validationErrors:e}=m;return e.push(t),t.code},clearErrors:t=>{const{validationErrors:e}=m;if(t){const o=e.filter(e=>e.group!==t);e.splice(0,e.length,...o)}else e.length=0},addToCart:(0,a.withSyncEvent)(function*(t){t.preventDefault();const{isFormValid:e}=m;if(!e){yield Promise.resolve().then(s.bind(s,1167));const{actions:t}=(0,a.store)("woocommerce/store-notices",{},{lock:u}),{noticeIds:e,validationErrors:o}=m;e.forEach(e=>{t.removeNotice(e)}),e.splice(0,e.length);const r=o.map(e=>t.addNotice({notice:e.message,type:"error",dismissible:!0}));return void e.push(...r)}yield Promise.resolve().then(s.bind(s,1401));const o=l.productInContext;if(!o)return;if("grouped"===o.type)return void(yield p.batchAddToCart());const{quantity:r,selectedAttributes:i}=(0,a.getContext)(),{actions:n}=(0,a.store)("woocommerce",{},{lock:u});yield n.addCartItem({id:o.id,quantityToAdd:r[o.id],variation:i,type:o.type},{showCartUpdatesNotices:!1})})}},{lock:u});