簡體   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