0

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?

1 Answer 1

0

You need to add some additional select attribute for that. Check magento default how they add. For example:

vendor/magento/module-catalog/Block/Product/AbstractProduct.php


/**
 * Add all attributes and apply pricing logic to products collection
 * to get correct values in different products lists.
 * E.g. crosssells, upsells, new products, recently viewed
 *
 * @param \Magento\Catalog\Model\ResourceModel\Product\Collection $collection
 * @return \Magento\Catalog\Model\ResourceModel\Product\Collection
 */
protected function _addProductAttributesAndPrices(
    \Magento\Catalog\Model\ResourceModel\Product\Collection $collection
) {
    return $collection
        ->addMinimalPrice()
        ->addFinalPrice()
        ->addTaxPercents()
        ->addAttributeToSelect($this->_catalogConfig->getProductAttributes())
        ->addUrlRewrite();
}

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.