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();

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...).

  • print_r(get_class_methods($product)); you will get list of methods . Accordingly you can think Jan 20 '18 at 2:31
  • You can update product name,description, stock etc by using csv also Jan 20 '18 at 4:14

You can apply status and visibility filter in collection to fetch all Products:

 * @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
 * @param \Magento\Catalog\Model\Product\Attribute\Source\Status $productStatus
 * @param \Magento\Catalog\Model\Product\Visibility $productVisibility
 * @throws \Magento\Framework\Exception\LocalizedException
public function __construct(
    \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
    \Magento\Catalog\Model\Product\Attribute\Source\Status $productStatus,
    \Magento\Catalog\Model\Product\Visibility $productVisibility
) {
    $this->productCollectionFactory = $productCollectionFactory;
    $this->productStatus = $productStatus;
    $this->productVisibility = $productVisibility;

 * @return \Magento\Framework\DataObject[]
 * @throws \Magento\Framework\Exception\LocalizedException
public function getProducts()
    /** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $collection */
    $collection = $this->productCollectionFactory->create();
    $collection->joinAttribute('status', 'catalog_product/status', 'entity_id', null, 'inner');
    $collection->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner');
    $collection->addAttributeToFilter('status', ['in' => $this->productStatus->getVisibleStatusIds()])
        ->addAttributeToFilter('visibility', ['in' => $this->productVisibility->getVisibleInSiteIds()]);

    return $collection->getItems();

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

  • I just tried your solution, but it doesn't work, there are no change in the result i'm getting (and no error either). It seems like the products are filtered to only return in stock items even before we get to filter the collection.
    – Oylex
    Jan 22 '18 at 16:22
  • Can you try to print sql using $collection->getSelect() to check what conditions are applying Jan 22 '18 at 18:15
  • Without your code added, it's SELECT e.* FROM catalog_product_entity AS e, with your code, see pastebin.com/TACnaje9
    – Oylex
    Jan 22 '18 at 18:39
  • When I run the first query in mysql it returns 51 rows.
    – Oylex
    Jan 22 '18 at 18:51
  • $collection->load(true); gave this output: pastebin.com/6CujUncN
    – Oylex
    Jan 22 '18 at 18:58

I found the solution with the help of Pankaj Pareek.

I just added $collection->setFlag('has_stock_status_filter', false); and now I get every products.


You can update product name,description, stock etc by using csv also, export product form admin

Login To Admin
Go To System => Export =>
Select Option As per Below and export product
Entity Type => Products

https://prnt.sc/i2yvjl https://prnt.sc/i2yvhb

Form CSV you can remove extra fields and you can update name ,description etc by using SKU In CSV Fileds like

  • Sorry, I want to do this automatically.
    – Oylex
    Jan 22 '18 at 16:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.