I've been trying to figure this out for too many hours now…
How can I fetch every products from the database?
I've tried a lot of different method of getting the products, but all of them either don't work at all or only return products in stock.
This is what I'm using at the moment and it returns every products in stock (5 products out of the ~50 products that I can see in the admin):
$collection = $this->productCollectionFactory->create();
$collection->addAttributeToSelect('*');
$collection->load();
foreach ($collection->getItems() as $productItem) {
$product = $productItem->getData();
echo $product['name'].'<br>';
}
If it matters, my goal is to update these products (name, description, stock, price…).
Best Answer
You can apply
status
andvisibility
filter in collection to fetch all Products:You can check more on below links:
How to get a list of all products in Magento 2?
How to get only out of stock products in magento 2