显示Magento 2中的所有产品

时间:2020-07-09 08:04:34

标签: magento2 product disable

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

与此

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

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

1 个答案:

答案 0 :(得分:2)

找到了两个解决方案,请尝试第一个,如果对您不起作用,则可以尝试第二个。

您可以通过以下方式在收藏中使用禁用库存检查:

$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();