【发布时间】:2020-10-29 18:31:30
【问题描述】:
我想显示所有产品,无论是启用还是禁用都没关系。
有了这个
$collection = $this->_productCollectionFactory->create();
$collection->addAttributeToSelect('*');
return $collection;
我只获得启用的产品,请帮助我获得禁用的产品。
【问题讨论】:
我想显示所有产品,无论是启用还是禁用都没关系。
有了这个
$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();
【讨论】:
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory