显示Magento 2中的所有产品



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

用这个

$collection = $this->_productCollectionFactory->create();
$collection->addAttributeToSelect('*');
return $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();