Magento How to get the parent category of a subcategory from a product

Root Category (id: 1) - Apparel (id: 2) -- Shirts (id:4) -- Pants (id:5) - Accessories (id: 3) -- Handbags (id:6) -- Jewelry (id:7) 

In Magento, we can get product category identifiers using $productObj->getCategoryIds()

 $productObj = Mage::getModel('catalog/product')->load($product_id); $categoryIds = $productObj->getCategoryIds(); 

which will return an array of product category identifiers. I have a definite need to get the first level parent of a product category. Accepts, for example, the tree of categories above, if the product is classified in the Pants category, I want to get the first level category, which is Clothing (in this case, only the product is marked in the Trousers category, but not in the Clothing category).

Question: What method can I use to get the parent category of a subcategory, or can I get a first-level category from a product?

+4
source share
1 answer

You can use the catalog/category model and its getParentCategory() method:

 foreach ($categoryIds as $iCategoryId) { $m = Mage::getModel('catalog/category') ->load($iCategoryId) ->getParentCategory(); var_dump($m->debug()); } 
+9
source

Source: https://habr.com/ru/post/1397752/


All Articles