How do I get categories for a product in Magento

后端 未结 6 2122
情书的邮戳
情书的邮戳 2021-01-31 16:12

I\'m trying to add product_type to my Magento Google Base output based on the product\'s categories, but I seem to be unable to. I have the following code:

// Ge         


        
相关标签:
6条回答
  • 2021-01-31 16:21

    This is utterly not tested..

    //load the product 
    
    $product = Mage::getModel('catalog/product')->load($productId);
    
    //load the categories of this product 
    
    $categoryCollection = $product->getCategoryCollection();
    

    You can then loop through $categoryCollection like through an array.

    source

    0 讨论(0)
  • 2021-01-31 16:22

    This code work in phtml file in Magento 2

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');//get current product
    $categories = $product->getCategoryIds(); /*will return category ids array*/  
    
    0 讨论(0)
  • 2021-01-31 16:27

    Rao's solution tries to load all the attributes which means lots of queries and joins but you only need a product_id to load it's categories. So you can do this:

      $product = Mage::getModel("catalog/product")->setId($productId);
      $categories = $product->getCategoryCollection();
    

    This will not load the product and will give you the categories.

    0 讨论(0)
  • 2021-01-31 16:32

    Using the source link dropped by Rao above I actually found a better answer:

    $product = Mage::getModel('catalog/product')->load($productId);
    
    $cats = $product->getCategoryIds();
    foreach ($cats as $category_id) {
        $_cat = Mage::getModel('catalog/category')->load($category_id) ;
        echo $_cat->getName();
    } 
    
    0 讨论(0)
  • 2021-01-31 16:41

    Get all Categories of the Product

    <?php
        $_Product = Mage::getModel("catalog/product")->load( PRODUCT_ID );
        $categoryIds = $_Product->getCategoryIds();//array of product categories
    
        foreach($categoryIds as $categoryId) {
          $category = Mage::getModel('catalog/category')->load($categoryId);
          $this->_setAttribute('product_type', $category->getName(), 'text' );
       } 
    ?>
    
    0 讨论(0)
  • 2021-01-31 16:42

    Want to point out that the solution by @Rao is the best if you have a product object to get category ID's from as it makes only one SQL call.

    If you just have category id's, you can do the following:

    $categories = Mage::getModel('catalog/category')
        ->getCollection()
        ->addAttributeToSelect('name') //you can add more attributes using this
        ->addAttributeToFilter('entity_id', array('in'=>array(1,2,3)));
    
    foreach($categories as $_cat){
        $holder[]= $_cat->getName();
    }
    

    Where array(1,2,3) are your categories. Note that the array has integers, not string values, I found that SQL can be picky about that.

    Also wanted to point out that solutions pulling one category at a time are very inefficient as it makes an SQL call for every iteration of the loop e.g.:

    foreach(..){
        Mage::getModel('catalog/category')->load($categoryId);
    }
    
    0 讨论(0)
提交回复
热议问题