pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

從 magento 菜單中刪除空類別

Remove empty categories from magento menu(從 magento 菜單中刪除空類別)
本文介紹了從 magento 菜單中刪除空類別的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我希望我的主菜單不包含任何空的類別.我已經使用

I want my main menu to not include any categories that are empty. I've done this for the layered navigation very easily in the relevant phtml file by using

$_category->getProductCount()

但是,對于導航菜單,我發現不可能那么容易地做到這一點(我看過 Prattski 的例子,但它看起來確實有點 OTT).

However, for the navigation menu, I'm finding it impossible to do this as easily (I have seen the Prattski example but it does seem rather OTT).

主菜單似乎是在Mage_Page_Block_Html_Topmenu.php 中構建的,特別是在_getHtml 函數中.這將獲取菜單中的所有子項,如果我嘗試類似 $child->getId() 的內容,我會得到類似 "category-node-36" 的內容.

The main menu seems to be built in Mage_Page_Block_Html_Topmenu.php, specifically in the function _getHtml. This gets all the children in the menu and if I try something like $child->getId(), I get something like "category-node-36".

看起來我離能夠使用 getProductCount() 不遠了,所以測試它是否大于零.

It doesn't seem like I'm too far from being able to use getProductCount() and so test if it's more than zero.

可以這樣做嗎?有人可以指點我嗎?

Is it possible to do this? Can somebody point me to how?

如果可以,我會用我的版本擴展課程.

If I can, I'll extend the class with my version.

推薦答案

我終于破解了它,盡管我遠不相信這是一個最佳解決方案.無論如何,我將描述我在這里所做的事情,希望有人可以使它更有效率.由于我對很多領域都不熟悉,我將逐一描述.很抱歉篇幅過長.

I finally cracked it although I'm far from convinced it's an optimum solution. Anyway, I'll described what I did here and hopefully somebody can make it more efficient. I'll give a blow-by-blow description as I was ufamiliar with quite a few areas. So apologies for the length.

正如我所說,至少在我的情況下,主菜單是通過 app/code/core/Mage/Page/Block/Html 中的 Topmenu.php 構建的,特別是方法 _getHtml.我絕對不想修改核心文件,所以我發現了如何通過新模塊擴展此方法.(如果您熟悉創建新模塊,可以跳過這一點.)

As I said, in my case at least, the main menu is built via Topmenu.php in app/code/core/Mage/Page/Block/Html, specifically the method _getHtml. I very definitely don't want to modify a core file so I found out how to extend this method via a new module. (You can skip this bit if you're familiar with creating new modules.)

我需要創建一個新模塊(下面我將其稱為 MYMOD).當我覆蓋核心 magento 頁面塊時,我必須創建新文件夾:app/code/local/MYMOD/Page 以及其中的兩個子文件夾 Block 等(我相信它們區分大小寫).并在阻止另一個子文件夾 Html 中.您可以看到這完全反映了 app/code/core/Mage 中的文件夾結構.

