4

Error When i load my Helper Class Magento2. it shows

1 exception(s): Exception #0 (Magento\Framework\Exception\RuntimeException): Type Error occurred when creating object: Wac\DropPin\Helper\Data

app/code/Wac/DropPin/view/frontend/templates/cart/item/default.phtml

 echo $this->helper('Wac\DropPin\Helper\Data')->productWeight('Test');

app/code/Wac/DropPin/Helper/Data.php

<?php
namespace Wac\DropPin\Helper;

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
        protected $_productRepository;

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,       
        \Magento\Catalog\Model\ProductRepository $productRepository,
        array $data = []
    )
    {
        $this->_productRepository = $productRepository;
        parent::__construct($context, $data);
    }



    public function productWeight($id)
    {
        return $id;
    }

}
  • What is the exact error you are getting? The posted error or some more lines there? – Sukumar Gorai Jun 27 at 8:59
  • Sir ,Please Check Screenshot – Shafeel Sha Jun 27 at 9:02
  • Did you run di compile? – Sukumar Gorai Jun 27 at 9:05
  • 1
    You have injected block context which is not needed. – Sukumar Gorai Jun 27 at 9:10
5

What about this

namespace Wac\DropPin\Helper;

use Magento\Framework\App\Helper\AbstractHelper;

class Data extends AbstractHelper
{

    /**
     * @param \Magento\Framework\App\Helper\Context $context
     */
    public function __construct(
        \Magento\Framework\App\Helper\Context $context
    ) {
        parent::__construct($context);
    }

    // custom method
}

Remember to di compile

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.