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

Rutvee Sojitra
  • 3,881
  • 2
  • 17
  • 55
bsod99
  • 165
  • 1
  • 8

1 Answers1

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

Adarsh Khatri
  • 8,360
  • 2
  • 25
  • 58
  • 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 Sep 27 '18 at 23:21
  • I can confirm it is working in my instance. Showing me all the disabled products. – Adarsh Khatri Sep 27 '18 at 23:35
  • Ok it may be specific to my instance in that case - thanks, will check it out locally! – bsod99 Sep 27 '18 at 23:41