Get category id from product in Magento

No comments
As we all know, that a product can be associated with multiple categories and a category can also be associated with multiple products. So in some cases, we need to Get all the categories of a product by product id. By using below code you can get all the categories that are associated with the current product id.

Here is the code:

$product = Mage::getModel(catalog/product)->load($product_id);
$cats = $product->getCategoryIds();
foreach ($cats as $category_id) {
$_cat = Mage::getModel(catalog/category)->load($category_id) ;
echo $_cat->getName();

No comments :

Post a Comment