在类别页面 Magento 2 上显示数量
Display quantity on category page Magento 2
我想在分类页面上显示产品数量。我试过 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 文件以使用此 class。
有什么方法可以让数量显示在分类页面上,性能更好?
正确的做法是获取页面上所有产品的数量
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;
}
}
我想在分类页面上显示产品数量。我试过 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 文件以使用此 class。
有什么方法可以让数量显示在分类页面上,性能更好?
正确的做法是获取页面上所有产品的数量
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;
}
}