I needed to create a new module (I'll call it MYMOD below). As I'm overwriting the core magento page block, I had to create new folders: app/code/local/MYMOD/Page and in there two sub-folders, Block and etc (I believe they are case sensitive). And within Block another subfolder Html. You can see this is exactly mirroring the folder structure from app/code/core/Mage.

etc 文件夾在 config.xml 文件中保存新模塊的規范.這是我的樣子:

The etc folder holds the specification for the new module in a config.xml file. This is what mine looks like:

<?xml version="1.0" encoding="UTF-8"?>
<!-- The root node for Magento module configuration -->
<config> 
    <!-- 
        The module's node contains basic 
        information about each Magento module
    -->
    <modules>
        <!--
            This must exactly match the namespace and module's folder
            names, with directory separators replaced by underscores
        -->
        <MYMOD_Page>
            <!-- The version of our module, starting at 0.0.1 -->
            <version>0.0.1</version>
        </MYMOD_Page>
    </modules>

    <global>
        <blocks>
            <page>
                <rewrite>
                    <html_topmenu>MYMOD_Page_Block_Html_Topmenu</html_topmenu>
                </rewrite>
            </page>
        </blocks>
    </global>

</config>

您可以在其他地方找到原因和原因.

You can find out about the whys and wherefors of this elsewhere.

不幸的是(在我看來!),這并不是在 Magento 中指定新模塊所需要做的全部.您還必須在 app/etc/modules 中創建一個名為MYMOD_Page.xml"的文件.這只是告訴 Magento 關于你的模塊以及在哪里尋找它.我的看起來像這樣:

Unfortunately (in my opinion!), that's not all you have to do to specify a new module in Magento. You also have to create a file called "MYMOD_Page.xml" in app/etc/modules. This is just telling Magento about your module and where to look for it. Mine looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <MYMOD_Page>

            <!-- Whether our module is active: true or false -->
            <active>true</active>

            <!-- Which code pool to use: core, community or local -->
            <codePool>local</codePool>

        </MYMOD_Page>
    </modules>
</config>

好的,對于與模塊無關的說明,我很抱歉,但我確實喜歡獨立的逐一解釋.

OK, sorry for the irrelevant instructions on modules but I do like self-contained blow-by-blow explanations.

我現在可以在我的模塊中創建一個帶有子類的新文件,在該文件中我可以使用方法(函數)代替核心 Magento 的方法(函數).文件名必須與原始文件Topmenu.php 相同,并在app/code/local/MYMOD/Page/Block/Html 中.

I can now create a new file in my module with a subclass in which I can have methods (functions) that will be used instead of the core Magento ones. The file name has to be the same as the original file, Topmenu.php and it goes in app/code/local/MYMOD/Page/Block/Html.

記住,面向對象的結構意味著Topmenu.php原始核心版本中的所有功能都對我可用,我不必在我的新版本中復制它們(非常便于維護).我的 Topmenu.php 版本只需要包含我可能想要的任何新函數(在這種情況下我不需要任何函數)并重新聲明我想用我自己的版本覆蓋的任何函數.就我而言,我需要修改兩個函數:_getHtml 和 _getMenuItemClasses.

Remember, the object oriented structure means that all of the functions in the original core version of Topmenu.php are available to me, I don't have to copy them in my new version (great for maintainability). My version of Topmenu.php only has to contain any new functions I might want (in this case I didn't need any) and redeclare any functions I want to overwite with my own version. In my case, I needed to modify two functions: _getHtml and _getMenuItemClasses.

_getHtml 需要額外檢查,因此不包括任何空類別.

_getHtml needs additional checks so any empty categories aren't included.

_getMenuItemClasses 需要額外檢查,以便父"類不會添加到子項為空的類別中.

_getMenuItemClasses needs additional checks so that the class "parent" isn't added to categories whose children are empty.

這是我是怎么做的(有評論).我確信有更好的方法,但我對 Magento 還是很陌生.

Here's how I did it (with comments). I'm sure there are better ways but I'm still fairly new to Magento.

