I am injecting this collection into my model:

\Magento\Reports\Model\ResourceModel\Product\Collection $prodCollection

I use it to create an array of all products but I want to add the products category ids to the results.

I tried:

$prodItems = $this->prodCollection->addFieldToSelect('*')->addCategoryIds()->getData();

This doesn't include the category IDs in results. I am trying to avoid having to load the product through the product repository and calling the method to get categories on every single product as that would be a very expensive operation.

Whats the cheapest way to get the product categories?


1 Answer 1

    $productCollection = $this->_objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
    $productCollection->addAttributeToFilter('visibility', 4);

This works absolutely fine for me... I'm not sure what getData is supossed to do. I just go throught it with:

    foreach ($productCollection as $item) {

Your Answer

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

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