Article Count for every category in sidebar of listing/category page

Hello Guys,

I am new to Shopware, developing plugin to get the article count of a particular category and display in the listing/category page beside the category name in side bar, I have written the below code in Bootstrap.php of my plugin but I am getting only the parent ID of category.

 

        
    /** @var \Enlight_Controller_Action $controller */
             
       
        $request = $args->getSubject()->Request();
        $response = $args->getSubject()->Response();
        $view = $args->getSubject()->View();
        $parent = Shopware()->Shop()->get(‚parentID‘);
        $categoryId = $args->getRequest()->getParam(‚sCategory‘);
        $view->assign(‚id‘,$categoryId);
        $view->addTemplateDir(__DIR__ . ‚/Views/responsive‘);

Kindly reply with the solution to get ID of each category.

Hi,

the count of the products in the category is already available in the template. The template variable is called ‘sNumberArticles’

So you only to extend the template block and print out the number there 

Best regards from Schöppingen

cool Michael Telgmann

2 „Gefällt mir“

Dear Schöppingen,

Firstly thank you very much for replying to my question, I have applied your solution to my template and I am getting the following outpu, please check the below screen shot.

Please reply to my query, your help will be very much thankful.

Hi,

this is because the variable sNumberArticles contains the number of the current category, which is „Mountain air & adventure“ in your case. If you want to add the right count for each category, you have to extend the sCategories variable. Which means you have to change the result of the \sCategories::sGetCategories() method.

Best regards

cool Michael Telgmann

1 „Gefällt mir“

Dear Michael Telgmann,

Thank you for the valuable reply, I understood the answer for it, but the extending should be be done in template level or plugin ?? this question may be awkward but I am new to Shopware so please helpme out with your detailed reply.

Thank you so much.

Hi,

in your own template, which is extending from on of the default themes (Bare, Responsive), you should only do, yeah, template stuff  Wink Also see the dev docs

A plugin extends the functionality of shopware. So this is the right place for your extension. Read Developing plugins for further information. 

Best regards

cool Michael Telgmann

Dear Michael Telgmann,

Thank you for the reply, I have got two clues from your suggestions, I am mentionaing it below:

* sCategories::sGetCategoryIdByArticleId function use now the s_articles_categories table.
 * We have to extend the sCategories variable. Which means you have to change the result of the sCategories::sGetCategories() method.
 

Can you please help me to implement it, I don’t need complete code, just need to know how to extend sCategories class in my plugin or template.  I can see sCategories class returns $category array which has all info about category but not article count. Please reply me with a hint.

Thank you.

Hi,

I can see sCategories class returns $category array which has all info about category but not article count.

That’s right. This is what you have to add to the array  Wink  In this article you can read about how to find extension points: Best practices of Shopware plugin development

In your case you need an after hook on the sCategories::sGetCategories() method, to extend the array which the method returns. Unfortunately there is no event, you can use. After you added the new value, you can use it in the themes/Frontend/Bare/frontend/index/sidebar-categories.tpl template file.

Best regards

cool Michael Telgmann

Dear Michael Telgmann,

I have went through the links you provided, not found any appropiate solution to my problem, can you please help me with some more in depth hint, I  need an example how to extend the sCategories::sGetCategories(). 

What I am trying to do is, I will get the ID of each category through my plugin and count products in s_srticles_categories and display in the TPL which you mentioned in previous reply.

Please help me for my solution.

Thank you.

 

 

Hallo ajit369,

you can simply use your orginal code, get the categories array from the view var.

$sCategories = $view->getAssign('sCategories');

// do waht you want

$view->assign('sCategories',$sCategories);

Regard’s,

Ahmad

Dear ahmadsaad , 

Thank you for the hint, but my target is to get ID of the category from the TPL to my plugin, so that I can fetch the count, as per your answer sCategories array is sent to TPL , looping there and pritining the ID or other fields, I am unable to access ID of individual category in my plugin.

Please reply to for my query.

Thank you.

As I understand you want to get the id for every child category then calculate the product count to show it in the template.

 

Also sCategories have all child categories you just add the code to your plugin.

 

Und loop throug the child categories calculate the product count and add it to the child category array.

Then reassign sCategories variabel to the the view

In your template you can use the new array key which you had added and have the products count.

I hope this will help you.

Regards

Ahmad

A hint 

You can use the same Smarty funnction in s/Frontend/Bare/frontend/index/sidebar-categories.tpl

And convert it to php fmunction and use it in your plugin to loop throug all the child categories tree array