class MYMOD_Page_Block_Html_Topmenu extends Mage_Page_Block_Html_Topmenu
// Create my subclass, in accordance with how I've defined the new module
{
    /**
     * Recursively generates top menu html from data that is specified in $menuTree
     *
     * @param Varien_Data_Tree_Node $menuTree
     * @param string $childrenWrapClass
     * @return string
     */
    protected function _getHtml(Varien_Data_Tree_Node $menuTree, $childrenWrapClass)
    {
        $html = '';

        $children = $menuTree->getChildren();
        $parentLevel = $menuTree->getLevel();
        $childLevel = is_null($parentLevel) ? 0 : $parentLevel + 1;

        $counter = 1;
        $childrenCount = $children->count();

        $parentPositionClass = $menuTree->getPositionClass();
        $itemPositionClassPrefix = $parentPositionClass ? $parentPositionClass . '-' : 'nav-';

        foreach ($children as $child) {

            $child->setLevel($childLevel);
            $child->setIsFirst($counter == 1);
            $child->setIsLast($counter == $childrenCount);
            $child->setPositionClass($itemPositionClassPrefix . $counter);

            $outermostClassCode = '';
            $outermostClass = $menuTree->getOutermostClass();

            if ($childLevel == 0 && $outermostClass) {
                $outermostClassCode = ' class="' . $outermostClass . '" ';
                $child->setClass($outermostClass);
            }
            /*
             *  Find out if this category has any products. I don't know an easier way.
             *  The id of every child returned by getID is of the form "category-node-nnn"
             *  where nnn is the id that can be used to select the category details.
             *  substr strips everything leaving just the nnn.
             *  Then use getModel-> getCollection with a filter on the id. Although this looks
             *  like it will return many, obviously category ids are unique so in fact it only
             *  returns the category we're currently looking at.
             */
            $_gcategoryId = substr($child->getId(), 14, 6);
            $_gcategories = Mage::getModel('catalog/category')->getCollection()->addFieldToFilter('entity_id', array('eq', $_gcategoryId));
            foreach ($_gcategories as $_gcategory) {
                $_gcategoryCount = $_gcategory->getProductCount();
            }
            /*
             *  Now only include those categories that have products.
             *  In my case I also wanted to include the top level categories come what may.
             */
            if (($childLevel == 0) || ($_gcategoryCount > 0)) {

                $html .= '<li ' . $this->_getRenderedMenuItemAttributes($child) . '>';
                $html .= '<a href="' . $child->getUrl() . '" ' . $outermostClassCode . '><span>'
                    . $this->escapeHtml($child->getName()) . '</span></a>';

                if ($child->hasChildren()) {
                    if (!empty($childrenWrapClass)) {
                        $html .= '<div class="' . $childrenWrapClass . '">';
                    }
                    $html .= '<ul class="level' . $childLevel . '">';
                    $html .= $this->_getHtml($child, $childrenWrapClass);
                    $html .= '</ul>';

                    if (!empty($childrenWrapClass)) {
                        $html .= '</div>';
                    }
                }
                $html .= '</li>';
            }
            $counter++;
        }

        return $html;
    }
    /**
     * Returns array of menu item's classes
     *
     * @param Varien_Data_Tree_Node $item
     * @return array
     */
    protected function _getMenuItemClasses(Varien_Data_Tree_Node $item)
    {
        $classes = array();

        $classes[] = 'level' . $item->getLevel();
        $classes[] = $item->getPositionClass();

        if ($item->getIsFirst()) {
            $classes[] = 'first';
        }

        if ($item->getIsActive()) {
            $classes[] = 'active';
        }

        if ($item->getIsLast()) {
            $classes[] = 'last';
        }

        if ($item->getClass()) {
            $classes[] = $item->getClass();
        }

        if ($item->hasChildren()) {
            /*
             *  Don't just check if there are children but, if there are, are they all empty?
             *  If so, then the changes in _getHtml will mean none of them will be included
             *  and so this one has no children displayed and so the "parent" class is not appropriate.
             */
            $children = $item->getChildren(); // Get all the children from this menu category
            foreach ($children as $child) { // Loop over each child and find out how many products (see _getHtml)
                $_gcategoryId = substr($child->getId(), 14, 6);
                $_gcategories = Mage::getModel('catalog/category')->getCollection()->addFieldToFilter('entity_id', array('eq', $_gcategoryId));
                foreach ($_gcategories as $_gcategory) { // Remember, there's actually only one category that will match the child's id
                    $_gcategoryCount = $_gcategory->getProductCount();
                }
                if ($_gcategoryCount > 0) { // As soon as one child has products, then we have a parent and can stop looking
                    $classes[] = 'parent';
                    break;
                }
            }
        }

        return $classes;
    }

}

我希望這很清楚.它可以滿足我的要求(我的商店很小),但歡迎提出任何改進建議.

I hope this is clear. It does what I want (my store is small) but any suggestions for improvement welcome.

