skip to Main Content

I’m trying to get all products using collectionFactory in a custom module like this:

$products = $this->_productCollectionFactory->create();
            ->addAttributeToSelect('*');

where _productCollectionFactory is initialized in the __construct() as an instance of

MagentoCatalogModelResourceModelProductCollectionFactory $productCollectionFactory,  

the problem is that, without any applied filter, the number of products in the collection is smaller than the products that I have in Magento db.

From the backend it results I have 30.697 products but a count($products) results in only 22.000 products.

What am I missing? Which products are considered and which not?

2

Answers


  1. Disabled products are removed from the collection by Magento itself. Even if there is no filter applied. So you should check the status of your products.

    In my case there was an issue with the catalog_product_flat index which didn’t update my flat tables correctly. I managed to fix that by running bin/magento indexer:reindex catalog_product_flat.

    You can receive the SQL query that is triggered by your collection with the following line:

    $products->getSelect()->assemble();
    

    This might helps you, if your problem has a different cause.

    Login or Signup to reply.
  2. I would say we need to check these things:

    1. Configuration -> Catalog Tab -> Inventory -> Display Out Of Stock configuration: it might be affected this issue.
    2. We need to check if there is any plugin/observer for before/after getProductCollection() function.

    Note: If you use PHPStorm, try to install Magento PHPStorm plugin. It will help you find out the overridden classes and plugin if any.

    As this example, you can check preferences or plugin of PHP file

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search