【问题标题】:Sub category listing in MagentoMagento 中的子类别列表
【发布时间】:2013-11-29 17:58:24
【问题描述】:

我环顾四周,发现了很多关于提取父 ID 和子猫列表的信息,但这略有不同,我似乎无法找到答案,我还不是 PHP 专家(还) 所以对我放轻松。我尝试了各种方法,但最终都出现了错误。

我创建了一个 phtml 模板,用于在主导航下方的内联链接块中显示父类别的子类别。我用管理员的静态块调用这个模板,它工作正常,但是当我导航到子类别页面时,链接块消失了,显然是因为这段代码调用了父级的子猫,但当你实际上在子猫。这是我目前使用的代码:

<?php if (!Mage::registry('current_category')) return ?>
<?php $_categories = $this->getCurrentChildCategories() ?>
<?php $_count = is_array($_categories)?count($_categories):$_categories->count(); ?>
<?php if($_count): ?>
    <div class="category-products <?php echo Mage::getModel('catalog/layer')->getCurrentCategory()->getName(); ?>">
        <dl id="narrow-by-list2">
            <dt></dt>
            <dd>
                <ol class="subcat_list">
                <?php foreach ($_categories as $_category): ?>
                    <?php if($_category->getIsActive()): ?>
                    <li>
                        <a href="<?php echo $this->getCategoryUrl($_category) ?>"<?php if ($this->isCategoryActive($_category)): ?> class="current"<?php endif; ?>><?php echo $this->htmlEscape($_category->getName()) ?></a>
                    </li>
                    <?php endif; ?>
                <?php endforeach ?>
                </ol>
            </dd>
        </dl>
        <script type="text/javascript">decorateDataList('narrow-by-list2')</script>
    </div>
<?php endif; ?>

关于如何修改它以便在我实际查看子猫时列表保留在那里的任何想法? 非常感谢

【问题讨论】:

    标签: php magento categories


    【解决方案1】:

    环顾四周后,我终于偶然发现了我需要的东西 感谢 MagikSwapna 的投入,它帮助我更了解了一些事情

    我最终得到了这个

        <div class="category-products <?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('parent_cat_name')->toHtml() ?>">
    <?php  echo "<ol class='subcat_list'>"; ?>
    
     <?php
        $currentCat = Mage::registry('current_category');
    
        if ( $currentCat->getParentId() == Mage::app()->getStore()->getRootCategoryId() )
        {
            // current category is a toplevel category
            $loadCategory = $currentCat;
    
        }
        else
        {
            // current category is a sub-(or subsub-, etc...)category of a toplevel category
            // load the parent category of the current category
            $loadCategory = Mage::getModel('catalog/category')->load($currentCat->getParentId());
            // @TODO enhance for more nested category levels to display sub-categories
        }    
        $subCategories = explode(',', $loadCategory->getChildren());
    
        foreach ( $subCategories as $subCategoryId )
        {
            $cat = Mage::getModel('catalog/category')->load($subCategoryId);
    
            if($cat->getIsActive())
            {
                if($crcat == $cat->getName())                                                   //Check if current category is this subcategory
                    echo '<li><b><a href="'.$cat->getURL().'">'.$cat->getName().'</a></b>'.'</li>'; //If yes display it as bold (Currently Selected)
                else                                                                            //
                    echo '<li><a href="'.$cat->getURL().'">'.$cat->getName().'</a>'.'</li>';        //Otherwise display it as normal
            }
        }
    
    ?>
    <?php  echo "</ol>"; ?>
    </div>
    

    它工作正常,我创建了一个用于类别页面的自定义布局,如果在非类别相关页面中使用它会引发错误,但它可以工作! 最后。

    【讨论】:

      【解决方案2】:
      <?php $_helper = Mage::helper('catalog/category') ?>
          <?php $_categories = $_helper->getStoreCategories() ?>   
          <?php 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): ?>                                                                                                                                    
                               <?php foreach($_subcategories as $_subcategory): ?>
                                      <h3><a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>"><?php echo $_subcategory->getName() ?></a></h3>
                                      <!--sub sub category-->
                                      <?php $_subcategory = Mage::getModel('catalog/category')->load($_subcategory->getId()) ?>                             
                                      <?php $_subsubcategories = $_subcategory->getChildrenCategories() ?>
                                      <?php if (count($_subsubcategories) > 0): ?>
                                         <ul>
                                          <?php foreach($_subsubcategories as $_subsubcategory): ?>
                                             <li>
                                                <a href="<?php echo $_helper->getCategoryUrl($_subsubcategory) ?>"><?php echo $_subsubcategory->getName() ?></a>            
                                                <?php $_subsubsubcategory = Mage::getModel('catalog/category')->load($_subsubcategory->getId()) ?>                             
                                                <?php $_subsubsubcategories = $_subsubcategory->getChildrenCategories() ?>
                                                <?php if (count($_subsubsubcategories) > 0): ?>                                                                                            
                                                         <ul>
                                                           <?php foreach($_subsubsubcategories as $_subsubsubcategory): ?>
                                                             <li>
                                                               <a href="<?php echo $_helper->getCategoryUrl($_subsubsubcategory) ?>"><span><?php echo $_subsubsubcategory->getName() ?></span></a>
                                                             </li> 
                                                           <?php endforeach; ?>
                                                         </ul>                                                                                               
                                                <?php endif; ?>
                                             </li> 
                                          <?php endforeach; ?>
                                         </ul>                                   
                                     <?php endif; ?>
                                       <!--sub sub category-->                               
                               <?php endforeach; ?>                                                                                                                                                     
                          <?php endif; ?>
                          </li> 
                   <?php endforeach; ?> 
        </ul> 
          <?php endif; ?>
      

      【讨论】:

      • 太棒了,非常感谢,我现在正在尝试不同的排列方式。顺便说一句,我的网站在这里ashfordweb-dev.co.uk/proaudio/monitoring/active-monitors.html 我仍然不完全确定如何在其中显示父子类别的链接,我正在尝试使用 getIsActive()); ?> 但它不起作用
      • 有没有办法按字母顺序排序?
      猜你喜欢
      • 2023-03-16
      • 2014-04-02
      • 2011-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多