1

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

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

3 Answers 3

2

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

5
  • 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
    Commented Jan 22, 2018 at 16:22
  • Can you try to print sql using $collection->getSelect() to check what conditions are applying Commented Jan 22, 2018 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
    Commented Jan 22, 2018 at 18:39
  • When I run the first query in mysql it returns 51 rows.
    – Oylex
    Commented Jan 22, 2018 at 18:51
  • $collection->load(true); gave this output: pastebin.com/6CujUncN
    – Oylex
    Commented Jan 22, 2018 at 18:58
2

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.

0

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
sku,name,description,price

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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