Magento 2 Get out of stock Products


Magento 2 Get out of stock Products– We can use product collection and joinField to get the out of stock items. Here in this tutorial, we are going to explain how you can get out of stock items in Magento2.


Magento 2 Get out of stock Products Collection Query xample

You can get out of stock items in Magento 2 Simply as below-

Magento 2 Get out of stock Products Example:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$productCollectionFactory = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$productCollection = $productCollectionFactory
    ->create()
    ->addAttributeToSelect('*')
    ->addAttributeToSort('created_at', 'DESC')
    ->joinField('stock_item', 'cataloginventory_stock_item', 'qty', 'product_id=entity_id', 'qty=0')
    ->setPageSize(10)
    ->load();

The above example will give you list of items that are not in stock.


Advertisements

Add Comment

📖 Read More