Magento2 在Magento 2中显示所有产品

Magento2 在Magento 2中显示所有产品,magento2,product,disable,Magento2,Product,Disable,我想显示所有产品,如果它是启用或禁用并不重要 用这个 $collection = $this->_productCollectionFactory->create(); $collection->addAttributeToSelect('*'); return $collection; 我只获得启用的产品请帮助我获得禁用的产品。找到了两个解决方案,请尝试第一个,如果它不适合您,那么您可以尝试第二个 您可以通过以下方式对您的收藏使用禁用库存检查: $produ

我想显示所有产品,如果它是启用或禁用并不重要

用这个

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

实际上,我已经在我的块文件中使用了它,然后返回它并在phtml文件中使用它。如何做到这一点@你用过这个模型吗<代码>\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory我已经用您提供的参数$productCollection->setFlag('has\u stock\u status\u filter',false)解决了这个问题;谢谢,请调查一下这个问题,你能告诉我如何获得仅限禁用的产品@Shoaib吗