【问题标题】:Show All Product in Magento 2在 Magento 2 中显示所有产品
【发布时间】:2020-10-29 18:31:30
【问题描述】:

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

有了这个

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

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

【问题讨论】:

    标签: magento2 product disable


    【解决方案1】:

    找到了两个解决方案,请尝试第一个,如果它不适合您,那么您可以尝试第二个。

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

    $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 文件中使用它怎么做!@Shoaib
    • 你用过这个型号吗? \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory
    • 我已经用你提供的参数 $productCollection->setFlag('has_stock_status_filter', false);谢谢
    • 你能告诉我如何只获得禁用产品@Shoaib
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-14
    • 2019-09-22
    • 2014-04-04
    • 1970-01-01
    • 2012-04-23
    • 1970-01-01
    • 2019-08-26
    相关资源
    最近更新 更多