繁体   English   中英

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

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

我试图在 magento 中隐藏没有活动产品的前端的所有类别和子类别,但我不想使用管理后端手动执行此操作。 我只想在顶级导航中显示产品数量大于零的类别。 我已经尝试了以下链接中给出的先前解决方案:

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

& Josh Prattski 的博客文章,

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

没有一个解决方案对我有用,我也无法知道我做错了什么。 任何帮助将非常感激。

您可以执行以下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

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

最好的方法是创建自己的模板类别树,并为呈现类别树的函数创建条件:

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

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

要从“顶部”菜单隐藏空类别,请执行以下操作:

转到app/code/core/Mage/Catalog/Block文件夹,在本地包中复制Navigation.php和Override Navigation.php

打开您包的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;
}

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

我仍在努力解决存储库和工厂问题,以上所有我可以找到的关于代码的代码都没有使用存储库或工厂,即使在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');

然后将其激活,并在菜单中包括所有有库存产品的类别

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');

有一个地方没有为较新的magento更新的扩展名,有人在此stackexchange中遇到了问题https://magento.stackexchange.com/questions/36/hide-categories-with-no-active-products

两行很简单,甚至可以做成一个过程。

这绝不是最终的解决方案,而是应该完成工作,直到将这种东西构建到核心中为止……不必为这种简单的代码编写扩展...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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