I'm trying to hide all the categories and sub-categories from frontend with no active products in magento but I don't want to use admin backend to do it manually. I want to display only those categories in top level navigation which have their product count greater than zero. I have tried previous solutions given on the below links:

https://magento.stackexchange.com/a/80/6099

& Josh Prattski's blog post,

http://prattski.com/2011/10/06/magento-module-hide-empty-categories/

None of the solutions are working for me and there's no way for me to know what am I doing wrong. Any help would be really appreciated.

有帮助吗?

解决方案 2

Best way is to create your own template category tree and implemnt condition to the function which render category tree:

foreach ($children as $child) {
    if ($child->getIsActive() && $this->_hasProducts($child->entity_id)) {
        $activeChildren[] = $child;
    }
}

Function _hasProducts:

    protected function _hasProducts($category_id) {
    $products = Mage::getModel('catalog/category')->load($category_id)
        ->getProductCollection()
        ->addAttributeToSelect('entity_id')
        ->addAttributeToFilter('status', 1)
        ->addAttributeToFilter('visibility', 4);
    return ( $products->count() > 0 )  ? true : false;
}

其他提示

You can execute following sql to disable all categories without products.

UPDATE `catalog_category_entity_int` AS `status`
INNER JOIN `eav_attribute` AS `attr` ON `attr`.`attribute_code` = 'is_active'
AND `attr`.`entity_type_id` = 3
AND `status`.`attribute_id` = `attr`.`attribute_id`
SET `status`.`value` = IF((SELECT COUNT(`index`.`product_id`)
    FROM `catalog_category_product_index` AS `index`
    WHERE `index`.`category_id` = `status`.`entity_id` GROUP BY `index`.`category_id`) > 0, 1, 0)
WHERE `status`.`store_id` = 0

More details you can find here http://quicktips.ru/all/hide-all-categories-without-products-and-show-categories-with-pr/

I should preface all of this with the below applies to magento 2.3, possibly 2.x and up... I am working on 2.3 at the moment...

I am still trying to grapple with repositories and factories, all the code above or that i can find regarding this does not use either repositories or factories, and this seems to still be an issue even in magento 2.3 so what I did was two lines in mysql you may have to modify if you have more than one store... first it is much easier to just disable all categories and then enable any category that has any products, than the other way around, so one line to disable all categories in tree on frontend in mysql shell or phpmyadmin...

update catalog_category_entity_int set catalog_category_entity_int.value = 0 where catalog_category_entity_int.store_id = 0 and catalog_category_entity_int.attribute_id = (select attribute_id from  eav_attribute where attribute_code = 'include_in_menu') or catalog_category_entity_int.attribute_id = (select attribute_id from  eav_attribute where attribute_code = 'is_active');

then turn active and include in menu any categories that have any products in stock with

update catalog_category_entity_int left join catalog_category_product on catalog_category_product.category_id = catalog_category_entity_int.entity_id left join cataloginventory_stock_item on cataloginventory_stock_item.product_id = catalog_category_product.product_id set catalog_category_entity_int.value = 1 where catalog_category_entity_int.store_id = 0 and cataloginventory_stock_item.qty > 0 and catalog_category_entity_int.attribute_id = (select attribute_id from  eav_attribute where attribute_code = 'include_in_menu') or cataloginventory_stock_item.qty > 0 and catalog_category_entity_int.attribute_id = (select attribute_id from  eav_attribute where attribute_code = 'is_active');

there is an extension somewhere that has not been updated for the newer magento, that someone experienced issues with from this stackexchange here https://magento.stackexchange.com/questions/36/hide-categories-with-no-active-products

two lines is simple and could even be made into a procedure.

this by no means should be a final solution, but should get the job done until this sort of thing is built into the core... one shouldn't have to code an extension for something simple like this...

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top