cancel
Showing results for 
Search instead for 
Did you mean: 

How to get disable products in product collection in magento2.2

   Did you know you can see the translated content as per your choice?

Translation is in progress. Please check again after few minutes.

How to get disable products in product collection in magento2.2

I am trying to get product collection with all enabled/disabled products. It works fine until I upgrade Magento to 2.2. Now It's not fetching disabled products in collection. Here is my code -

class Index extends \Magento\Framework\App\Action\Action
{
    protected $_productCollectionFactory;


    public function __construct(

        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
        \Magento\Framework\ObjectManagerInterface $objectmanager,

    ) {
        $this->_objectManager = $objectmanager;
        parent::__construct($context);
        $this->_productCollectionFactory = $productCollectionFactory;
    }


    public function execute()
    {

        $collection = $this->_productCollectionFactory->create()
                    ->addAttributeToSelect('*')
                    ->addAttributeToFilter('diamond_vendor', array('in'=>$vendor))
                    //->addAttributeToFilter('status', array('in'=>array(1,2)))
                    ->addAttributeToFilter('entity_id', array('nin'=>$notin_ids))
                    ->setPageSize($noOfProducts)
                    ->load();
    }
}

 

In this I also tried with  - 

$collection->getSelect()->where('stock_status_index.stock_status IN(1,0)');

But its not making any effect.

Plz help me in this. 

thx. 

4 REPLIES 4

Re: How to get disable products in product collection in magento2.2

You just run below code for getting all product list,

 

 

$productCollectionFactory = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
$collection = $productCollectionFactory->create();
$collection->addAttributeToSelect('*');

echo count($collection->getData()); 

I have checked inside magento 2.2.2 version and its working fine.

 

If Issue Solved, Click Kudos/Accept As solutions. Get Magento insight from
Magento 2 Blogs/Tutorial

Re: How to get disable products in product collection in magento2.2

Below code works for me -  

$collection = $this->_productCollectionFactory->create()
                    ->addAttributeToSelect('*')
                    ->addAttributeToFilter('entity_id', array('nin'=>$notin_ids))
                    ->setPageSize($noOfProducts)
                    ->load();
//Fix : Disabled product not coming in product collection in ver-Mage2.2.2 
$collection->clear();
$fromAndJoin = $collection->getSelect()->getPart('FROM');
foreach ($fromAndJoin as $key => $index) {
	if ($key == 'stock_status_index') {
		$index['joinType'] = 'left join';
	}
	$updatedfromAndJoin[$key] = $index;
}
$collection->getSelect()->setPart('FROM', $updatedfromAndJoin);

$where = $collection->getSelect()->getPart('where');
foreach ($where as $key => $condition) {
	if (strpos($condition, 'stock_status_index.stock_status = 1') == false) {
		$updatedWhere[] = $condition;
	}
}
$collection->getSelect()->setPart('where', $updatedWhere);

$collection->load();

Re: How to get disable products in product collection in magento2.2

We need to set flag after collection to get disable products from collection object like below

 

$productCollection = $this->_productFactory->create()->getCollection();
$productCollection->setFlag('has_stock_status_filter', false);

Re: How to get disable products in product collection in magento2.2

$productCollection = $this->_productFactory->create()->getCollection();
$productCollection->setFlag('has_stock_status_filter', false);