這篇關于從 magento 菜單中刪除空類別的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Override Magento Config(覆蓋 Magento 配置)
What would cause a print_r and/or a var_dump to fail debugging a variable?(什么會導致 print_r 和/或 var_dump 調試變量失敗?)
How to update custom options programatically in magento?(如何在 magento 中以編程方式更新自定義選項?)
Magento 404 on Admin Page(管理頁面上的 Magento 404)
Magento - get price rules from order(Magento - 從訂單中獲取價格規則)
Magento Change Product Page Titles to Include Attributes(Magento 更改產品頁面標題以包含屬性)
主站蜘蛛池模板: 定量包装机,颗粒定量包装机,粉剂定量包装机,背封颗粒包装机,定量灌装机-上海铸衡电子科技有限公司 | 威实软件_软件定制开发_OA_OA办公系统_OA系统_办公自动化软件 | 济南铝方通-济南铝方通价格-济南方通厂家-山东鲁方通建材有限公司 | 新疆十佳旅行社_新疆旅游报价_新疆自驾跟团游-新疆中西部国际旅行社 | 火锅底料批发-串串香技术培训[川禾川调官网] | 北京森语科技有限公司-模型制作专家-展览展示-沙盘模型设计制作-多媒体模型软硬件开发-三维地理信息交互沙盘 | 隐形纱窗|防护纱窗|金刚网防盗纱窗|韦柏纱窗|上海青木装潢制品有限公司|纱窗国标起草单位 | 电镀标牌_电铸标牌_金属标贴_不锈钢标牌厂家_深圳市宝利丰精密科技有限公司 | 激光内雕_led玻璃_发光玻璃_内雕玻璃_导光玻璃-石家庄明晨三维科技有限公司 激光内雕-内雕玻璃-发光玻璃 | 济南展厅设计施工_数字化展厅策划设计施工公司_山东锐尚文化传播有限公司 | 免联考国际MBA_在职MBA报考条件/科目/排名-MBA信息网 | 济南电缆桥架|山东桥架-济南航丰实业有限公司 | 科研ELISA试剂盒,酶联免疫检测试剂盒,昆虫_植物ELISA酶免试剂盒-上海仁捷生物科技有限公司 | 护腰带生产厂家_磁石_医用_热压护腰_登山护膝_背姿矫正带_保健护具_医疗护具-衡水港盛 | 岩棉板|岩棉复合板|聚氨酯夹芯板|岩棉夹芯板|彩钢夹芯板-江苏恒海钢结构 | 美国HASKEL增压泵-伊莱科elettrotec流量开关-上海方未机械设备有限公司 | 依维柯自动挡房车,自行式国产改装房车,小型房车价格,中国十大房车品牌_南京拓锐斯特房车 - 南京拓锐斯特房车 | 气动球阀_衬氟蝶阀_调节阀_电动截止阀_上海沃托阀门有限公司 | 德州网站开发定制-小程序开发制作-APP软件开发-「两山开发」 | 激光内雕_led玻璃_发光玻璃_内雕玻璃_导光玻璃-石家庄明晨三维科技有限公司 激光内雕-内雕玻璃-发光玻璃 | 立式矫直机_卧式矫直机-无锡金矫机械制造有限公司 | 北京三友信电子科技有限公司-ETC高速自动栏杆机|ETC机柜|激光车辆轮廓测量仪|嵌入式车道控制器 | 微量水分测定仪_厂家_卡尔费休微量水分测定仪-淄博库仑 | 【中联邦】增稠剂_增稠粉_水性增稠剂_涂料增稠剂_工业增稠剂生产厂家 | 二手光谱仪维修-德国OBLF光谱仪|进口斯派克光谱仪-热电ARL光谱仪-意大利GNR光谱仪-永晖检测 | 北京开源多邦科技发展有限公司官网| 天津拓展_天津团建_天津趣味运动会_天津活动策划公司-天津华天拓展培训中心 | 内窥镜-工业内窥镜厂家【上海修远仪器仪表有限公司】 | 高低温试验箱-模拟高低温试验箱订制-北京普桑达仪器科技有限公司【官网】 | 二次元影像仪|二次元测量仪|拉力机|全自动影像测量仪厂家_苏州牧象仪器 | 电加热导热油炉-空气加热器-导热油加热器-翅片电加热管-科安达机械 | PE一体化污水处理设备_地埋式生活污水净化槽定制厂家-岩康塑业 | 磁力抛光机_磁力研磨机_磁力去毛刺机_精密五金零件抛光设备厂家-冠古科技 | 数码听觉统合训练系统-儿童感觉-早期言语评估与训练系统-北京鑫泰盛世科技发展有限公司 | 柔性输送线|柔性链板|齿形链-上海赫勒输送设备有限公司首页[输送机] | 北京律师事务所_房屋拆迁律师_24小时免费法律咨询_云合专业律师网 | 广州昊至泉水上乐园设备有限公司| CXB船用变压器-JCZ系列制动器-HH101船用铜质开关-上海永上船舶电器厂 | 专业生物有机肥造粒机,粉状有机肥生产线,槽式翻堆机厂家-郑州华之强重工科技有限公司 | 施工围挡-施工PVC围挡-工程围挡-深圳市旭东钢构技术开发有限公司 | 减速机三参数组合探头|TSM803|壁挂式氧化锆分析仪探头-安徽鹏宸电气有限公司 |