3

How can I get category collection data from product ID? I have tried using the following way but it's not working for me.

$product = $this->_productFactory->create()->getCollection()->load($pid);
$cats = $product->getCategoryIds();
if(count($cats) ){
    $firstCategoryId = $cats[0];
    $_category = $this->_categoryFactory->create()->getCollection()->load($firstCategoryId);
    return $_category->getName();
}
6
$product = $this->_productFactory->create()->load($pid);
$cats = $product->getCategoryIds();
if(count($cats) ){
    $firstCategoryId = $cats[0];
    $_category = $this->_categoryFactory->create()->load($firstCategoryId);
    return $_category->getName();
}
| improve this answer | |
  • 2
    You should use the CategoryRepository to get an existent category (see post of Giel Berkers). – Matthias Kleine Jun 2 '17 at 6:33
  • hi @Sohel, this code gives the category where product is not assigned. – jafar pinjar Oct 23 '19 at 7:01
  • You should use getCategoryCollection to request all categories one time, also, getCategoryCollection will cache result. – Petro Chaikivskyi Oct 2 at 15:26
10

Note that all above answers are not really the correct way to do it in Magento 2. Magento 2 introduced service contracts and data models as a new way to fetch this kind of data.

For example $model->load() is deprecated since 2.1, and collections ought to be replaced with repositories and it's uniform getList()-method that uses search criteria.

The Magento Way of fetching the categories according to a product ID would be:

// Get a product Data Model from the repository:
$product = $this->productRepository->getById($productId);

// Fetch the 'category_ids' attribute from the Data Model.
if ($categoryIds = $product->getCustomAttribute('category_ids')) {
    foreach ($categoryIds->getValue() as $categoryId) {
        $category = $this->categoryRepository->get($categoryId);
    }
}
| improve this answer | |
  • Can this work with multiple store views? I am getting all store views categories when I want the ones for current store. – harri Oct 24 '17 at 11:50
  • 1
    Magento\Catalog\Api\CategoryRepositoryInterface::get() has a second parameter $storeId that you can use to get the category for a specific store. – Giel Berkers Oct 24 '17 at 12:19
  • Thanks for the quick reply, do you think you could show me an example? ->get($categoryId, $storeId); ? – harri Oct 24 '17 at 12:38
  • How do you get a repository? I cannot find one for Magento\Widget\Model\Widget\InstanceFactory, for example. – Jānis Elmeris Nov 1 '17 at 17:19
5

You don't need to load the entire category which does too many sql queries.

/** @var \Magento\Catalog\Api\CategoryAttributeRepositoryInterface $this->categoryAttributeRepository */
$nameAttribute = $this->categoryAttributeRepository->get('name');

/** @var \Magento\Catalog\Model\ResourceModel\Category $categoryResource */
$categoryResource = $this->categoryResourceFactory->create();
$value = $categoryResource->getAttributeRawValue(
    $categoryId,
    $nameAttribute,
    $this->storeManager->getStore()
);

// Additionally for other types of attributes (select, multiselect, ..)
$category = $this->categoryFactory->create();
$category->setCategoryId($categoryId);
$category->setData('name', $value);

// Final value as seen on frontend
$mixed = $nameAttribute->getFrontend()->getValue($category);
| improve this answer | |
0

As you have using getCollection() to $this->_categoryFactory->create()->getCollection()->load($firstCategoryId); that $_category given a category collection instead of single category object

for your case,getting category name use from $_category add getFirstItem();

return   $_category->getFirstItem()->getName();
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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