简体   繁体   English

Magento - 如何隐藏没有产品的类别和子类别

[英]Magento - How to hide categories and sub-categories with no products

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.我试图在 magento 中隐藏没有活动产品的前端的所有类别和子类别,但我不想使用管理后端手动执行此操作。 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 https://magento.stackexchange.com/a/80/6099

& Josh Prattski's blog post, & Josh Prattski 的博客文章,

http://prattski.com/2011/10/06/magento-module-hide-empty-categories/ 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.任何帮助将非常感激。

You can execute following sql to disable all categories without products. 您可以执行以下sql来禁用所有不带产品的类别。

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/ 您可以在这里找到更多详细信息http://quicktips.ru/all/hide-all-categories-without-products-and-show-categories-with-pr/

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: 函数_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;
}

To hide an empty category from Top menu, do the following: 要从“顶部”菜单隐藏空类别,请执行以下操作:

Go to app/code/core/Mage/Catalog/Block Folder, copy Navigation.php and Override Navigation.php in your local package. 转到app/code/core/Mage/Catalog/Block文件夹,在本地包中复制Navigation.php和Override Navigation.php

Open Navigation.php of your package and paste the following code in this file: 打开您包的Navigation.php并将以下代码粘贴到此文件中:

if ($category->getIsActive()) {
$cat = Mage::getModel('catalog/category')->load($category->getId());
$products = Mage::getResourceModel('catalog/product_collection')->addCategoryFilter($cat);
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);
Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($products);
if(count($products)==0)
return;
}

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... 我应该以以下所有内容作为序言,适用于magento 2.3,可能是2.x及更高版本...目前我正在研究2.3 ...

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... 我仍在努力解决存储库和工厂问题,以上所有我可以找到的关于代码的代码都没有使用存储库或工厂,即使在magento 2.3中,这似乎仍然是一个问题,所以我所做的只是两行如果您有多个商店,则可能需要修改mysql ...首先,禁用所有类别,然后启用具有任何产品的任何类别,要比其他方法容易得多,因此一行可以禁用其中的所有类别mysql shell或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 有一个地方没有为较新的magento更新的扩展名,有人在此stackexchange中遇到了问题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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM