【问题标题】:Magento: Display Level specific categories of currenty category treeMagento:显示当前类别树的特定类别
【发布时间】:2015-02-13 04:40:45
【问题描述】:

我正在尝试在我的 magento 商店中显示一个菜单,该菜单仅显示我所在的当前类别树的第 3 级类别。

我的类别树分为两条路径:

  • 默认
    • 出售
      • 项目 1
      • 第 2 项
    • 修复
      • 项目 1
      • 第 2 项

当我属于任何销售类别时,我只想显示第 3 级销售,反之亦然,当我属于任何维修类别时。

下面的代码显示了 3 级类别,但仅用于修复,无论我碰巧属于哪个类别。

<?php 
$layer = Mage::getSingleton('catalog/layer');

    /* @var $category Mage_Catalog_Model_Category */
    $categories = $layer->getCurrentCategory();


$categories = Mage::getModel('catalog/category')
          ->setStoreId(Mage::app()->getStore()->getId())                     
                     ->getCollection()
                     // magic is prepared here..
                     ->addAttributeToSelect('*')
                     // then the magic happens here:
                     ->addAttributeToFilter('level', array('eq' => 3))
                     ->load();
  ?>
    <?php foreach ($categories as $cat): ?>

谢谢。

模组

【问题讨论】:

    标签: php magento magento-1.7


    【解决方案1】:

    我通过使用 parentid 过滤找到了解决方案,这是我添加的代码:

    $_cat = new Mage_Catalog_Block_Navigation();
    $curent_cat = $_cat->getCurrentCategory();
    $curent_cat_id = $curent_cat->getId();
    
    
    $parentId=Mage::getModel('catalog/category')->load($curent_cat_id)->getParentId();
    

    然后我使用添加属性过滤器过滤 parentId:这仅显示第 3 级类别,它们是该类别的 parentid(根 ID)的子类别。 (格式编辑)

    $categories = Mage::getModel('catalog/category')
    
                         ->getCollection()
    
                         ->addAttributeToSelect('*')
    
                          ->addAttributeToFilter('level', array('eq' => 3))
                          ->addAttributeToFilter('parent_id', array('eq' => $parentId))
    
                         ->load();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-06
      • 1970-01-01
      • 1970-01-01
      • 2013-10-27
      • 1970-01-01
      相关资源
      最近更新 更多