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 更改產品頁面標題以包含屬性)
主站蜘蛛池模板: 江门流水线|江门工作台|江门市伟涛行工业设备有限公司 | 净气型药品柜-试剂柜-无管道净气型通风柜-苏州毕恩思 | 膏方加工_丸剂贴牌_膏滋代加工_湖北康瑞生物科技有限公司 | 板框压滤机-隔膜压滤机-厢式压滤机生产厂家-禹州市君工机械设备有限公司 | 深圳市源和塑胶电子有限公司-首页 | 冷轧机|两肋冷轧机|扁钢冷轧机|倒立式拉丝机|钢筋拔丝机|收线机-巩义市华瑞重工机械制造有限公司 | 小程序开发公司-小程序制作-微信小程序开发-小程序定制-咏熠软件 | 直齿驱动-新型回转驱动和回转支承解决方案提供商-不二传动 | 广州迈驰新GMP兽药包装机首页_药品包装机_中药散剂包装机 | 无缝钢管-聊城无缝钢管-小口径无缝钢管-大口径无缝钢管 - 聊城宽达钢管有限公司 | 中式装修设计_全屋定制家具_实木仿古门窗花格厂家-喜迎门 | 数码听觉统合训练系统-儿童感觉-早期言语评估与训练系统-北京鑫泰盛世科技发展有限公司 | elisa试剂盒价格-酶联免疫试剂盒-猪elisa试剂盒-上海恒远生物科技有限公司 | 北京森语科技有限公司-模型制作专家-展览展示-沙盘模型设计制作-多媒体模型软硬件开发-三维地理信息交互沙盘 | 六维力传感器_三维力传感器_二维力传感器-南京神源生智能科技有限公司 | 滑板场地施工_极限运动场地设计_滑板公园建造_盐城天人极限运动场地建设有限公司 | 数年网路-免费在线工具您的在线工具箱-shuyear.com | 昆明化妆培训-纹绣美甲-美容美牙培训-昆明博澜培训学校 | 空气能暖气片,暖气片厂家,山东暖气片,临沂暖气片-临沂永超暖通设备有限公司 | 滑石粉,滑石粉厂家,超细滑石粉-莱州圣凯滑石有限公司 | 紫外线老化试验箱_uv紫外线老化试验箱价格|型号|厂家-正航仪器设备 | 校园气象站_超声波气象站_农业气象站_雨量监测站_风途科技 | 【铜排折弯机,钢丝折弯成型机,汽车发泡钢丝折弯机,线材折弯机厂家,线材成型机,铁线折弯机】贝朗折弯机厂家_东莞市贝朗自动化设备有限公司 | 预制直埋蒸汽保温管-直埋管道-聚氨酯发泡保温管厂家 - 唐山市吉祥保温工贸有限公司 | 青州开防盗门锁-配汽车芯片钥匙-保险箱钥匙-吉祥修锁店 | 北京公司注册_代理记账_代办商标注册工商执照-企力宝 | 吹塑加工_大型吹塑加工_滚塑代加工-莱力奇吹塑加工有限公司 | 冷水机-冰水机-冷冻机-冷风机-本森智能装备(深圳)有限公司 | 智能门锁电机_智能门锁离合器_智能门锁电机厂家-温州劲力智能科技有限公司 | 金现代信息产业股份有限公司--数字化解决方案供应商 | 偏心半球阀-电动偏心半球阀-调流调压阀-旋球阀-上欧阀门有限公司 | 深圳网站建设-高端企业网站开发-定制网页设计制作公司 | 免费个人pos机申请办理-移动pos机刷卡-聚合收款码办理 | 中国玩具展_玩具展|幼教用品展|幼教展|幼教装备展 | 安徽千住锡膏_安徽阿尔法锡膏锡条_安徽唯特偶锡膏_卡夫特胶水-芜湖荣亮电子科技有限公司 | AGV无人叉车_激光叉车AGV_仓储AGV小车_AGV无人搬运车-南昌IKV机器人有限公司[官网] | 电缆故障测试仪_电缆故障定位仪_探测仪_检测仪器_陕西意联电气厂家 | 蓝牙音频分析仪-多功能-四通道-八通道音频分析仪-东莞市奥普新音频技术有限公司 | 车充外壳,车载充电器外壳,车载点烟器外壳,点烟器连接头,旅行充充电器外壳,手机充电器外壳,深圳市华科达塑胶五金有限公司 | 大倾角皮带机-皮带输送机-螺旋输送机-矿用皮带输送机价格厂家-河南坤威机械 | 岩棉板|岩棉复合板|聚氨酯夹芯板|岩棉夹芯板|彩钢夹芯板-江苏恒海钢结构 |