I am trying to get all the products from the collection(both in stock and out of stock) but with no luck at all. The setting on admin needs to hide the out of stock products from frontend but I want to show them in a specific page.

This is my code:

$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection');

This brings the in stock products only. How can I modify it to bring and the out of stock as well?


  • 1
    Did you find any solution? I am having the same problem.
    – Sarvagya
    Commented Jan 23, 2018 at 12:00

4 Answers 4


If it helps anyone, this is the only way I've managed to get all products, including out of stock products, in a loop for updating the stock values.

I needed to use ->getAllIds() to get an array of all of the products IDs, and then load each product individually rather than loop through the whole collection.

$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$productCollectionIds = $productCollection->create()->getAllIds();

foreach ($productCollectionIds as $productId) {
    try {
        $product = $objectManager->create('Magento\Catalog\Model\Product')->load($productId);
        // do actions with product
    } catch (Exception $e) {

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

$productCollection = $objectManager->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');

$collection = $productCollection->create() ->setFlag('has_stock_status_filter', true)->load() ->addAttributeToSelect('*') ->load(); print_r($collection->getData());

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

 public function getProductCollection()
    $collection = $this->_productCollectionFactory->create();
    $collection->setFlag('has_stock_status_filter', true)->load();
    $collection->setPageSize(3); // fetching only 3 products
    return $collection;

Use below code to filter "Out of Stock" product:

$productCollection =
->addAttributeToFilter('status', array('eq' =>
$productCollection->joinField( 'qty', 'cataloginventory_stock_item',
'product_id=entity_id', '{{table}}.stock_id=1');

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.