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?

3 Answers 3

4

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;
    }
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();
0
1

Just add below line before return $product statement.

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

Then return $product;

It would work !!!

Your Answer

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

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