我试图在自定义模块中使用collectionFactory获取所有产品,如下所示:
$products = $this->_productCollectionFactory->create();
->addAttributeToSelect('*');
其中_productCollectionFactory
在__construct()
中初始化为
\Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
问题在于,如果没有应用任何过滤器,集合中的产品数量将小于我在Magento db中的产品数量。
从后端开始,我得到30.697个产品,但count($products)
仅产生22.000个产品。
我错过了什么?考虑哪些产品,哪些不?
答案 0 :(得分:1)
Magento会从集合中删除禁用的产品。即使没有应用过滤器。因此,您应该检查产品的状态。
就我而言,catalog_product_flat
索引存在问题,无法正确更新我的平面表。我设法通过运行bin/magento indexer:reindex catalog_product_flat
来解决此问题。
您可以通过以下行接收到由集合触发的SQL查询:
$products->getSelect()->assemble();
如果您的问题有不同的原因,这可能会对您有所帮助。
答案 1 :(得分:0)
我会说我们需要检查以下内容:
getProductCollection()
之前/之后是否有任何插件/观察器。 注意:如果您使用PHPStorm,请尝试安装Magento PHPStorm
插件。它将帮助您找出被覆盖的类和插件(如果有)。
As this example, you can check preferences or plugin of PHP file