【问题标题】:Display quantity on category page Magento 2在类别页面上显示数量 Magento 2
【发布时间】:2018-11-15 08:01:46
【问题描述】:

我想在类别页面上显示产品数量。我试过stockRegistry,它工作得很好。但是,在生产环境中运行时,对于产品数量较多的品类,会报错500或其他描述没有服务器响应的错误。我认为原因是stockRegistry 创建了太多导致问题的请求。

我的代码如下:

class ListProduct extends \Magento\Catalog\Block\Product\ListProduct {
    private $_stockRegistry;

    private $stockHelper;

    public function __construct(
        \Magento\Catalog\Block\Product\Context $context,
        \Magento\Framework\Data\Helper\PostHelper $postDataHelper,
        \Magento\Catalog\Model\Layer\Resolver $layerResolver,
        CategoryRepositoryInterface $categoryRepository,
        \Magento\Framework\Url\Helper\Data $urlHelper,
        \Magento\CatalogInventory\Api\StockRegistryInterface 
        $stockRegistry,
        Stock $stockHelper,
        array $data = []
    )
    {
        $this->_stockRegistry = $stockRegistry;
        $this->stockHelper = $stockHelper;

        parent::__construct(
            $context,
            $postDataHelper,
            $layerResolver,
            $categoryRepository,
            $urlHelper,
            $data
        );
    }


    public function getProductStock($id) {
        return $this->_stockRegistry->getStockItem($id)->getQty();
    }
}

当然,我已经更新了我的 XML 文件来使用这个类。

有什么方法可以让数量显示在具有更好性能的类别页面上?

【问题讨论】:

    标签: php magento magento2


    【解决方案1】:

    这样做的正确方法是获取页面上所有产品的数量

    namespace Example\CatalogInventory\Model\ResourceModel;
    
    use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
    
    class Stock extends AbstractDb
    {
        /**
         * {@inheritdoc}
         */
        protected function _construct()
        {
            $this->_init('cataloginventory_stock_item', 'item_id');
        }
    
        /**
         * Retrieve products quantities
         * Return array as key product id, value - qty
         */
        public function getProductsStockQty($productIds, $websiteId, $stockId = \Magento\CatalogInventory\Model\Stock::DEFAULT_STOCK_ID)
        {
            $select = $this->getConnection()->select()
                ->from($this->getMainTable(), ['product_id', 'qty'])
                ->where('product_id IN(?)', $productIds)
                ->where('stock_id=?', (int)$stockId)
                ->where('website_id=?', (int)$websiteId);
    
            return $this->getConnection()->fetchPairs($select);
        }
    }
    

    并在您的自定义块中使用它:

    class ListProduct extends \Magento\Catalog\Block\Product\ListProduct
    {
        /**
         * @var \Example\CatalogInventory\Model\ResourceModel\Stock
         */
        private $stock;
    
        /**
         * @var array
         */
        private $quantities;
    
        public function __construct(
            \Magento\Catalog\Block\Product\Context $context,
            \Magento\Framework\Data\Helper\PostHelper $postDataHelper,
            \Magento\Catalog\Model\Layer\Resolver $layerResolver,
            \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository,
            \Magento\Framework\Url\Helper\Data $urlHelper,
            \Example\CatalogInventory\Model\ResourceModel\Stock $stock,
            array $data = []
        ) {
            parent::__construct($context, $postDataHelper, $layerResolver, $categoryRepository, $urlHelper, $data);
            $this->stock = $stock;
        }
    
        public function getProductStock($productId)
        {
            if (!$this->quantities) {
                $this->quantities = $this->stock->getProductsStockQty(
                    $this->getLoadedProductCollection()->getLoadedIds(),
                    $this->_storeManager->getStore()->getWebsiteId()
                );
            }
    
            return $this->quantities[$productId] ?? 0;
        }
    }
    

    【讨论】:

    • 从未想过我们必须深入研究 SQL 查询,但这是一种更好的方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多