在 Magento 2 中显示所有产品

Show All Product in Magento 2

我想显示所有产品,无论是启用还是禁用都没有关系。

有了这个

$collection = $this->_productCollectionFactory->create();
    $collection->addAttributeToSelect('*');
    return $collection;

我只获得启用的产品,请帮助我获得禁用的产品。

找到两个解决办法,请尝试第一个,如果它不适合你,那么你可以尝试第二个。

您可以通过以下方式在 collection 上使用禁用库存检查:

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

否则你可以使用这个:

$collection = $this->_productCollectionFactory->create()
                            ->addAttributeToSelect('*')
                            ->load();
            // Patch to alter load and get disabled products too
       $collection->clear();
            $where = $collection->getSelect()->getPart('where');
            foreach ($where as $key => $condition)
            {
                if(strpos($condition, 'stock_status_index.stock_status = 1') !== false){
                    $updatedWhere[] = 'AND (stock_status_index.stock_status IN (1,0))';
                } else {
                    $updatedWhere[] = $condition;
                }   
            }
            $collection->getSelect()->setPart('where', $updatedWhere);
            $collection->load();