【问题标题】:Magento: Showing sibling categories in layered navigationMagento:在分层导航中显示同级类别
【发布时间】:2019-02-18 20:15:49
【问题描述】:

我想将同级类别添加到分层导航(只要客户是 已经一层)。

换句话说:假设我有一个名为“动物”的类别和分别名为“猫”、“狗”和“狮子”的子类别,如果客户点击“狮子”,我希望他们在“Shop by”中看到类别“猫和狗。

有人知道怎么做吗?

提前致谢。

【问题讨论】:

标签: magento


【解决方案1】:

这应该不会太难。下面的代码未经测试,但无论您将它放在前端的任何位置,它都应该可以工作。它的作用是让您访问同级类别的集合,但您需要弄清楚您将它放在模板中的哪个位置。

$parentId = Mage::registry('current_category')->getParentCategory()->getId();
$cats = Mage::getModel('catalog/category')->load($parentId)->getChildrenCategories();
foreach ($cats as $cat) {
  // first, skip current category.
  if ($cat->getId() == Mage::registry('current_category')->getId()) continue;
  // then do something with $cat
  echo $cat->getName().", ";
}

【讨论】:

    【解决方案2】:

    前往

    app/code/core/Mage/Catalog/Model/Layer/Filter/Category.php

    查找函数_getItemsData()

    见第 163 行:

    $categories = $categoty->getChildrenCategories();
    

    删除这一行并粘贴这些

    if(count($categoty->getChildrenCategories())){
        $categories = $categoty->getChildrenCategories();
    }else{
        $categories = $categoty->getParentCategory()->getChildrenCategories();
    }
    

    【讨论】:

    • @RehanMonbin 我尝试了您的解决方案,但在过滤所有其他子类别中,除了选定的具有getProductCount() 零的子类别。如何获得与父类别处于活动状态的完全相同的类别过滤器块?假设如果选择了动物类别,则类别为猫 (3)、狗 (5)、狮子 (4),然后如果选择了狗,则猫和狮子会显示具有正数的活跃链接。
    • 如果您选择此解决方案,请考虑覆盖
    【解决方案3】:

    这将检索同级类别的 id、url 和名称

    $currentCategory = $this->helper('catalog/data')->getCategory();
    $parentCategoryId = $currentCategory->parent_id;
    
    $siblingCategoryIds = array();
    $siblingCategories = array();
    
    foreach(Mage::getModel('catalog/category')->load($parentCategoryId)->getChildrenCategories() as $siblingCategory) {
        if ($siblingCategory->getIsActive()) {
            $siblingCategories[] = array($siblingCategory->getId(), array('name' => $siblingCategory->getName(), 'url' => $siblingCategory->getUrl()));
            $siblingCategoryIds[] = $siblingCategory->getId();
        }
    }
    
    $pos = array_search($currentCategory->getId(), $siblingCategoryIds);
    
    $nextPos = $pos+1;
    $prevPos = $pos-1;
    
    if (isset($siblingCategoryIds[$nextPos])) {
        $nextCategory = $siblingCategories[$nextPos];
    }
    else {
       $nextCategory = null;
    }
    
    if (isset($siblingCategoryIds[$prevPos])) {
        $prevCategory = $siblingCategories[$prevPos];
    }
    else {
       $prevCategory = null;
    }
    
    echo var_dump($prevCategory);
    echo var_dump($nextCategory);
    

    【讨论】:

      【解决方案4】:

      方法 app/code/core/Mage/Catalog/Block/Navigation.php::getCurrentChildCategories() 执行所需的操作并保持产品计数正确。您可以将其功能复制到您自己的助手中。

      【讨论】:

        猜你喜欢
        • 2016-10-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多