为了设计目的,我需要检索每个产品的产品类别路径。我能够使用目录/product/list.phtml中的以下代码检索类别路径并爆炸了第一个级别:

<?php $currentCat = Mage::registry('current_category'); ?>
<?php $exp = explode("/", $currentCat->getPath());?>

另一方面,我尝试在搜索结果中检索每个产品显示的类别路径,以便根据其主要类别为每个产品显示不同的颜色,但没有好的结果。

任何帮助将不胜感激。 brgds。

有帮助吗?

解决方案

以下代码将为您提供一个从根类别开始的数组(ID:1),然后将root类别存储,然后将其介绍到您的产品所在的类别。

$category = Mage::getModel('catalog/product')->load([id])->getCategory();
$path = explode('/', $category->getPath());

然而,这只是用于设计目的的“沉重”代码。您可能会更好地将产品收藏扩展在 Mage_Catalog_Block_Product_List 类,方法 _getProductCollection 加入 catalog_category_entity 表到集合查询并添加 path 字段到选择字段

其他提示

从根目录中获取路径

public function getCategoryPath($sku)
{
    $product = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);

    $pathArray = array();
    $collection1 = $product->getCategoryCollection()
        ->setStoreId(Mage::app()->getStore()->getId())
        ->addAttributeToSelect('path')
        ->addAttributeToSelect('is_active');


    foreach($collection1 as $cat1){            
        $pathIds = explode('/', $cat1->getPath());            
        $collection = Mage::getModel('catalog/category')->getCollection()
            ->setStoreId(Mage::app()->getStore()->getId())
            ->addAttributeToSelect('name')
            ->addAttributeToSelect('is_active')
            ->addFieldToFilter('entity_id', array('in' => $pathIds));

        $pahtByName = '';
        foreach($collection as $cat){                
            $pahtByName .= '/' . $cat->getName();
        }

        $pathArray[] = $pahtByName;

    }

    return $pathArray;
}

我使用以下代码在降落到网站外的产品页面时找到面包屑的产品类别。 (什么时候 if ($category = $this->getCategory()) 失败)

$cate 是最后一个类别产品,您可以使用类别功能来获取路径。

magento/app/code/core/mage/catalog/helper/data.php

如果来自类别以外的地方,则添加发现产品的位置

需求修复:TODO:在循环之前检查产品是否具有类别!

if(empty($path)) {
    if ($this->getProduct()) {
        $collection =  Mage::getModel('catalog/category')->getCollection()
            ->setStoreId(Mage::app()->getStore()->getId())
            ->addAttributeToSelect('name')
            ->addAttributeToSelect('is_active')
            ->addFieldToFilter('entity_id', array('in' => $this->getProduct()->getCategoryIds()));
        $categories = array();
        $cate = "";
        foreach ($collection as $category) {
            $cate = $category;
            $pathInStore = $category->getPathInStore();
            $pathIds = array_reverse(explode(',', $pathInStore));
        }
        $categories = $cate->getParentCategories();
        if($categories) {
        foreach($pathIds as $categoryId) {
                $path['category'.$categoryId] = array(
                    'label' => $categories[$categoryId]->getName(),
                    'link' => $this->_isCategoryLink($categoryId) ? $categories[$categoryId]->getURL() : ''
                );
        }}
    }
}

感谢您的回答,帮助我实现了这一目标。

许可以下: CC-BY-SA归因
scroll top