Answers:
最简单的方法是只重写Mage_Catalog_Block_Navigation
块:
=> 在_renderCategoryMenuItemHtml()
您要替换循环的方法中
foreach ($children as $child) {
if ($child->getIsActive()) {
$activeChildren[] = $child;
}
}
=>与此:
foreach ($children as $child) {
if ($child->getIsActive() && $this->_hasProducts($child->entity_id)) {
$activeChildren[] = $child;
}
}
=>并且类似地在方法中renderCategoriesMenuHtml()
替换代码
foreach ($this->getStoreCategories() as $child) {
if ($child->getIsActive()) {
$activeCategories[] = $child;
}
}
=>与此:
foreach ($this->getStoreCategories() as $child) {
if ($child->getIsActive() && $this->_hasProducts($child->entity_id)) {
$activeCategories[] = $child;
}
}
=>最后添加我们刚才在代码中使用的方法:
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;
}
请记住,该方法使用目录/类别模型分别检查每个类别。因此,如果您有许多类别,则可能需要重写代码以免遇到性能问题。在我们正在运行的较小商店中,这可以正常工作。
没有用于隐藏空类别的内置功能(但是您可以在管理区域->目录->管理类别中为每个类别手动选择“是否激活=否”)
这是Josh Prattski博客中一篇精彩文章的链接,他在其中撰写了有关如何为此目的进行扩展的分步演练。
要从“顶部”菜单隐藏一个空类别,请执行以下操作:
转到
app/code/core/Mage/Catalog/Block
文件夹并复制Navigation.php
。覆盖
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;
}
}
对我有用的是创建模板类别树并实现呈现类别树的条件函数:
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;
}
您可以执行以下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/
隐藏空类别所做的就是重写Mage_Catalog_Model_Resource_Category_Tree
load()
函数。我必须将产品集合与以下类别集合一起加入。
$collection = Mage::getResourceModel('catalog/product_collection');
Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($collection);
Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection);
$collection->getSelect()->join(
array('product_category' => Mage::getSingleton('core/resource')->getTableName('catalog/category_product_index')),
'product_category.product_id = e.entity_id',
array('')
);
$collection->getSelect()->where('product_category.category_id = '.$this->_table.'.entity_id');
$select->columns(array('product_count' => $collection->getSelectCountSql()));
在$arrNodes = $this->_conn->fetchAll($select);
此行之前添加此代码。
并在这种情况下包装此代码,该类也从前端和后端模型调用
if(!Mage::getSingleton('admin/session')->isLoggedIn())
我添加了额外的字段product_count,其中包含实际的有效产品数量。
我正在使用第3方模块在顶部菜单上显示类别,并且在呈现菜单时根据产品数量设置了条件。