Andron777 Posted July 27, 2013 Share Posted July 27, 2013 Hello, I need help to finally figure this out. I am using PS. 1.5.4.1 All I want is to see how many products are in each category, and it will be really nice if this will work in the category tree (Front office) As an example i want to make smthing like this: - Computers (80) --- Laptops (55) --- Hardware (21) -------- DVD (11) -------- Video (10) --- Accessories (4 I searched this forum and Google and all i could find is a solution that counts products only for subcategories: BUT HOW TO MAKE IT COUNT FOR ALL CATEGORIES?? Solution i found and TESTED: in BLOCKCATEGORIES.php in "public function getTree" right after: if (!isset($resultIds[$id_category])) return false; (This code was added) ------------------------------------------------------------------------------------------------------ $ProductsCount = 0; $ProductsCount = (int)Db::getInstance()->getValue('SELECT COUNT(cp.`id_product`) AS total FROM `'._DB_PREFIX_.'product` p '.Shop::addSqlAssociation('product', 'p').' LEFT JOIN `'._DB_PREFIX_.'category_product` cp ON p.`id_product` = cp.`id_product` WHERE cp.`id_category` = '.$id_category. ' AND product_shop.`visibility` IN ("both", "catalog") AND product_shop.`active` = 1;' ); $return = array('id' => $id_category, 'link' => $this->context->link->getCategoryLink($id_category, $resultIds[$id_category]['link_rewrite']), 'name' => $resultIds[$id_category]['name'], 'desc'=> $resultIds[$id_category]['description'], 'children' => $children, 'products' => $ProductsCount); return $return; --------------------------------------------------------------------------------------------------------------- in category-tree-branch.tpl replace this code: <a href="{$node.link|escape:'htmlall':'UTF-8'}" {if isset($currentCategoryId) && $node.id == $currentCategoryId}class="selected"{/if} title="{$node.desc|escape:'htmlall':'UTF-8'}">{$node.name|escape:'htmlall':'UTF-8'} </a> With <a href="{$node.link|escape:'htmlall':'UTF-8'}" {if isset($currentCategoryId) && $node.id == $currentCategoryId}class="selected"{/if} title="{$node.desc|escape:'htmlall':'UTF-8'}">{$node.name|escape:'htmlall':'UTF-8'} ({$node.products})</a> ---------------------------------------------------------------------------------------------------------------------- The Above modification give this result: - Computers (0) --- Laptops (0) --- Hardware (0) -------- DVD (11) -------- Video (10) --- Accessories (0) BUT HOW TO MAKE IT COUNT FOR ALL CATEGORIES?? Thanks for help! Link to comment Share on other sites More sharing options...
PascalVG Posted July 28, 2013 Share Posted July 28, 2013 Do your higher categories have products directly coupled to them? E.g. In your shop, are there any products associated directly to the category "Hardware"? What you can do to make it work with the current code, is adding the parent category/-ies to the product as well, so that it will count them just at that level directly. The subcategories are then a sort of filters, with the parent categories a superset of all subcategories. Not sure if you like that idea though... So for example: Product DVD1 has both categories Hardware and DVD associated with it. In your tree, you then get Hardware 1 - DVD 1 Problem with counting products is: If hardware has (maybe) products on its own, products that come in Hardware AND one or more of it's subdirectory as well (we have to check for double/triple/... counting) and products that are only added to a subcategory of Hardware. That's a lot of checking to do, slowing down the loading/refreshing of the page considerably. So adding them (manually) to the parent category can be a fast alternative. Let me know if this works for you. Otherwise we have to start coding... pascal. Link to comment Share on other sites More sharing options...
Andron777 Posted July 28, 2013 Author Share Posted July 28, 2013 Well, yes I got you point. However i see dark in front of my eyes when I think that I have a lot of categories, with sub categories in them. And Now + I already have hundreds of products added. And it will take some time to go over them and select categories (main category. How ever for feature: In the admin panel > add product / edit > category tree I think with the help of JS ca be done if child category is selected AUTO select all parents category. This can save some time for feature added products. Link to comment Share on other sites More sharing options...
HustlaOwnz Posted April 3, 2014 Share Posted April 3, 2014 (edited) Hello, I need help to finally figure this out. I am using PS. 1.5.4.1 All I want is to see how many products are in each category, and it will be really nice if this will work in the category tree (Front office) As an example i want to make smthing like this: - Computers (80) --- Laptops (55) --- Hardware (21) -------- DVD (11) -------- Video (10) --- Accessories (4 I searched this forum and Google and all i could find is a solution that counts products only for subcategories: BUT HOW TO MAKE IT COUNT FOR ALL CATEGORIES?? Solution i found and TESTED: in BLOCKCATEGORIES.php in "public function getTree" right after: if (!isset($resultIds[$id_category])) return false; (This code was added) ------------------------------------------------------------------------------------------------------ $ProductsCount = 0; $ProductsCount = (int)Db::getInstance()->getValue('SELECT COUNT(cp.`id_product`) AS total FROM `'._DB_PREFIX_.'product` p '.Shop::addSqlAssociation('product', 'p').' LEFT JOIN `'._DB_PREFIX_.'category_product` cp ON p.`id_product` = cp.`id_product` WHERE cp.`id_category` = '.$id_category. ' AND product_shop.`visibility` IN ("both", "catalog") AND product_shop.`active` = 1;' ); $return = array('id' => $id_category, 'link' => $this->context->link->getCategoryLink($id_category, $resultIds[$id_category]['link_rewrite']), 'name' => $resultIds[$id_category]['name'], 'desc'=> $resultIds[$id_category]['description'], 'children' => $children, 'products' => $ProductsCount); return $return; --------------------------------------------------------------------------------------------------------------- in category-tree-branch.tpl replace this code: <a href="{$node.link|escape:'htmlall':'UTF-8'}" {if isset($currentCategoryId) && $node.id == $currentCategoryId}class="selected"{/if} title="{$node.desc|escape:'htmlall':'UTF-8'}">{$node.name|escape:'htmlall':'UTF-8'} </a>With <a href="{$node.link|escape:'htmlall':'UTF-8'}" {if isset($currentCategoryId) && $node.id == $currentCategoryId}class="selected"{/if} title="{$node.desc|escape:'htmlall':'UTF-8'}">{$node.name|escape:'htmlall':'UTF-8'} ({$node.products})</a>---------------------------------------------------------------------------------------------------------------------- The Above modification give this result: - Computers (0) --- Laptops (0) --- Hardware (0) -------- DVD (11) -------- Video (10) --- Accessories (0) BUT HOW TO MAKE IT COUNT FOR ALL CATEGORIES?? Thanks for help! I edited the /modules/blockcategories/ path's blockcategories.php and category-tree-branch.tpl file, but nothing changed with the categories box. It just displays the regular category names! I've been trying to figure this out FOR MONTHS! Prestashop needs to add this feature, it's such a basic tool that they should have!!! I have a custom theme... are there different files that I need to be editing??? Edited April 3, 2014 by HustlaOwnz (see edit history) Link to comment Share on other sites More sharing options...
PascalVG Posted April 3, 2014 Share Posted April 3, 2014 Hi Hustla, What PS version do you use?? (Oh, URL to site can be handy. Please add as well) pascal Link to comment Share on other sites More sharing options...
PascalVG Posted April 4, 2014 Share Posted April 4, 2014 Hustla, I can imagine that the theme has a folder: themes/<your theme folder>/modules/blockcategories/ where you should make the changes. Link to comment Share on other sites More sharing options...
HustlaOwnz Posted April 8, 2014 Share Posted April 8, 2014 Hi Hustla, What PS version do you use?? (Oh, URL to site can be handy. Please add as well) pascal PrestaShop™ 1.5.3.1 is the version. My shop's URL is www.PolarFusion.org I tried doing this in both the theme folder and the '/modules/blockcategories/ path's blockcategories.php and category-tree-branch.tpl file'. Nothing every changed Link to comment Share on other sites More sharing options...
PascalVG Posted April 9, 2014 Share Posted April 9, 2014 OK, try this: in themes/thgr00027/modules/blockcategories/category-tree-branch.tpl, at the top of the file (after the comment...): (make backup of the file, just in case) add red code: <li {if isset($last) && $last == 'true'}class="last"{/if}> <a href="{$node.link|escape:'htmlall':'UTF-8'}" {if isset($currentCategoryId) && $node.id == $currentCategoryId}class="selected"{/if} title="{$node.desc|escape:'htmlall':'UTF-8'}">{$node.name|escape:'htmlall':'UTF-8'} (<span id="catblock_product_amount">{$node.products}</span>)</a> Then in file: /modules/blockcategories/blockcategories.php (make backup, just in case...): In function getTree, add the red code: public function getTree($resultParents, $resultIds, $maxDepth, $id_category = null, $currentDepth = 0) { if (is_null($id_category)) $id_category = $this->context->shop->getCategory(); $children = array(); if (isset($resultParents[$id_category]) && count($resultParents[$id_category]) && ($maxDepth == 0 || $currentDepth < $maxDepth)) foreach ($resultParents[$id_category] as $subcat) $children[] = $this->getTree($resultParents, $resultIds, $maxDepth, $subcat['id_category'], $currentDepth + 1); if (!isset($resultIds[$id_category])) return false; // added the # of products per category, so we can display these in the category tree block $ProductsCount = 0; $ProductsCount = (int)Db::getInstance()->getValue('SELECT COUNT(cp.id_category) FROM '._DB_PREFIX_.'category_product cp, '._DB_PREFIX_.'product pr WHERE cp.id_category = '. $id_category .' AND cp.id_product = pr.id_product AND pr.active = 1' ); $return = array('id' => $id_category, 'link' => $this->context->link->getCategoryLink($id_category, $resultIds[$id_category]['link_rewrite']), 'name' => $resultIds[$id_category]['name'], 'desc'=> $resultIds[$id_category]['description'], 'children' => $children, 'products' => $ProductsCount); return $return; } That should do it. pascal Link to comment Share on other sites More sharing options...
kashyyyk Posted April 14, 2014 Share Posted April 14, 2014 This works great, but my categories that have subcategories don't have items, just the subcategories do so the category shows 0. Any what to have the subcategory totals add to the main catagory? Link to comment Share on other sites More sharing options...
PascalVG Posted April 15, 2014 Share Posted April 15, 2014 To add numbers of subcategories, add this : (Example code of 1.5.3.1, your code can differ a little) /modules/blockcategories/blockcategories.php: (make backup, just in case): public function getTree($resultParents, $resultIds, $maxDepth, $id_category = null, $currentDepth = 0) { if (is_null($id_category)) $id_category = $this->context->shop->getCategory(); $children = array(); if (isset($resultParents[$id_category]) && count($resultParents[$id_category]) && ($maxDepth == 0 || $currentDepth < $maxDepth)) foreach ($resultParents[$id_category] as $subcat) $children[] = $this->getTree($resultParents, $resultIds, $maxDepth, $subcat['id_category'], $currentDepth + 1); if (!isset($resultIds[$id_category])) return false; // added the # of products per category, so we can display these in the category tree block $ProductsCount = 0; $ProductsCount = (int)Db::getInstance()->getValue('SELECT COUNT(cp.id_category) FROM '._DB_PREFIX_.'category_product cp, '._DB_PREFIX_.'product pr WHERE cp.id_category = '. $id_category .' AND cp.id_product = pr.id_product AND pr.active = 1' ); $childrenProductsCount = $ProductsCount; foreach ($children as $child) $childrenProductsCount += $child['childrenproducts']; $return = array('id' => $id_category, 'link' => $this->context->link->getCategoryLink($id_category, $resultIds[$id_category]['link_rewrite']), 'name' => $resultIds[$id_category]['name'], 'desc'=> $resultIds[$id_category]['description'], 'children' => $children, 'products' => $ProductsCount, 'childrenproducts' => $childrenProductsCount); return $return; } This will add the amount of products of all child-categories to the counting. Now we have to change the template a little. In themes/<your theme folder>/modules/blockcategories/category-tree-branch.tpl (make backup!) add this: (example code of PS 1.5.3.1) <li {if isset($last) && $last == 'true'}class="last"{/if}> <a href="{$node.link|escape:'htmlall':'UTF-8'}" {if isset($currentCategoryId) && $node.id == $currentCategoryId}class="selected"{/if} title="{$node.desc|escape:'htmlall':'UTF-8'}">{$node.name|escape:'htmlall':'UTF-8'} (<i><span title="{l s='(products in this category / including subcategories)' mod='blockcategories'}">{$node.products}/{$node.childrenproducts}</span></i>)</a> {if $node.children|@count > 0} ... What it does is: It shows the amount of products in the current category and the amount of products including all products in its sub-categories: (And it gives some hint what it is when hovering over these numbers) N.B. If you only want the total of self + subcategories, change above to: <li {if isset($last) && $last == 'true'}class="last"{/if}> <a href="{$node.link|escape:'htmlall':'UTF-8'}" {if isset($currentCategoryId) && $node.id == $currentCategoryId}class="selected"{/if} title="{$node.desc|escape:'htmlall':'UTF-8'}">{$node.name|escape:'htmlall':'UTF-8'} (<i><span title="{l s='(products including those of subcategories)' mod='blockcategories'}">{$node.childrenproducts}</span></i>)</a> {if $node.children|@count > 0} ... Hope this helps, pascal Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now