Forum
Setting shop categories via page attributes
I want to allow the client to set what category of products get displayed per page using page attributes.
I can select categories via 'page details', but the below does not work:
$cats = perch_page_attributes(array('template' => 'shop_settings.html'), true);
perch_shop_products([
'template' => '/products/rhapso-product-list.html',
'category' => $cats
]);
shop_settings.html is:
<perch:categories id="productsToDisplay" set="products" label="Category" divider-before="Product categories to display" >
<perch:before>array(</perch:before>'<perch:category id="catPath" />'<perch:if not-exists="perch_item_last">,</perch:if><perch:after>)</perch:after>
</perch:categories>
How can I get this to work?
Thanks in advance.
If you inspect your
$cats
variable, what does it look like? I suspect you might need to trim it down to just the paths.If I echo the $cats variable and then use that output by copying and pasting to the category filter, it works. It's just when I directly use the variable in code, it doesn't. What do you mean trim it down?
I wasn't asking rhetorically, I was looking to help. Taking a blind guess:
That's fine Drew, thanks for your help :-)
I'll take a look at this this evening.
I think the problem is that it's not recognising $cats as an array. Even if I use your solution above, it errors with:
I cant seem to get this right Drew. Essentially, I would like to use whatever categories selected when editing/creating a page using page attributes to then display products by the categories selected.
Any help with this would be appreciated.
Can you try this:
If I do this:
It returns all of my page attributes:
It's weird - if I use my original setup:
It returns exactly what I want:
...But I can also echo that value, which I shouldn't be able to do - it should give me a Notice: Array to string conversion error.
Even though it gives me what I want, it doesn't work unless I paste it directly into the perch_shop_products:
Can you show me
shop_settings.html
?Sure:
Make it like this:
And then:
That worked brilliantly! Thanks very much Drew. It's funny, I tried explode before, but I used semi-colon instead of pipe but it didnt work. I must have done something wrong...
Thanks again.