3

I'm using below code to get the product collection

<?php

namespace Vendor\Module\Block\Product;

use Magento\Framework\View\Element\Template;
use Magento\Catalog\Model\ResourceModel\Product\Collection;

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


        public function __construct(
                Template\Context $context
                , Collection $productFactory
                , array $data = []
    ) {
        $this->productFactory = $productFactory;
        parent::__construct ( $context, $data );
    }

    public function getFilterProducts() {

        $product = $this->productFactory->addAttributeToSelect ( '*' );

        $product->addAttributeToFilter ( 'visibility', array (
                'eq' => \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH 
        ) );
        $product->addAttributeToSort ( 'entity_id', 'DESC' );

        return $product;
    }

}

But above code gives me only in stock products, how do I include out of stock products in this collection?

4

By default magento filters only in stock products in collection. We have to set the in stock filter as false.

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

$collection = $productCollection->create()->setFlag('has_stock_status_filter', false)->load();
| improve this answer | |
  • Awesome!! Thanks @Sufyan Khot :) It worked! – Octopus May 15 '19 at 18:26
1

Use below code

$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'
    )->joinTable('cataloginventory_stock_item', 'product_id=entity_id', array('stock_status' => 'is_in_stock'))
    ->addAttributeToSelect('stock_status')
    ->load();

Or replace your function with below

public function getFilterProducts() {

        $product = $this->productFactory->create();
        $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'
            )->joinTable('cataloginventory_stock_item', 'product_id=entity_id', array('stock_status' => 'is_in_stock'))
            ->addAttributeToSelect('stock_status')
            ->addAttributeToSort ( 'entity_id', 'DESC' )
            ->addAttributeToFilter('status',\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
            ->load();
        return $product;
    }
| improve this answer | |
1

Just add below line before return $product statement.

$product->setFlag('has_stock_status_filter', true);

Then return $product;

It would work !!!

| improve this answer | |

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.