0
  • I'm not sure if this is an m2 2.2 bug or just due the way I'm trying to filter the collection, but I can't seem to include disabled products in my product list collection.
  • I've tried a few different options from what I've seen in a few forum posts.
  • The below code snippet is purely to retrieve disabled products (as a first step, though the aim is to have enabled and disabled products).
  • This code results in zero products listed despite having disabled products which meet the criteria. Trying here in case there's something glaringly obvious I'm missing or a known bug with m2 2.2 requiring a workaround.

           $collection = $this->productCollectionFactory->create();
    //$collection->setVisibility($this->catalogProductVisibility->getVisibleInCatalogIds());
    
    $collection = $this->_addProductAttributesAndPrices($collection)
        ->addStoreFilter()
        ->addAttributeToFilter('status', \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_DISABLED)
        ->setCurPage($this->getRequest()->getParam($this->getData('page_var_name'), 1));
    
    $conditions = $this->getConditions();
    $conditions->collectValidatedAttributes($collection);
    $this->sqlBuilder->attachConditionToCollection($collection, $conditions);
    
    return $collection;
    

My widget is extended from: class ProductsList extends \Magento\CatalogWidget\Block\Product\ProductsList

1 Answer 1

1

Try this code:

$collection = $this->productCollectionFactory->create()
    ->addAttributeToSelect('*') //all attributes
    ->addAttributeToFilter('status', \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_DISABLED);

//print all collection
foreach($collection as $product){
   echo $product->getName(). "<br>";
}

Ref

3
  • Hi @Adarsh - thanks for this. I'd tried this already and it only outputs one product, when I know there are other disabled products it should output.
    – bsod99
    Commented Sep 27, 2018 at 23:21
  • I can confirm it is working in my instance. Showing me all the disabled products. Commented Sep 27, 2018 at 23:35
  • Ok it may be specific to my instance in that case - thanks, will check it out locally!
    – bsod99
    Commented Sep 27, 2018 at 23:41

Your Answer

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

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