Magento 2小组件-致命错误:允许的内存大小为792723456字节,已用尽(尝试分配400560128字节)

时间:2019-05-07 09:14:35

标签: php magento ubuntu-18.04 magento-2.3

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

窗口小部件显示在所需位置,但不包含任何数据。在尝试打印输出后,此错误弹出-“致命错误:允许的内存大小已用完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;
    }

0 个答案:

没有答案