2010-12-10 12 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]는 도움이 될 수 있습니다

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

에서 당신이 얻을 수있는 가정> getCategoryIds를(); 이것은 한 서버에서 카테고리 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를 얻을 수 있습니다. > getProduct() - - 당신이 현재 제품 ID 모든 카테고리 ID를 원하는 경우

2

은 당신에게