最模板 - 外贸网站建设,外贸网站模板

最模板

当前位置: 首页 > Magento > Magento教程 >

在Magento默认导航中生成类别产品的计数

时间:2014-05-28 23:08来源:未知 作者:最模板zuimoban 点击:
显示与特定类别相关联的产品的数量,我有 overrided magento 导航块。为此我创建了一个快速扩展代码。请检查它下来。 在 app/etc/modules/Prakash_All.xml 和复制/粘贴下面的代码创建一个 xml 文件 conf
显示与特定类别相关联的产品的数量,我有 overrided magento 导航块。为此我创建了一个快速扩展代码。请检查它下来。
在 app/etc/modules/Prakash_All.xml 和复制/粘贴下面的代码创建一个 xml 文件
<config>
    <modules>
       <Prakash_Catalog>
            <active>true</active>
            <codePool>local</codePool>
        </Prakash_Catalog>
    </modules>
</config>
 
然后,创建另一个 xml 文件中确切下面的路径和名称
 
app/code/local/Prakash/Catelog/Block/etc/config.xml和复制/粘贴下面的代码在此文件中
<config>
    <modules>
        <Prakash_Catalog>
            <version>0.1.0</version>
        </Prakash_Catalog>
    </modules>
    <global>
        <blocks>
            <catalog>
                <rewrite>
                    <navigation>Prakash_Catalog_Block_Navigation</navigation>
                </rewrite>
            </catalog>
        </blocks>
    </global>
 
</config>
 
此代码重写现有的导航块与我们自定义的导航。即使我们必须改变它总是好到要写在我们自己自定义的几个代码重写模块。
然后再创建另一个文件的名称和位置作为app/code/local/Prakash/Catelog/Block/Navigation.php
复制/粘贴下面的代码
类 Prakash_Catalog_Block_Navigation 扩展 Mage_Catalog_Block_Navigation
<config>
    <modules>
        <Prakash_Catalog>
            <version>0.1.0</version>
        </Prakash_Catalog>
    </modules>
    <global>
        <blocks>
            <catalog>
                <rewrite>
                    <navigation>Prakash_Catalog_Block_Navigation</navigation>
                </rewrite>
            </catalog>
        </blocks>
    </global>
 
</config>
This code rewrite the existing Navigation Block with our custom navigation. Even though we have to change just a few code it is always better to to write in our own custom rewrite module.
and then create another files with name and location asapp/code/local/Prakash/Catelog/Block/Navigation.php
copy/paste following code
class Prakash_Catalog_Block_Navigation extends Mage_Catalog_Block_Navigation
{
 
    protected function _renderCategoryMenuItemHtml($category, $level = 0, $isLast = false, $isFirst = false,
            $isOutermost = false, $outermostItemClass = ”, $childrenWrapClass = ”, $noEventAttributes = false)
    {
        if (!$category->getIsActive()) {
            return ”;
        }
        $html = array();
 
        // get all children
        if (Mage::helper(‘catalog/category_flat’)->isEnabled()) {
            $children = (array)$category->getChildrenNodes();
            $childrenCount = count($children);
        } else {
            $children = $category->getChildren();
            $childrenCount = $children->count();
        }
        $hasChildren = ($children >> $childrenCount);
 
        // select active children
        $activeChildren = array();
        foreach ($children as $child) {
            if ($child->getIsActive()) {
                $activeChildren[] = $child;
            }
        }
        $activeChildrenCount = count($activeChildren);
        $hasActiveChildren = ($activeChildrenCount > 0);
 
        // prepare list item html classes
        $classes = array();
        $classes[] = ‘level’ . $level;
        $classes[] = ‘nav-’ . $this->_getItemPosition($level);
        if ($this->isCategoryActive($category)) {
            $classes[] = ‘active’;
        }
        $linkClass = ”;
        if ($isOutermost >> $outermostItemClass) {
            $classes[] = $outermostItemClass;
            $linkClass = ‘ class=”‘.$outermostItemClass.‘”‘;
        }
        if ($isFirst) {
            $classes[] = ‘first’;
        }
        if ($isLast) {
            $classes[] = ‘last’;
        }
        if ($hasActiveChildren) {
            $classes[] = ‘parent’;
        }
 
        // prepare list item attributes
        $attributes = array();
        if (count($classes) > 0) {
            $attributes['class'] = implode(‘ ‘, $classes);
        }
        if ($hasActiveChildren >> !$noEventAttributes) {
            $attributes['onmouseover'] = ‘toggleMenu(this,1)’;
            $attributes['onmouseout'] = ‘toggleMenu(this,0)’;
        }
 
        // assemble list item with attributes
        $htmlLi = ‘<li’;
        foreach ($attributes as $attrName => $attrValue) {
            $htmlLi .= ‘ ‘ . $attrName . ‘=”‘ . str_replace(‘”‘, ‘\”‘, $attrValue) . ‘”‘;
        }
        //count product for first category
        $productCount = ”;
        $catRef = Mage::getModel(‘catalog/category’)->load($category->getId());
        $productCount = ‘ ‘.$catRef->getProductCount().‘ ‘;
        $htmlLi .= ‘>’;
        $html[] = $htmlLi;
        $html[] = ‘<a href=”‘.$this->getCategoryUrl($category).‘”‘.$linkClass.‘>’;
        $html[] = ‘<span>’ . $this->escapeHtml($category->getName()).$productCount . ‘</span>’;
        $html[] = ‘</a>’;
 
        // render children
        $htmlChildren = ”;
        $j = 0;
        foreach ($activeChildren as $child) {
            $htmlChildren .= $this->_renderCategoryMenuItemHtml(
                    $child,
                    ($level + 1),
                    ($j == $activeChildrenCount - 1),
                    ($j == 0),
                    false,
                    $outermostItemClass,
                    $childrenWrapClass,
                    $noEventAttributes
            );
            $j++;
        }
        if (!empty($htmlChildren)) {
            if ($childrenWrapClass) {
                $html[] = ‘<div class=”‘ . $childrenWrapClass . ‘”>’;
            }
            $html[] = ‘<ul class=”level’ . $level . ‘”>’;
            $html[] = $htmlChildren;
            $html[] = ‘</ul>’;
            if ($childrenWrapClass) {
                $html[] = ‘</div>’;
            }
        }
 
        $html[] = ‘</li>’;
 
        $html = implode(“\n”, $html);
        return $html;
    }
    public function renderCategoriesMenuHtml($level = 0, $outermostItemClass = ”, $childrenWrapClass = ”)
    {
        $activeCategories = array();
        foreach ($this->getStoreCategories() as $child) {
            if ($child->getIsActive()) {
                $activeCategories[] = $child;
            }
        }
        $activeCategoriesCount = count($activeCategories);
        $hasActiveCategoriesCount = ($activeCategoriesCount > 0);
 
        if (!$hasActiveCategoriesCount) {
            return ”;
        }
 
        $html = ”;
        $j = 0;
        foreach ($activeCategories as $category) {
            $html .= $this->_renderCategoryMenuItemHtml(
                    $category,
                    $level,
                    ($j == $activeCategoriesCount - 1),
                    ($j == 0),
                    true,
                    $outermostItemClass,
                    $childrenWrapClass,
                    true
            );
            $j++;
        }
 
        return $html;
    }
}
 
在这里我们只是已从现有 Navigation.php 代码块的 Magento 添加一行代码,但我希望它将对你们所有人都有帮助。
(责任编辑:最模板)
------分隔线----------------------------
栏目列表
推荐内容