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
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 runningbin/magento indexer:reindex catalog_product_flat
.You can receive the SQL query that is triggered by your collection with the following line:
This might helps you, if your problem has a different cause.
I would say we need to check these things:
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