久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

從 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 更改產品頁面標題以包含屬性)
主站蜘蛛池模板: 成人欧美一区二区三区在线观看 | 欧美日韩最新 | 午夜欧美 | 99re在线视频观看 | 成人毛片在线视频 | 欧美高清视频一区 | 欧美一级网站 | 久久久夜色精品亚洲 | 三级黄色片在线观看 | 中文字幕视频在线观看 | 欧美精品一区二区三区在线四季 | 国产精品一区二区久久久久 | 国产视频久久 | 神马久久av | 伦理午夜电影免费观看 | 欧美在线一区二区三区 | www.天天操.com | 中文字幕国产精品 | 亚洲a在线视频 | 国产最好的av国产大片 | 新超碰97 | 久久久久国产精品 | 91热爆在线观看 | 亚洲成人一二三 | 久久精品中文字幕 | 亚洲国产精品人人爽夜夜爽 | 中文字幕乱码一区二区三区 | 国产精品日韩欧美一区二区 | 日韩欧美一区二区三区免费观看 | 一二三区av | 成人免费视频 | 精品日韩在线 | a级免费观看视频 | 国产视频1区| 亚洲精品欧美一区二区三区 | 91九色在线观看 | av一区二区三区四区 | 国产视频在线一区二区 | 盗摄精品av一区二区三区 | 国产午夜精品视频 | 成人在线视频免费播放 |