【发布时间】: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 文件来使用这个类。
有什么方法可以让数量显示在具有更好性能的类别页面上?
【问题讨论】: