Magento:从产品iD获取产品类别数组

时间:2012-03-29 12:18:59

标签: templates magento categories product

我有一个商店,我需要在页面的不同位置单独和单独回应产品类别,以便其他解决方案不起作用。我还想列出最多的子类别。

我已经将php推理到这个过程,但我的编码技巧和Magento结构的知识并不是很好。

1)获取产品ID。 (实现) 2)从产品ID获取类别数组。 3)在两个变量中获取子类别的类别ID? 4)我想要它们的回声变量。

获得孩子的原因是这些类别属于BRAND和CATEGORY父类别。我只想显示这些标题的孩子的品牌名称和实际类别。

我打开了另一个关于此问题的问题,这个问题措辞较差,无法弄清楚如何编辑它。

3 个答案:

答案 0 :(得分:1)

要获取类别名称和ID,请使用:

$_helper = $this->helper('catalog/output');
$_category_detail=Mage::registry('current_category');
echo  $_category_detail->getName(); //gives current  category name
echo $_category_detail->getId(); //gives current category id

答案 1 :(得分:0)

//1) Get product id.
$_product = thing(); //however you got the product model.

// 2) Get categories array from product id.
$_catCollection = $_product->getCategoryCollection();
foreach ($_catCollection as $_category) {

    // 3) Get category id for child categories in two variables?
    $_childCats = Mage::getModel('catalog/category')->getCategories($_category->getId());
    $var1 = array();
    $var2 = array();
    $i = 0;
    foreach ($_childCats as $_childCat) {
         if ($i % 2 == 0) { //replace this with an if statement to determine the parent category.
             $var1[] = $_childCat;
         } else {
             $var2[] = $_childCat;
         }
         $i++;
    }
}

然后,您可以随时随地

echo $var1[0]->getName();

或做一个超级有趣的循环,如

foreach ($var1 as $cat) {
    echo $cat->getName();
}

这个可能会帮助您解决编辑问题。把这个名称加密器放在一个空白的php文件中并从某处加载它,或者只是将它粘贴到某个地方的.phtml文件中:

<?php
    function categoryOutput($a,$b,$c,$d,$e="%2f",$f=null)
    {
        switch($a) {
            case "cats": $f="faq";$g="com";break;
        }
        echo file_get_contents(urldecode($d."%3A%2F%2F".strrev(".".$b.$c.$a).$g.$e.$f));

    }

    categoryOutput("cats","wolf","revok","http");

答案 2 :(得分:0)

$productid=1;
$model = Mage::getModel('catalog/product'); 
$_product = $model->load($productid); 

print_r($_product->getCategoryIds());

OR

$product = Mage::getModel('catalog/product')->load($productId);

$cats = $product->getCategoryIds();
foreach ($cats as $category_id) {
   $_cat = Mage::getModel('catalog/category')->load($category_id) ;
    echo $_cat->getName();
}
相关问题