2010-12-10 104 views
6

在magento如何从其产品ID获取每个产品的类别ID。从产品ID Magento类别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] - > getProduct() - > getCategoryIds();这将在一台服务器中返回类别标识,但不在另一台服务器中。任何想法? – Elamurugan 2010-12-11 10:23:52

+0

您是否尝试过重新索引服务器上的平面类别表?这种奇怪的东西通常与过时的(或损坏的)索引相关联。 – mcmil 2010-12-11 18:16:18

回答

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 2016-01-05 09:53:49

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

这样,当前的产品类别编号可以得到。

2

想,如果你想从目前的产品ID的所有类别ID,你可以从

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

它可以帮助你

相关问题