Jump to content

How to count products in categories if the product-pages are not in the category tree?


Recommended Posts

Hello, i must count products per category like so:

category 1 (4 products)
--category 1.1 (2 products)
----category 1.1.1 (1 product)

and so on.

category 1
--category 1.1
----category 1.1.1
----category 1.1.2
----category 1.1.3

--category 1.2
----category 1.2.1
----category 1.2.2
----category 1.2.3

produkts have page references for the categories:

product 1
-- page reference->category 1.1.1
-- page reference->category 1.1.3
-- page reference->category 1.2.2
-- page reference->category 1.2.3

what is the best way to do this, i have many categories and many many products.
Thank you.


Link to comment
Share on other sites

1 hour ago, Matzn said:


use $pages->count https://processwire.com/api/ref/pages/count/ 

for each category like:

$cnt = $pages->count('parent=yourCategory,id=yourProductId');


yes, i know count, so far i have this result, but only for level 3 of the categories:

//categories level3


foreach ($kategorienId as $id){
    $count=$pages->count("template=produkt, pro_kategorie=$id");
$kategorienId->data('count', $produktCount);


and this is the ugly function getCategoryLevel3:

function getCategoryLevel3($page){
    if ($page->children!=''){
        foreach($children as $children2){    
                foreach($children2->children as $children3){
                        foreach($children3->children as $child3){
                            //level3 add id    
    return $id;

there is a better way to do this?
I also need the number of products for level2 and level1.

Link to comment
Share on other sites

Your function returns only category 3. To get all pages (categories) in you category tree you can

$kategorien = $page->find();


$kategorien = $page->descendants();

After count your products for each category

foreach ($kategorien as $kategorie){

    $count = $pages->count("template=produkt, pro_kategorie={$kategorie->id}");

	echo $count . " Produkte in Kategorie " . $kategorie->title;


Link to comment
Share on other sites

The page reference field (pro_kategorie) have only the level 3 category.
Here i can count the products, but then i have to go up to  level 2  and
add all products in all level 3 categories to the count of level 2.

Sorry if that was not clear.

 I have to search the categories from bottom to top like this
this doesn't make things easy in processwire, or did I miss something?

Link to comment
Share on other sites

Thanks for your feedback.

I ended up doing this:
For each product I store the parend category for each category in the page reference field. This way I can easy count all products in the parent category.

$this->addHookBefore('Pages::saved', function(HookEvent $event) {
$pages = $event->object;
$page = $event->arguments(0);

	if($page->template == 'produkt') {
		//remove parent kategorie
		foreach($page->pro_kategorie_parent as $item) {
		foreach($page->pro_raeume_parent as $item) {
		//add parent kategorie
		if ($page->pro_kategorie){
			foreach($page->pro_kategorie as $item) {
			$page->setAndSave(pro_kategorie_parent, $cat_parent);
		if ($page->pro_raeume){
			foreach($page->pro_raeume as $item) {
			$page->setAndSave(pro_raeume_parent, $raum_parent);


Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...