0

我正在尝试制作“畅销产品”小部件。

小部件显示在所需位置,但不包含任何数据。在我尝试打印输出后,弹出此错误 - “致命错误:允许的内存大小为 792723456 字节已用尽(尝试分配 400560128 字节)”。我怀疑可能存在内存分配问题,所以我尝试分配更多的内存,例如 2G、4G,甚至 16G,但错误仍然存​​在。请帮助。这是我的代码-

<?php
namespace Aakash\TopSeller\Block\Widget;

class Bestsellerdproduct extends \Magento\Framework\View\Element\Template implements \Magento\Widget\Block\BlockInterface
{
    protected $_template = 'widget/bestsellerdproduct.phtml';

    protected $DEFAULT_PRODUCTS_COUNT = 10;
    protected $DEFAULT_IMAGE_WIDTH = 150;
    protected $DEFAULT_IMAGE_HEIGHT = 150;
    protected $_productsCount;
    protected $httpContext;
    protected $_resourceFactory;
    protected $_catalogProductVisibility;
    protected $_productCollectionFactory;
    protected $_imageHelper;
    protected $_cartHelper;
    public function __construct(
        \Magento\Catalog\Block\Product\Context $context,
        \Magento\Reports\Model\ResourceModel\Report\Collection\Factory $resourceFactory,
        \Magento\Reports\Model\Grouped\CollectionFactory $collectionFactory,
        \Magento\Reports\Helper\Data $reportsData,
        array $data = []
    ) {
        $this->_resourceFactory = $resourceFactory;
        $this->_collectionFactory = $collectionFactory;
        $this->_reportsData = $reportsData;
        $this->_imageHelper = $context->getImageHelper();
        $this->_cartHelper = $context->getCartHelper();
        parent::__construct($context, $data);
    }

    public function imageHelperObj(){
        return $this->_imageHelper;
    }
    /**
        get featured product collection
    */
    public function getBestsellerProduct(){
        $limit = $this->getProductLimit();

        $resourceCollection = $this->_resourceFactory->create('Magento\Sales\Model\ResourceModel\Report\Bestsellers\Collection')->setPageSize($limit);
        return $resourceCollection;
    }

    /**
        Get the configured limit of products
    */
    public function getProductLimit() {
        if($this->getData('productcount')==''){
            return $this->DEFAULT_PRODUCTS_COUNT;
        }
        return $this->getData('productcount');
    }
    public function getProductimagewidth() {
        if($this->getData('imagewidth')==''){
            return $this->DEFAULT_IMAGE_WIDTH;
        }
        return $this->getData('imagewidth');
    }

    public function getProductimageheight() {
        if($this->getData('imageheight')==''){
            return $this->DEFAULT_IMAGE_HEIGHT;
        }
        return $this->getData('imageheight');
    }
    public function getAddToCartUrl($product, $additional = [])
    {
        return $this->_cartHelper->getAddUrl($product, $additional);
    }

    public function getProductPriceHtml(
        \Magento\Catalog\Model\Product $product,
        $priceType = null,
        $renderZone = \Magento\Framework\Pricing\Render::ZONE_ITEM_LIST,
        array $arguments = []
    ) {
        if (!isset($arguments['zone'])) {
            $arguments['zone'] = $renderZone;
        }
        $arguments['zone'] = isset($arguments['zone'])
        ? $arguments['zone']
        : $renderZone;
        $arguments['price_id'] = isset($arguments['price_id'])
        ? $arguments['price_id']
        : 'old-price-' . $product->getId() . '-' . $priceType;
        $arguments['include_container'] = isset($arguments['include_container'])
        ? $arguments['include_container']
        : true;
        $arguments['display_minimal_price'] = isset($arguments['display_minimal_price'])
        ? $arguments['display_minimal_price']
        : true;

        $priceRender = $this->getLayout()->getBlock('product.price.render.default');

        $price = '';
        if ($priceRender) {
            $price = $priceRender->render(
                \Magento\Catalog\Pricing\Price\FinalPrice::PRICE_CODE,
                $product,
                $arguments
            );
        }
        return $price;
    }
}  

此方法显示错误 -

    public function getBestsellerProduct(){
        $limit = $this->getProductLimit();

        $resourceCollection = $this->_resourceFactory->create('Magento\Sales\Model\ResourceModel\Report\Bestsellers\Collection')->setPageSize($limit);
        return $resourceCollection;
    }
4

0 回答 0