2010-12-10 14 views
6

Magentoでは、各製品のカテゴリIDを製品IDから取得する方法。MagentoカテゴリID製品ID

$items = $request->getAllItems(); 
    $c   = count($items); 

    for ($i = 0; $i < $c; $i++) { 
     if ($items[$i]->getProduct() instanceof Mage_Catalog_Model_Product) { 

      if ($items[$i]->getProduct()->getId()) { 
       $this->_dhlAllowed = false; 
       } 
     } 
    } 

ここで$items[$i]->getProduct()->getId()は製品IDを返します。カテゴリIDが必要です。

+2

$アイテム[$ i]を助けるかもしれないあなたは、現在の製品IDからすべてのカテゴリIDをしたい場合は、

Mage::registry('current_product')->getCategoryIds(); 

から得ることができたと - > getProduct() - > getCategoryIds();これは、あるサーバーではカテゴリIDを返しますが、別のサーバーではカテゴリIDを返しません。何か案が? – Elamurugan

+0

サーバーでフラットカテゴリテーブルのインデックスを再作成しようとしましたか?この種の奇妙なことは、古くなった(または壊れた)索引と関連しているのが普通です。 – mcmil

答えて

6
public function getProductCategory() { 
    /* @var $product Mage_Catalog_Model_Product */ 
    $product = Mage::registry('current_product'); 
    if ($product->getId()) { 
     $categoryIds = $product->getCategoryIds(); 
     if (is_array($categoryIds) and count($categoryIds) >= 1) { 
      return Mage::getModel('catalog/category')->load($categoryIds[0]); 
     }; 
    } 
    return false; 
} 
+1

'count($ categoryIds)'は '> = 1'にする必要があります – aki

2
Mage::registry('current_product')->getCategoryId(); 

このようにして、現在の製品のカテゴリIDを取得することができます。

2

それはあなた

関連する問題