【问题标题】:Magento: Adding child categories to left nav menuMagento:将子类别添加到左侧导航菜单
【发布时间】:2015-08-13 15:19:18
【问题描述】:

我正在使用 magento。这是加载我的左侧导航栏的代码。它将主要类别加载到 ol.我还想让它加载每个 li 中的子类别。我把 LOAD CHILD CATEGORIES HERE 我想知道获取和显示上述父类别的所有子类别的 php 代码。我找到了几个修复方法,但对 pHp 不太了解导致了一系列错误。

/**
 * Category left navigation
 *
 * @see Mage_Catalog_Block_Navigation
 */


if (!Mage::registry('current_category')) {
    //no current category selected so default to showing the Products category
    $cur_category=Mage::getModel('catalog/category')->load(51);
    $layer = Mage::getSingleton('catalog/layer');
    $layer->setCurrentCategory($cur_category);
}else{
    //current category selected so display the child categories for it
    $layer = Mage::getSingleton('catalog/layer');
    $_category = $layer->getCurrentCategory();
    $currentCategoryId= $_category->getId();

    if($currentCategoryId == 306){
        //best sellers is selected so show the Product category
        $cur_category=Mage::getModel('catalog/category')->load(51);
        $layer = Mage::getSingleton('catalog/layer');
        $layer->setCurrentCategory($cur_category);
    }
}

$_categories=$this->getCurrentChildCategories();
$_count = is_array($_categories)?count($_categories):$_categories->count();
if($_count):
?>
<div class="box layered-nav">
    <div class="head">
        <h3><?php echo $this->__('Browse By') ?> PRODUCT CATEGORY</h3>
    </div>
    <div class="border-creator">
        <div class="narrow-by">
            <dl id="narrow-by-list">
                <dd>
                    <ol>
                    <?php foreach ($_categories as $_category): ?>
                        <?php if($_category->getIsActive()): ?>
                        <li>                        
                            <a href="<?php echo $this->getCategoryUrl($_category) ?>"<?php if ($this->isCategoryActive($_category)): ?> class="active"<?php endif ?>><?php echo $this->htmlEscape($_category->getName()) ?></a>
                        **LOAD CHILD CATEGORIES IN HERE**
                        </li>
                        <?php endif; ?>
                    <?php endforeach ?>
                    </ol>
                </dd>
            </dl><script type="text/javascript">decorateDataList('narrow-by-list')</script>
        </div>
    </div>
</div>
<?php
endif; 
?>
<!-- [ends] .browse-by // -->

到目前为止我想出的最好的,我知道它并不多

<ul class="subnav">
<li><a></a>
</ul>

非常感谢任何帮助

【问题讨论】:

    标签: php magento nav


    【解决方案1】:

    检查以下代码

     <?php $_helper = Mage::helper('catalog/category'); 
     $_categories = $_helper->getStoreCategories(); 
     $currentCategory = Mage::registry('current_category') ;
     if (count($_categories) > 0): ?>
        <ul>
            <?php foreach($_categories as $_category): ?>
                <li>
                    <a href="<?php echo $_helper->getCategoryUrl($_category) ?>">
                        <?php echo $_category->getName() ?>
                    </a>
                    <?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
                    <?php $_subcategories = $_category->getChildrenCategories() ?>
                    <?php if (count($_subcategories) > 0): ?>
                        <ul>
                            <?php foreach($_subcategories as $_subcategory): ?>
                                <li>
                                    <a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>">
                                        <?php echo $_subcategory->getName() ?>
                                    </a>
                                </li>
                            <?php endforeach; ?>
                        </ul>
                    <?php endif; ?>
                </li>
            <?php endforeach; ?>
        </ul>
    <?php endif; ?>
    

    现在$_subcategories = $_category-&gt;getChildrenCategories() 获取所有子类别。

    【讨论】:

    • 感谢您抽出宝贵时间 chanz,我已经查看了代码,我了解其中的一部分,但不是全部。这是否解释了我提供的代码顶部的 If 语句,它根据页面加载不同的导航栏?我知道它问了很多,但你能告诉我我需要在这个页面的确切位置插入它吗?我已经尝试了我的最佳猜测,但它只拉入一个随机的 CMS 页面,而不是任何类别链接。
    【解决方案2】:

    感谢 Chanz,我使用了您的代码 sn-p 并最终想出了以下内容。花了一段时间,因为我有点慢,但你给了我我需要的东西。非常感谢。

    /**
     * Category left navigation
     *
     * @see Mage_Catalog_Block_Navigation
     */
    
    
    if (!Mage::registry('current_category')) {
        //no current category selected so default to showing the Products category
        $cur_category=Mage::getModel('catalog/category')->load(51);
        $layer = Mage::getSingleton('catalog/layer');
        $layer->setCurrentCategory($cur_category);
    }else{
        //current category selected so display the child categories for it
        $layer = Mage::getSingleton('catalog/layer');
        $_category = $layer->getCurrentCategory();
        $currentCategoryId= $_category->getId();
    
        if($currentCategoryId == 306){
            //best sellers is selected so show the Product category
            $cur_category=Mage::getModel('catalog/category')->load(51);
            $layer = Mage::getSingleton('catalog/layer');
            $layer->setCurrentCategory($cur_category);
        }
    }
    
    $_categories=$this->getCurrentChildCategories();
    $_count = is_array($_categories)?count($_categories):$_categories->count();
    if($_count):
    ?>
    <div class="box layered-nav">
        <div class="head">
            <h3><?php echo $this->__('') ?> PRODUCT CATEGORIES</h3>
        </div>
        <div class="border-creator">
            <div class="narrow-by">
                <dl id="narrow-by-list">
                    <dd>
                        <ol>
                        <?php foreach ($_categories as $_category): ?>
                            <?php if($_category->getIsActive()): ?>
                            <li>                        
                                <a href="<?php echo $this->getCategoryUrl($_category) ?>"<?php if ($this->isCategoryActive($_category)): ?> class="active"<?php endif ?>><?php echo $this->htmlEscape($_category->getName()) ?></a>
                                <?php $_subcategories = $_category->getChildrenCategories() ?>
                                <?php if (count($_subcategories) > 0): ?>
                                    <ul>
                                        <?php foreach($_subcategories as $_subcategory): ?>
                                        <?php if($_category->getIsActive()): ?>
                                        <li>
                                            <a href="<?php echo $this->getCategoryUrl($_subcategory) ?>"<?php if ($this->isCategoryActive($_subcategory)): ?> class="active"<?php endif ?>><?php echo $this->htmlEscape($_subcategory->getName()) ?></a>
                                            </a>
                                        </li>
                                        <?php endif; ?>
                                        <?php endforeach; ?>
                                    </ul>
                                <?php endif; ?>
                            </li>
                            <?php endif; ?>
                        <?php endforeach; ?>
                        </ol>
                    </dd>
                </dl><script type="text/javascript">decorateDataList('narrow-by-list')</script>
            </div>
        </div>
    </div>
    <?php
    endif; 
    ?>
    <!-- [ends] .browse-by // -->
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-12-09
      • 2017-08-05
      • 2014-06-23
      • 1970-01-01
      • 2017-05-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多