Magento:从产品iD获取产品类别阵列

Magento:从产品iD获取产品类别阵列

问题描述:

我有一个商店,我需要在页面上的不同位置单独和单独回显产品类别,以便其他解决方案不起作用。我也想列出最多的孩子类别。Magento:从产品iD获取产品类别阵列

我推理出了这个过程,但我的编码技巧和Magento的结构知识并不好。

1)获取产品ID。 (已完成) 2)从产品ID获取类别数组。 3)获取两个变量的子类别的类别ID? 4)回声变量,我想要他们。

获取儿童的原因是这些类别在BRAND和CATEGORY父类别下。我只想显示这些标题的子女的品牌名称和实际类别。

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

+0

有来关于如何做到这一点的帖子。谷歌,StackOverflow和MagentoCommerce有很多例子。这里有一个来自SO:http://stackoverflow.com/questions/5232879/how-to-get-sub-categories-of-a-specific-parent-category – seanbreeden 2012-03-29 13:26:09

+0

重复问题 – ShaunOReilly 2012-03-29 22:56:21

+0

这回声在一起的类别。我的问题是,如何通过产品视图模板单独和单独回显它们,因为我需要在两个类别名称之间放置内容。 – James 2012-03-29 23:20:29

//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"); 

;)

+0

感谢您的回复Mike。 我在执行代码时遇到了一些问题。我使用$ productId = $ this-> getProduct() - > getId();获取产品ID,但是我只注意到由于某种原因,当我回显$ productId时,发生了某些情况,它不显示产品图像,页脚和一些内容。我已经知道$ productiD = $ this-> getProduct() - > getId();是问题。如果我只是用echo $ this-> getProduct() - > getId();它会显示ID没有问题,但我不能把它变成一个变量? 我很抱歉,因为我的PHP知识不是很强。 – James 2012-03-30 23:10:02

+0

你在做什么文件? – 2012-04-01 09:06:06

+0

嗨偏离主题对不起。你在哪里推荐开始学习经验丰富的PHP开发人员的magento?大多数教程只是吸 – Yang 2014-06-21 08:56:01

用于获取类别名称和标识的使用:

$_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 
+0

Downvoter,请解释。 – Ankur 2014-09-16 09:41:12

$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(); 
} 
+0

请编辑您的答案并重新设置它的格式以使代码可读,而不是为代码的每一行添加项目符号。 如果您想提供高质量的答案,请考虑添加更多细节。 – Neeku 2014-06-19 08:51:25