cancel
Showing results for 
Search instead for 
Did you mean: 

Not enough data in Product\CollectionFactory, how can I get the rest?

Not enough data in Product\CollectionFactory, how can I get the rest?

I need to get several info from each single product in the catalog, so I used the Magento\Catalog\Model\ResourceModel\Product\CollectionFactory to get a collection of products.

Iterating through them I notice I cannot get all the data related to each product.

E.g.

  • $product->getData('name') returns the Product Tile
  • $product->getData('qty') dosen't work
  • $product->getData('quantity_and_stock_status') returns 1 (just the number like a string)

In the last example $product->getData('quantity_and_stock_status'), if I load the product with Magento\Catalog\Model\Product, then instead of returning 1, I get an array with 2 values qty and is_in_stock, which is what I want.

 

Just a quick note, loading each product with `Magento\Catalog\Model\Product` is taking ages, so I'd avoid it.

 

Is it possible having all the info related to the product with the Product\CollectionFactory?

2 REPLIES 2

Re: Not enough data in Product\CollectionFactory, how can I get the rest?

You can use Magento\Catalog\Api\ProductRepositoryInterface

public function __construct(
        \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
    ) {
        $this->productRepository = $productRepository;
    }

 

Now get the product data by get($sku) method.

Check link by SKU, https://www.rakeshjesadiya.com/how-to-get-product-collection-by-product-sku-in-magento-2/

 

By Product Id load product object,

https://www.rakeshjesadiya.com/how-to-get-product-collection-by-product-id-in-magento-2/

If Issue Solved, Click Kudos/Accept As solutions. Get Magento insight from
Magento 2 Blogs/Tutorial

Re: Not enough data in Product\CollectionFactory, how can I get the rest?

Hey Rakesh,
Thanks for your reply.

Unfortunately, loading products one by one, will take ages for loading and processing a big catalog.
As far as you know, is there any way I can do get all the info of all the products with less DB calls as possible?