在类别页面Magento 2上显示数量

时间:2018-11-12 07:28:29

标签: php magento magento2

我想在类别页面上显示产品数量。我已经尝试过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文件以使用此类。

怎样才能使数量显示在具有更好性能的类别页面上?

1 个答案:

答案 0 :(得分: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;
    }
}