5

I need to be able to get the Category name and ID from a product and I am able to retrieve the ID doing the following:

$categoryID = $product->getCategoryCollection()->getFirstItem()->getId();

However If I try to get the name, It always returns null.

$categoryName = $product->getCategoryCollection()->getFirstItem()->getName();

How do I get the name?

I have tried loading the Category by ID but that seems to be returning null as well, I have checked in the admin screen the correct ID for the category is returned.

3 Answers 3

6

try it like this:

$product->getCategoryCollection()
    ->addAttributeToSelect('name')
    ->getFirstItem()
    ->getName();
0

Normally, this should work:

$categoryID = $product->getCategoryCollection()->getFirstItem()->getId();
$category = Mage::getModel('catalog/category')->load($categoryID);
$categoryName = $category->getName();

If it doesn't (as you write), I can't imagine why. However, have you tried this?

$categoryName = $product->getCategoryCollection()->addAttributeToSelect('name')->getFirstItem()->getName();
2
  • Thanks for the reply but still returns null. Seems weird I can get the ID but not the name Apr 13, 2016 at 22:40
  • If the category exists, at least the load()-method should return something. And $product->getCategoryCollection()->count() is > 0? Can you add the output of $product->getCategoryCollection()->addAttributeToSelect()->getFirstItem()->debug() to your question? Apr 14, 2016 at 7:17
0
/**
 * @param $product_id
 * @return array
 */
public function mc_getCategoryIds($product_id)
{
//       +++ get product the category id
    $registry = $this->mc_C['product'];
    $getObj = $this->mc_get_obj($registry);
    /**
     * @var \Magento\Catalog\Model\Product $getObj
     * \Magento\Catalog\Model\Product
     */
    $product = $getObj->load($product_id);

    /**
     * @var \Magento\Catalog\Model\Product $product
     * \Magento\Catalog\Model\Product
     */

    return $product->getCategoryIds();
}

/**
 * @param $product_id
 * @return mixed
 */
public function mc_getCategoryName($product_id)
{
    $cats = $this->mc_getCategoryIds($product_id);
    if(count($cats) ){
        $firstCategoryId = $cats[(count($cats)-1)];
        $registry = $this->mc_C['category'];
        $getObj = $this->mc_get_obj($registry);

        /**
         * @var \Magento\Catalog\Model\CategoryFactory $getObj
         */
        $_category = $getObj->create();
        /**
         * @var \Magento\Catalog\Model\CategoryFactory $_category
         */
        $data = $_category->load($firstCategoryId);
        /**
         * @var \Magento\Catalog\Model\CategoryFactory $data
         */
        return $data->getName();
    }
    return 0;
}

https://www.cnblogs.com/q1104460935/p/11237280.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.