Forum

Thread tagged as: Question, Problem, Shop

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.

Jon Young

Jon Young 0 points

  • 5 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

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?

Drew McLellan

Drew McLellan 2638 points
Perch Support

I wasn't asking rhetorically, I was looking to help. Taking a blind guess:

$cats = perch_page_attributes(['template' => 'shop_settings.html'], true);

$cat_paths = [];
if (count($cats)) {
    foreach($cats as $cat) {
        $cat_paths[] = $cat['catPath'];
    }
}

perch_shop_products([
    'template' => '/products/rhapso-product-list.html',
    'category' => $cat_paths,
]);

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:

Warning: Invalid argument supplied for foreach()

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.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Can you try this:

$cats = perch_page_attributes([
        'template' => 'shop_settings.html',
        'skip-template' => true,
], true);

If I do this:

$cats = perch_page_attributes([
    'template' => 'shop_settings.html',
    'skip-template' => true,
], true);

print_r($cats);

It returns all of my page attributes:

Array ( [description] => Array ( [_flang] => plain [raw] => [processed] => ) [keywords] => Array ( [_flang] => plain [raw] => [processed] => ) [noindex] => [nofollow] => [nosnippet] => [gmaps] => [productsToDisplay] => Array ( [0] => 1 ) [navGroup] => repairs [pageID] => 5 [pageParentID] => 0 [pagePath] => /accessories.php [pageTitle] => Accessories [pageNavText] => Accessories [pageNew] => 0 [pageOrder] => 4 [pageDepth] => 1 [pageSortPath] => /accessories [pageTreePosition] => 000-004 [pageSubpageRoles] => [pageSubpagePath] => / [pageHidden] => 0 [pageNavOnly] => 0 [pageAccessTags] => [pageCreatorID] => 1 [pageModified] => 2016-08-20 21:50:03 [pageAttributes] => {"description":{"_flang":"plain","raw":"","processed":""},"keywords":{"_flang":"plain","raw":"","processed":""},"noindex":null,"nofollow":null,"nosnippet":null,"gmaps":null,"productsToDisplay":["1"],"navGroup":"repairs"} [pageAttributeTemplate] => default.html [pageTemplate] => [templateID] => 1 [pageSubpageTemplates] => [pageCollections] => [perch_description] => Array ( [_flang] => plain [raw] => [processed] => ) [perch_keywords] => Array ( [_flang] => plain [raw] => [processed] => ) [perch_noindex] => [perch_nofollow] => [perch_nosnippet] => [perch_gmaps] => [perch_productsToDisplay] => Array ( [0] => 1 ) [perch_navGroup] => repairs )

It's weird - if I use my original setup:

$cats = perch_page_attributes(array('template' => 'shop_settings.html'), true);

It returns exactly what I want:

array('products/accessories/')

...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:

perch_shop_products([
    'template' => '/products/rhapso-product-list.html',
    'category' => array('products/accessories/')
]);
Drew McLellan

Drew McLellan 2638 points
Perch Support

Can you show me shop_settings.html ?

Sure:

<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>
Drew McLellan

Drew McLellan 2638 points
Perch Support

Make it like this:

<perch:categories id="productsToDisplay" set="products" label="Category" divider-before="Product categories to display" ><perch:category id="catPath" /><perch:if not-exists="perch_item_last">|</perch:if></perch:categories>

And then:

$cats = perch_page_attributes(array('template' => 'shop_settings.html'), true);
$cats = explode('|', $cats);

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.