1

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?

0
    $productCollection = $this->_objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');
    $productCollection->addAttributeToFilter('visibility', 4);
    $productCollection->addAttributeToSelect('description');
    $productCollection->addAttributeToSelect('name');
    $productCollection->addCategoryIds();
    $productCollection->load();

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, privacy policy and cookie policy

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