← Back
Editing: product-button.js
import*as t from"@woocommerce/stores/woocommerce/cart";import*as e from"@wordpress/interactivity";import*as o from"@woocommerce/stores/woocommerce/products";var r={1401:e=>{e.exports=t}},a={};function n(t){var e=a[t];if(void 0!==e)return e.exports;var o=a[t]={exports:{}};return r[t](o,o.exports,n),o.exports}n.d=(t,e)=>{for(var o in e)n.o(e,o)&&!n.o(t,o)&&Object.defineProperty(t,o,{enumerable:!0,get:e[o]})},n.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e);const i=(s={getContext:()=>e.getContext,store:()=>e.store,useLayoutEffect:()=>e.useLayoutEffect},c={},n.d(c,s),c);var s,c;n.d({},{});const u=t=>t.replace(/^attribute_(pa_)?/,"").replace(/-/g," ").toLowerCase(),d=(t,e)=>u(t)===u(e),{state:m}=(0,i.store)("woocommerce/products",{},{lock:"I acknowledge that using a private store means my plugin will inevitably break on the next store release."}),l=(t,e)=>{if(!Array.isArray(t.variation)||!Array.isArray(e))return!1;if(t.variation.length!==e.length)return!1;const o=m.productVariations[t.id]?.parent,r=m.products[o]?.attributes??[];return t.variation.every(({attribute:t,value:o})=>e.some(e=>{const a=r.find(e=>d(t,e.name))?.terms,n=a?.find(t=>t.name===o)?.slug||o;return d(e.attribute,t)&&e.value.toLowerCase()===n?.toLowerCase()}))},p="I acknowledge that using a private store means my plugin will inevitably break on the next store release.";var y=function(t){return t.IDLE="IDLE",t.SLIDE_OUT="SLIDE-OUT",t.SLIDE_IN="SLIDE-IN",t}(y||{});const{state:I}=(0,i.store)("woocommerce",{},{lock:p}),{state:g}=(0,i.store)("woocommerce/add-to-cart-with-options",{},{lock:p}),{state:C}=(0,i.store)("woocommerce/products",{},{lock:p}),w={state:{get quantity(){const t=C.productInContext;if(!t)return 0;const e=(0,i.getContext)("woocommerce/add-to-cart-with-options"),o=C.productVariationInContext?e?.selectedAttributes??[]:void 0;return((t,e)=>{let o=0;for(const r of t)r.id===e.id&&("parent_item_key"in r&&r.parent_item_key||(void 0===e.selectedAttributes||"variation"!==r.type||l(r,e.selectedAttributes))&&(o+=r.quantity));return o})(I.cart?.items??[],{id:t.id,...void 0!==o&&{selectedAttributes:o}})},get slideInAnimation(){const{animationStatus:t}=(0,i.getContext)();return t===y.SLIDE_IN},get slideOutAnimation(){const{animationStatus:t}=(0,i.getContext)();return t===y.SLIDE_OUT},get addToCartText(){const{animationStatus:t,tempQuantity:e,addToCartText:o,groupedProductIds:r,hasPressedButton:a,inTheCartText:n}=(0,i.getContext)(),s=t===y.IDLE||t===y.SLIDE_OUT?e||0:f.quantity;if("grouped"===C.productInContext?.type){const t=r?.map(t=>{const e=I.findItemInCart({id:t});return e?.quantity||0});return t?.some(t=>t>0)&&a?n:o}return s>0?n.replace("###",s.toString()):o},get displayViewCart(){const{displayViewCart:t}=(0,i.getContext)();return!!t&&f.quantity>0}},actions:{*addCartItem(){const t=C.productInContext;if(!t)return;yield Promise.resolve().then(n.bind(n,1401));const{actions:e}=(0,i.store)("woocommerce",{},{lock:p}),o=(0,i.getContext)();yield e.addCartItem({id:t.id,quantityToAdd:o.quantityToAdd,type:t.type},{showCartUpdatesNotices:!1}),o.displayViewCart=!0},*refreshCartItems(){yield Promise.resolve().then(n.bind(n,1401));const{actions:t}=(0,i.store)("woocommerce",{},{lock:p});t.refreshCartItems()},handleAnimationEnd(t){const e=(0,i.getContext)();"slideOut"===t.animationName?e.animationStatus=y.SLIDE_IN:"slideIn"===t.animationName&&(e.tempQuantity=f.quantity,e.animationStatus=y.IDLE)},handlePressedState(){const t=(0,i.getContext)();(void 0===g?.isFormValid||g?.isFormValid)&&(t.hasPressedButton=!0,t.tempQuantity!==f.quantity&&t.animationStatus===y.IDLE&&(t.animationStatus=y.SLIDE_OUT))}},callbacks:{syncTempQuantityOnLoad(){const t=(0,i.getContext)();(0,i.useLayoutEffect)(()=>{t.tempQuantity=f.quantity},[])},startAnimation(){const t=(0,i.getContext)();t.tempQuantity!==f.quantity&&t.animationStatus===y.IDLE&&(t.animationStatus=y.SLIDE_OUT)}}},{state:f}=(0,i.store)("woocommerce/product-button",w,{lock:!0});
Save File
Cancel