0

How to get only disabled product In Controller I have getting all the products

<?php
namespace Mageplaza\HelloWorld\Controller\Index;

use Magento\Catalog\Model\Product\Attribute\Source\Status;
use Magento\Catalog\Model\ResourceModel\Product\Action;

class Index extends \Magento\Framework\App\Action\Action
{
protected $_pageFactory;
private $scopeConfig;
protected $_productCollectionFactory;
private $productAction;

public function __construct(
Action $productAction,
\Magento\Framework\App\Action\Context $context,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\Framework\View\Result\PageFactory $pageFactory,
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
){
$this->productAction = $productAction;
$this->_pageFactory = $pageFactory;
$this->scopeConfig = $scopeConfig;
$this->_productCollectionFactory = $productCollectionFactory;
return parent::__construct($context);
}

public function execute()
{   
$product_ids = [];

$onlydisproduct = $this->_productCollectionFactory->create();
$onlydisproduct->addAttributeToSelect('*');
$onlydisproduct->setFlag('has_stock_status_filter', false);
foreach ($onlydisproduct as $key => $value) {
    $product_ids[] = $value->getId();
}
//print_r($product_ids);




die('aaaa');
//return $this->_pageFactory->create();
}
}

Product Screenshot

enter image description here I am applying $onlydisproduct->setFlag('has_stock_status_filter', false); but still getting all products.

  • what's your magento version? – Rohan Hapani Aug 21 at 9:46
  • do you want disable product or out of stock product? – Dhiren Vasoya Aug 21 at 9:47
  • If you need disabled you bneed that : ->addAttributeToFilter('status', \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_DISABLED) – Claims Aug 21 at 9:53
  • My magento version Magento ver. 2.3.5-p1 @RohanHapani – Prits Aug 21 at 9:54
  • I want Disabled Product @DhirenVasoya – Prits Aug 21 at 9:54
0

Replace this below code in your controller and cache clean :

        $product_ids = [];
        $onlydisproduct = $this->_productCollectionFactory->create()
            ->addAttributeToSelect('*')
            ->addAttributeToFilter('status', \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_DISABLED);
        foreach ($onlydisproduct as $key => $value) {
            $product_ids[] = $value->getId();
        }
        echo "<pre/>";
        print_r($product_ids);
        exit;
| improve this answer | |
  • It gives me an empty array @Rohan – Prits Aug 21 at 10:05
  • Did you get product collection? or have you disabled products in your product list? – Rohan Hapani Aug 21 at 10:17
  • How do i check for product collection on var_dump it gives me Allowed memory size of 792723456 bytes exhausted @Rohan – Prits Aug 21 at 10:39
  • print_r($onlydisproduct->getData()); just do it. – Rohan Hapani Aug 21 at 10:41
  • Did you get collection? @Prits – Rohan Hapani Aug 21 at 10:49
0

Replace your code like this one :

$onlydisproduct = $this->_productCollectionFactory->create();
$onlydisproduct->addAttributeToSelect('*');
$onlydisproduct->addAttributeToFilter('status', \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_DISABLED);
| improve this answer | |
  • It gives me an empty array @Dhiren – Prits Aug 21 at 10:08
  • Kindly check that you have some disable product into system. – Dhiren Vasoya Aug 21 at 10:14
  • Yes i have disabled product in my system @Dhiren – Prits Aug 21 at 10:33
  • Then above code must work, have you check by caching and indexing commands. – Dhiren Vasoya Aug 21 at 10:38
  • Flushed all cached and indexer done but still no success@Dhiren – Prits Aug 21 at 10:49

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.