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.
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.
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();
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);
$productCollection = $this->_productFactory->create()->getCollection(); $productCollection->setFlag('has_stock_status_filter', false);