Forum

Thread tagged as: Question, PayPal

Category Sort Order and Multiple Categories with Products on a page (Paypal Shop...

I need to change the sort order of the categories in the shop when they are loaded into the front end of the site (I need it to be by categoryID rather than categoryTitle but I can't seem to find how to do this. I have tried adding it into the array when calling perch_shop_categories but that doesn't seem to work

<?php perch_shop_categories(array('template' => 'home_page_listing.html', 'sort' => 'categoryID')); ?>

Is this not available to use for categories?

I also need to be able to load certain categories (or at the moment all categories as there are only 4) into the shop/index.php with a selection of related products. I can load the categories no problem but I have no idea how to load and display X products per category as well. Again is this functionality available and I am just missing it?

If these functions aren't available and I want to use my own PHP to create my required functionality, where is it best to put it? Do I have to set up a whole APP using the API to add in these functions?

Emma Davis

Emma Davis 0 points

  • 7 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

I don't believe they are sortable in the current version. I think you'd need to use skip-template and then sort them in PHP.

You can use perch_shop_products_by_category() to get products by category.

Thanks Drew, I will look at that.

I think you misunderstand about the products though (unless I am missing something) as I know how to get products by category but that is presuming based on there only being 1 category on the page (i.e. in shop/category.php) not multiple categories. I need to dynamically load all the shop categories and 4 of their products on one page.

I think where I get confused is calling perch functions in the php file which then relies on an html template to format. So if I use skip-template and assign the returned data to a variable I can then use my own PHP to do what I need - is that right?

Drew McLellan

Drew McLellan 2638 points
Perch Support

You'd need to loop through your categories and display the products for each one.

Thanks Drew, yes that is what I meant (starting to get my head around things now lol)

Hi Drew

Sorry, but my head is obviously not working proplery this morning as I can't seem to be able to loop through the categories no matter what I try.

This is what I have tried with the errors I get as comments:

$categories = perch_shop_categories(array('skip-template' => true));

foreach ($categories as $cat) {
                echo $cat['categoryID']; // Cannot use object of type PerchShop_Category as array
            }

foreach ($categories as $cat) {
                echo $cat->categoryID; // Undefined property PerchShop_Category::$categoryID
            }

foreach ($categories as $cat) {
                echo $cat->details->categoryID; // Cannot access protected property PerchShop_Category::$details
            }

Help!

Drew McLellan

Drew McLellan 2638 points
Perch Support

Try:

foreach ($categories as $cat) {
    echo $cat->categoryID();
}