0

I want the collection of products which are out of stock.

In my Magento instance, I used a multisource inventory with multiple stocks and sources.

How can I get this functionality with a custom module?

1
<?php
namespace YourCompanyName\YourModuleName\Block;

use Magento\Framework\View\Element\Template;

class YourCustomBlock  extends \Magento\Framework\View\Element\Template {

    protected $_productCollectionFactory;

    protected $_productVisibility;

   public function __construct(Template\Context $context,         
                               \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
                               \Magento\Catalog\Model\Product\Visibility $productVisibility,
                               array $data = [])
   {
       $this->_productCollectionFactory = $productCollectionFactory;
       $this->_productVisibility = $productVisibility;
       parent::__construct($context, $data);
   }


    public function getProductCollection() {

        $product = $this->_productCollectionFactory->create();
        $product->setVisibility($this->productVisibility->getVisibleInSiteIds());
        $product->addWebsiteFilter();
        $product->addStoreFilter();
        $product->addAttributeToSelect('*')->setFlag('has_stock_status_filter', true);
        $product = $product
            ->joinField('qty',
                'cataloginventory_stock_item',
                'qty',
                'product_id=entity_id',
                '{{table}}.stock_id=1',
                'left'
            )
            ->addAttributeToSelect('stock_status')
            ->addAttributeToSort ( 'entity_id', 'DESC' )
            ->addAttributeToFilter('status',\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
            ->load();
        return $product;
    }

}

or using object manager

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$collection = $productCollection->create();
$collection->setFlag('has_stock_status_filter', true);
$collection = $collection
->joinField('qty',
        'cataloginventory_stock_item',
        'qty',
        'product_id=entity_id',
        '{{table}}.stock_id=1',
        'left'
    )
    ->addAttributeToSelect('stock_status')
    ->load();

You should not use the ObjectManager directly check here!

0
$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(8)
->load();
  • Thanks for your support but it will give products which have default stock inventory, but in my case, there is multisource inventory. and it is not working for multisource inventory. I have updated the question. – Utsav Gupta Mar 27 at 12:36

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.