← Back
Editing: product-filter-attribute.js
(()=>{var e,t,o,r={4796:(e,t,o)=>{"use strict";var r=o(5573),l=o(790);const n=window.wc.wcSettings,a=window.wp.blocks;var s=o(7723);const c=JSON.parse('{"$schema":"https://schemas.wp.org/trunk/block.json","name":"woocommerce/product-filter-attribute","title":"Attribute Filter","description":"Enable customers to filter the product grid by selecting one or more attributes, such as color.","category":"woocommerce","keywords":["WooCommerce"],"textdomain":"woocommerce","apiVersion":3,"ancestor":["woocommerce/product-filters"],"supports":{"interactivity":true,"color":{"text":true,"background":false,"__experimentalDefaultControls":{"text":false}},"typography":{"fontSize":true,"lineHeight":true,"__experimentalFontWeight":true,"__experimentalFontFamily":true,"__experimentalFontStyle":true,"__experimentalTextTransform":true,"__experimentalTextDecoration":true,"__experimentalLetterSpacing":true,"__experimentalDefaultControls":{"fontSize":false}},"spacing":{"margin":true,"padding":true,"blockGap":true,"__experimentalDefaultControls":{"margin":false,"padding":false,"blockGap":false}},"__experimentalBorder":{"color":true,"radius":true,"style":true,"width":true,"__experimentalDefaultControls":{"color":false,"radius":false,"style":false,"width":false}}},"usesContext":["query","filterParams"],"attributes":{"attributeId":{"type":"number","default":0},"showCounts":{"type":"boolean","default":false},"queryType":{"type":"string","default":"or"},"displayStyle":{"type":"string","default":"woocommerce/product-filter-checkbox-list"},"selectType":{"type":"string","default":"multiple"},"isPreview":{"type":"boolean","default":false},"sortOrder":{"type":"string","default":"count-desc"},"hideEmpty":{"type":"boolean","default":true}},"example":{"attributes":{"isPreview":true}},"style":"woocommerce/product-filter-attribute-view-style"}'),i=window.wc.wcBlocksData,u=window.wp.data;var d=o(6087),m=o(923),p=o.n(m);function _(e){const t=(0,d.useRef)(e);return p()(e,t.current)||(t.current=e),t.current}const b=window.wc.wcTypes,y=e=>{const{namespace:t,resourceName:o,resourceValues:r=[],query:l={},shouldSelect:n=!0}=e;if(!t||!o)throw new Error("The options object must have valid values for the namespace and the resource properties.");const a=(0,d.useRef)({results:[],isLoading:!0}),s=_(l),c=_(r),m=(()=>{const[,e]=(0,d.useState)();return(0,d.useCallback)((t=>{e((()=>{throw t}))}),[])})(),p=(0,u.useSelect)((e=>{if(!n)return null;const r=e(i.COLLECTIONS_STORE_KEY),l=[t,o,s,c],a=r.getCollectionError(...l);if(a){if(!(0,b.isError)(a))throw new Error("TypeError: `error` object is not an instance of Error constructor");m(a)}return{results:r.getCollection(...l),isLoading:!r.hasFinishedResolution("getCollection",l)}}),[t,o,c,s,n,m]);return null!==p&&(a.current=p),a.current};var f=o(4347),w=o(9456);const h=(0,d.createContext)("page"),g=()=>(0,d.useContext)(h),x=(h.Provider,e=>{const t=g();e=e||t;const o=(0,u.useSelect)((t=>t(i.QUERY_STATE_STORE_KEY).getValueForQueryContext(e,void 0)),[e]),{setValueForQueryContext:r}=(0,u.useDispatch)(i.QUERY_STATE_STORE_KEY);return[o,(0,d.useCallback)((t=>{r(e,t)}),[e,r])]}),v=(e,t,o)=>{const r=g();o=o||r;const l=(0,u.useSelect)((r=>r(i.QUERY_STATE_STORE_KEY).getValueForQueryKey(o,e,t)),[o,e]),{setQueryValue:n}=(0,u.useDispatch)(i.QUERY_STATE_STORE_KEY);return[l,(0,d.useCallback)((t=>{n(o,e,t)}),[o,e,n])]},k=window.wp.blockEditor,S=window.wp.components,C=[{label:(0,s.__)("Blue","woocommerce"),value:"blue",rawData:{id:23,name:(0,s.__)("Blue","woocommerce"),slug:"blue",attr_slug:"blue",description:"",parent:0,count:4}},{label:(0,s.__)("Gray","woocommerce"),value:"gray",selected:!0,rawData:{id:29,name:(0,s.__)("Gray","woocommerce"),slug:"gray",attr_slug:"gray",description:"",parent:0,count:3}},{label:(0,s.__)("Green","woocommerce"),value:"green",rawData:{id:24,name:(0,s.__)("Green","woocommerce"),slug:"green",attr_slug:"green",description:"",parent:0,count:3}},{label:(0,s.__)("Red","woocommerce"),value:"red",selected:!0,rawData:{id:25,name:(0,s.__)("Red","woocommerce"),slug:"red",attr_slug:"red",description:"",parent:0,count:4}},{label:(0,s.__)("Yellow","woocommerce"),value:"yellow",rawData:{id:30,name:(0,s.__)("Yellow","woocommerce"),slug:"yellow",attr_slug:"yellow",description:"",parent:0,count:1}}],j={"name-asc":(0,s.__)("Name, A to Z","woocommerce"),"name-desc":(0,s.__)("Name, Z to A","woocommerce"),"count-desc":(0,s.__)("Most results first","woocommerce"),"count-asc":(0,s.__)("Least results first","woocommerce")},T=Object.entries(j).map((([e,t])=>({label:t,value:e}))),E=c.attributes.sortOrder.default,O=c.attributes.queryType.default,B=(e,t)=>{if(!e)return null;if(0===e.innerBlocks.length)return null;for(const o of e.innerBlocks){if(t(o))return o;const e=B(o,t);if(e)return e}return null},L=(e,t)=>B(e,(function(e){return e.name===t})),P=({clientId:e,currentStyle:t,onChange:o})=>{const r=(0,u.select)("core/block-editor").getBlock(e),n=r?.name,s=(0,a.getBlockTypes)().filter((e=>n?e.ancestor?.includes(n):[])),{insertBlock:c,replaceBlock:i}=(0,u.useDispatch)("core/block-editor"),[m,p]=(0,d.useState)({});return 0===s.length?null:(0,l.jsx)(S.__experimentalToggleGroupControl,{value:t,isBlock:!0,__nextHasNoMarginBottom:!0,__next40pxDefaultSize:!0,label:"",hideLabelFromVision:!0,onChange:e=>{if(!e||"string"!=typeof e)return;if(!r)return;const l=L(r,t);l?(p({...m,[t]:l.attributes}),i(l.clientId,(0,a.createBlock)(e,m[e]||{}))):c((0,a.createBlock)(e),r.innerBlocks.length,r.clientId,!1),o(e)},style:{width:"100%"},children:s.map((e=>(0,l.jsx)(S.__experimentalToggleGroupControlOption,{label:e.title,value:e.name},e.name)))})};function D(e,t){const o=(0,u.select)("core/block-editor").getBlock(e);if(!o)return;const r=o.name,l=(0,a.getBlockTypes)().filter((e=>e.ancestor?.includes(r))).find((e=>L(o,e.name))),n=l?L(o,l.name):null,{insertBlock:s,replaceBlock:c}=(0,u.dispatch)("core/block-editor");n?c(n.clientId,(0,a.createBlock)(t)):s((0,a.createBlock)(t),o.innerBlocks.length,o.clientId,!1)}let I=[];const q=({clientId:e,attributes:t,setAttributes:o})=>{const{sortOrder:r,queryType:n,displayStyle:i,showCounts:u,hideEmpty:m}=t;return 0===I.length&&(I=(0,a.getBlockTypes)().filter((e=>e.ancestor?.includes("woocommerce/product-filter-attribute")))),(0,l.jsx)(l.Fragment,{children:(0,l.jsx)(k.InspectorControls,{children:(0,l.jsxs)(S.__experimentalToolsPanel,{label:(0,s.__)("Display Settings","woocommerce"),resetAll:()=>{o({sortOrder:E,queryType:O,displayStyle:c.attributes.displayStyle.default,showCounts:c.attributes.showCounts.default,hideEmpty:c.attributes.hideEmpty.default}),D(e,c.attributes.displayStyle.default)},children:[(0,l.jsx)(S.__experimentalToolsPanelItem,{label:(0,s.__)("Sort Order","woocommerce"),hasValue:()=>r!==E,onDeselect:()=>o({sortOrder:E}),children:(0,l.jsx)(S.SelectControl,{label:(0,s.__)("Sort order","woocommerce"),value:r,options:[{value:"",label:(0,s.__)("Select an option","woocommerce"),disabled:!0},...T],onChange:e=>{e&&Object.keys(j).includes(e)&&o({sortOrder:e})},help:(0,s.__)("Determine the order of filter options.","woocommerce"),__nextHasNoMarginBottom:!0})}),(0,l.jsx)(S.__experimentalToolsPanelItem,{label:(0,s.__)("Logic","woocommerce"),hasValue:()=>n!==O,onDeselect:()=>o({queryType:O}),children:(0,l.jsxs)(S.__experimentalToggleGroupControl,{label:(0,s.__)("Logic","woocommerce"),isBlock:!0,value:n,onChange:e=>{"and"!==e&&"or"!==e||o({queryType:e})},__nextHasNoMarginBottom:!0,__next40pxDefaultSize:!0,style:{width:"100%"},help:"and"===n?(0,d.createInterpolateElement)((0,s.__)("Display products that match <b>all</b> selected attributes (they need to include <b>all of them</b>).","woocommerce"),{b:(0,l.jsx)("strong",{})}):(0,s.__)("Display products that match any of the selected attributes (they don't need to match all).","woocommerce"),children:[(0,l.jsx)(S.__experimentalToggleGroupControlOption,{label:(0,s.__)("Any","woocommerce"),value:"or"}),(0,l.jsx)(S.__experimentalToggleGroupControlOption,{label:(0,s.__)("All","woocommerce"),value:"and"})]})}),(0,l.jsx)(S.__experimentalToolsPanelItem,{label:(0,s.__)("Display Style","woocommerce"),hasValue:()=>i!==c.attributes.displayStyle.default,isShownByDefault:!0,onDeselect:()=>{o({displayStyle:c.attributes.displayStyle.default}),D(e,c.attributes.displayStyle.default)},children:(0,l.jsx)(P,{clientId:e,currentStyle:i,onChange:e=>o({displayStyle:e})})}),(0,l.jsx)(S.__experimentalToolsPanelItem,{label:(0,s.__)("Product counts","woocommerce"),hasValue:()=>u!==c.attributes.showCounts.default,onDeselect:()=>o({showCounts:c.attributes.showCounts.default}),isShownByDefault:!0,children:(0,l.jsx)(S.ToggleControl,{label:(0,s.__)("Product counts","woocommerce"),checked:u,onChange:e=>o({showCounts:e}),__nextHasNoMarginBottom:!0})}),(0,l.jsx)(S.__experimentalToolsPanelItem,{label:(0,s.__)("Hide items with no products","woocommerce"),hasValue:()=>m!==c.attributes.hideEmpty.default,onDeselect:()=>o({hideEmpty:c.attributes.hideEmpty.default}),children:(0,l.jsx)(S.ToggleControl,{label:(0,s.__)("Hide items with no products","woocommerce"),checked:m,onChange:e=>o({hideEmpty:e}),__nextHasNoMarginBottom:!0})})]})},"inspector")})};o(2933);const A=(0,n.getSetting)("attributes",[]).reduce(((e,t)=>{const o=(r=t)&&r.attribute_name?{id:parseInt(r.attribute_id,10),name:r.attribute_name,taxonomy:"pa_"+r.attribute_name,label:r.attribute_label}:null;var r;return o&&o.id&&e.push(o),e}),[]),R=(e=[])=>(0,a.getBlockTypes)().map((e=>e.name)).filter((t=>!e.includes(t))),V=["woocommerce/product-filters","woocommerce/product-filter-attribute","woocommerce/product-filter-active","woocommerce/product-filter-price","woocommerce/product-filter-status","woocommerce/product-collection","core/query"];o(1652);const M=({children:e})=>(0,l.jsxs)("div",{className:"wc-block-product-filter-components-initial-disabled",children:[(0,l.jsx)("div",{className:"wc-block-product-filter-components-initial-disabled-overlay"}),e]});var N=o(2624);o(1007);const F=({children:e})=>(0,l.jsxs)("div",{className:"wc-block-product-filter-components-notice",children:[(0,l.jsx)(S.Icon,{className:"wc-block-product-filter-components-notice__icon",icon:N.A}),(0,l.jsx)("div",{className:"wc-block-product-filter-components-notice__content",children:e})]});function H(e){return"string"==typeof e.label?e.label:e.ariaLabel}const G=(0,n.getSetting)("attributes",[]),Y=(0,S.withSpokenMessages)((e=>{const{attributes:t}=e,{attributeId:o,queryType:r,isPreview:n,displayStyle:a,showCounts:c,sortOrder:i,hideEmpty:u}=t,m=function(e){if(e)return A.find((t=>t.id===e))}(o),[p,h]=(0,d.useState)([]),[S,j]=(0,d.useState)(!0),{results:T,isLoading:E}=y({namespace:"/wc/store/v1",resourceName:"products/attributes/terms",resourceValues:[m?.id||0],shouldSelect:!!m?.id,query:{orderby:"menu_order",hide_empty:u}}),{data:O,isLoading:B}=(({queryAttribute:e,queryTaxonomy:t,queryPrices:o,queryStock:r,queryRating:l,queryState:n,isEditor:a=!1})=>{let s=g();s=`${s}-collection-data`;const[c]=x(s),[i,u]=v("calculate_attribute_counts",[],s),[m,p]=v("calculate_taxonomy_counts",[],s),[h,k]=v("calculate_price_range",null,s),[S,C]=v("calculate_stock_status_counts",null,s),[j,T]=v("calculate_rating_counts",null,s),E=_(e||{}),O=_(t),B=_(o),L=_(r),P=_(l);(0,d.useEffect)((()=>{"object"==typeof E&&Object.keys(E).length&&(i.find((e=>(0,b.objectHasProp)(E,"taxonomy")&&e.taxonomy===E.taxonomy))||u([...i,E]))}),[E,i,u]),(0,d.useEffect)((()=>{O&&!m.includes(O)&&p([...m,O])}),[O,m,p]),(0,d.useEffect)((()=>{h!==B&&void 0!==B&&k(B)}),[B,k,h]),(0,d.useEffect)((()=>{S!==L&&void 0!==L&&C(L)}),[L,C,S]),(0,d.useEffect)((()=>{j!==P&&void 0!==P&&T(P)}),[P,T,j]);const[D,I]=(0,d.useState)(a),[q]=(0,f.d7)(D,200);D||I(!0);const A=(0,d.useMemo)((()=>(e=>{const t=e;return Array.isArray(e.calculate_attribute_counts)&&(t.calculate_attribute_counts=(0,w.di)(e.calculate_attribute_counts.map((({taxonomy:e,queryType:t})=>({taxonomy:e,query_type:t})))).asc(["taxonomy","query_type"])),Array.isArray(e.calculate_taxonomy_counts)&&(t.calculate_taxonomy_counts=e.calculate_taxonomy_counts),t})(c)),[c]),{results:R,isLoading:V}=y({namespace:"/wc/store/v1",resourceName:"products/collection-data",query:{...n,page:void 0,per_page:void 0,orderby:void 0,order:void 0,...A},shouldSelect:q});return{data:R,isLoading:V}})({queryAttribute:{taxonomy:m?.taxonomy||"",queryType:r},queryState:{},isEditor:!0});(0,d.useEffect)((()=>{if(E||B)return;const e=(0,b.objectHasProp)(O,"attribute_counts")&&(t=O.attribute_counts,Array.isArray(t)&&t.every((e=>"term"in e&&"count"in e)))?O.attribute_counts.map((e=>e.term)):[];var t;if(0===e.length&&u)h([]);else{const t=T.filter((t=>!u||e.includes(t.id))).map(((e,t)=>({label:e.name,value:e.id.toString(),selected:0===t,count:e.count})));h(function(e,t){return e.sort(((e,o)=>{switch(t){case"menu_order-asc":{var r,l;const t=null!==(r=e.menuOrder)&&void 0!==r?r:0,n=null!==(l=o.menuOrder)&&void 0!==l?l:0;return t!==n?t-n:H(e).localeCompare(H(o))}case"name-asc":return H(e).localeCompare(H(o));case"name-desc":return H(o).localeCompare(H(e));case"count-asc":return e.count-o.count;default:return o.count-e.count}}))}(t,i))}j(!1)}),[c,T,O,i,u,E,B,m]);const{children:L,...P}=(0,k.useInnerBlocksProps)((0,k.useBlockProps)(),{allowedBlocks:R(V),template:[["core/heading",{level:3,content:m?.label||(0,s.__)("Attribute","woocommerce"),style:{spacing:{margin:{bottom:"0.625rem",top:"0"}}}}],[a]]}),D=E||B||S;return 0===Object.keys(G).length?(0,l.jsxs)("div",{...P,children:[(0,l.jsx)(q,{...e}),(0,l.jsx)(F,{children:(0,l.jsx)("p",{children:(0,s.__)("Attributes are needed for filtering your products. You haven't created any attributes yet.","woocommerce")})})]}):o&&m?D||0!==T.length?(0,l.jsxs)("div",{...P,children:[(0,l.jsx)(q,{...e}),(0,l.jsx)(M,{children:(0,l.jsx)(k.BlockContextProvider,{value:{filterData:{items:0===p.length&&n?C:p,isLoading:D,showCounts:c}},children:L})})]}):(0,l.jsxs)("div",{...P,children:[(0,l.jsx)(q,{...e}),(0,l.jsx)(F,{children:(0,l.jsx)("p",{children:(0,s.__)("There are no products with the selected attributes.","woocommerce")})})]}):(0,l.jsxs)("div",{...P,children:[(0,l.jsx)(q,{...e}),(0,l.jsx)(F,{children:(0,l.jsx)("p",{children:(0,s.__)("Please select an attribute to use this filter!","woocommerce")})})]})})),Q=(0,n.getSetting)("attributes",[]),K=(0,n.getSetting)("defaultProductFilterAttribute");(0,a.registerBlockType)(c,{edit:Y,icon:()=>(0,l.jsx)(r.SVG,{width:"24",height:"24",viewBox:"0 0 24 24",fill:"none",xmlns:"http://www.w3.org/2000/svg",children:(0,l.jsx)(r.Path,{fillRule:"evenodd",clipRule:"evenodd",d:"M4.25 4C4.25 3.58579 4.58579 3.25 5 3.25L9.17284 3.25C9.37179 3.25 9.56258 3.32904 9.70324 3.46974L13.2877 7.05515L13.2893 7.05673C13.5844 7.35367 13.75 7.75532 13.75 8.17393C13.75 8.59255 13.5844 8.99419 13.2893 9.29114L12.7573 8.76246L13.2877 9.29272L10.2961 12.2851C10.1489 12.4325 9.97417 12.5494 9.78179 12.6292C9.58941 12.7089 9.38319 12.75 9.17493 12.75C8.96667 12.75 8.76045 12.7089 8.56807 12.6292C8.37575 12.5494 8.20104 12.4326 8.05392 12.2853L8.05378 12.2851L4.46991 8.7045C4.32911 8.56383 4.25 8.37296 4.25 8.17393V4ZM12.2253 8.23378L12.2257 8.23341C12.2412 8.21758 12.25 8.19622 12.25 8.17393C12.25 8.15164 12.2412 8.13029 12.2257 8.11446L12.2253 8.11409L8.86212 4.75H5.75V7.86307L9.11456 11.2246L9.11517 11.2252C9.12303 11.2331 9.13236 11.2393 9.14261 11.2436C9.15286 11.2478 9.16384 11.25 9.17493 11.25C9.18602 11.25 9.197 11.2478 9.20725 11.2436C9.2175 11.2393 9.22683 11.2331 9.23469 11.2252L9.23499 11.2249L12.2253 8.23378ZM12.9999 20H3.99988V18.5H12.9999V20ZM3.99988 16L19.9999 16V14.5L3.99988 14.5V16Z",fill:"currentColor"})}),attributes:{...c.attributes,attributeId:{...c.attributes.attributeId,default:parseInt(K.attribute_id,10)}},save:()=>{const e=k.useBlockProps.save(),t=k.useInnerBlocksProps.save(e);return(0,l.jsx)("div",{...t})},variations:Q.map(((e,t)=>({name:`product-filter-attribute-${e.attribute_name}`,title:(0,s.sprintf)( // translators: %s is the attribute label. // translators: %s is the attribute label. (0,s.__)("%s Filter","woocommerce"),e.attribute_label),description:(0,s.sprintf)( // translators: %s is the attribute label. // translators: %s is the attribute label. (0,s.__)("Enable customers to filter the product collection by selecting one or more %s attributes.","woocommerce"),e.attribute_label),attributes:{attributeId:parseInt(e.attribute_id,10)},isActive:["attributeId"],isDefault:0===t})))})},1652:()=>{},1007:()=>{},2933:()=>{},1609:e=>{"use strict";e.exports=window.React},790:e=>{"use strict";e.exports=window.ReactJSXRuntime},6087:e=>{"use strict";e.exports=window.wp.element},7723:e=>{"use strict";e.exports=window.wp.i18n},923:e=>{"use strict";e.exports=window.wp.isShallowEqual},5573:e=>{"use strict";e.exports=window.wp.primitives}},l={};function n(e){var t=l[e];if(void 0!==t)return t.exports;var o=l[e]={exports:{}};return r[e](o,o.exports,n),o.exports}n.m=r,e=[],n.O=(t,o,r,l)=>{if(!o){var a=1/0;for(u=0;u<e.length;u++){for(var[o,r,l]=e[u],s=!0,c=0;c<o.length;c++)(!1&l||a>=l)&&Object.keys(n.O).every((e=>n.O[e](o[c])))?o.splice(c--,1):(s=!1,l<a&&(a=l));if(s){e.splice(u--,1);var i=r();void 0!==i&&(t=i)}}return t}l=l||0;for(var u=e.length;u>0&&e[u-1][2]>l;u--)e[u]=e[u-1];e[u]=[o,r,l]},n.n=e=>{var t=e&&e.__esModule?()=>e.default:()=>e;return n.d(t,{a:t}),t},o=Object.getPrototypeOf?e=>Object.getPrototypeOf(e):e=>e.__proto__,n.t=function(e,r){if(1&r&&(e=this(e)),8&r)return e;if("object"==typeof e&&e){if(4&r&&e.__esModule)return e;if(16&r&&"function"==typeof e.then)return e}var l=Object.create(null);n.r(l);var a={};t=t||[null,o({}),o([]),o(o)];for(var s=2&r&&e;"object"==typeof s&&!~t.indexOf(s);s=o(s))Object.getOwnPropertyNames(s).forEach((t=>a[t]=()=>e[t]));return a.default=()=>e,n.d(l,a),l},n.d=(e,t)=>{for(var o in t)n.o(t,o)&&!n.o(e,o)&&Object.defineProperty(e,o,{enumerable:!0,get:t[o]})},n.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),n.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.j=2448,(()=>{var e={2448:0};n.O.j=t=>0===e[t];var t=(t,o)=>{var r,l,[a,s,c]=o,i=0;if(a.some((t=>0!==e[t]))){for(r in s)n.o(s,r)&&(n.m[r]=s[r]);if(c)var u=c(n)}for(t&&t(o);i<a.length;i++)l=a[i],n.o(e,l)&&e[l]&&e[l][0](),e[l]=0;return n.O(u)},o=globalThis.webpackChunkwebpackWcBlocksMainJsonp=globalThis.webpackChunkwebpackWcBlocksMainJsonp||[];o.forEach(t.bind(null,0)),o.push=t.bind(null,o.push.bind(o))})();var a=n.O(void 0,[94],(()=>n(4796)));a=n.O(a),((this.wc=this.wc||{}).blocks=this.wc.blocks||{})["product-filter-attribute"]=a})();
Save File
Cancel