【问题标题】:Magento display subcategory of specific current parent category PHPMagento 显示特定当前父类别 PHP 的子类别
【发布时间】:2014-08-22 22:22:57
【问题描述】:

我正在尝试在我的商店中展示品牌。我同时销售男装和女装,所以我有两个独立的品牌类别。

我的分类结构如下:

男士 品牌 x品牌 女性 品牌 品牌

我想展示产品的品牌。

我找到了这段代码:

$children = Mage::getModel('catalog/category')->getCategories(10);
foreach ($children as $category) {
    echo $category->getName();
}

但是,它仅适用于一个类别,并显示父类别中的所有子类别,而不是产品所拥有的类别。

如何修改它以显示当前品牌类别的子类别。

我希望这是有道理的,我很感激任何帮助!

【问题讨论】:

  • 您需要在商店的哪个位置找到它??
  • 如果你想在产品页面上做,试试这个 $_category_detail=Mage::registry('current_category'); echo $_category_detail->getName();
  • 嘿,我正在尝试在目录查看页面上执行此操作。感谢您的意见!

标签: php magento categories


【解决方案1】:

显示给定类别的子类别列表

$_category = Mage::registry('current_category');

$subcategories = Mage::getModel('catalog/category')->getCategories($_category->getId());

foreach ($subcategories as $subcategory){
     print_r($subcategory->getData()
}

查看更多@Magento: Display sub-category list

【讨论】:

    【解决方案2】:

    老问题已经过时了,但我想我会插话,因为这个问题没有得到回答,其他人可能会觉得这很有用..

    所以,首先,这段代码依赖于产品列表循环,即:

    $_productCollection=$this->getLoadedProductCollection();
    foreach ($_productCollection as $_product) {
        *... your product list output here ...*
    }
    

    其次,此代码取决于使用“品牌”作为 Magento 根类别的子类别(无论它被称为什么)的设置,并且每个品牌名称都被设置为“品牌”的子类别子猫。您可能需要在下面的代码中增加该猫的名称或更改您的类别结构以匹配我的。

    因此,在 app/design/frontend/your-package/your-theme/template/catalog/product/list.phtml 中已经设置的“ProductCollection”循环中,您可以将此 sn-p 以呼应品牌名称。

    $categories = $_product->getCategoryCollection()
                           ->addAttributeToSelect('name')
                           ->addAttributeToFilter('is_active', array('eq' => 1));
    
    foreach($categories as $category) {
        $catID = $category->getId();
        $catParent = Mage::getModel('catalog/category')->load($catID)
                                                       ->getParentCategory();
        if ( $catParent->getName() == 'BRANDS' ) {
            echo '<a href="'.$category->getUrl().'">'.$category->getName().'</a>';
        }
    }
    

    奖金编辑: 添加了代码以将品牌名称包装在品牌类别页面的链接中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-06
      相关资源
      最近更新 更多