I am trying to call helper function in override block

Whenever i am trying to add __construct in block is not loading.

Here is my code code of block

<?php
namespace Company\Module\Block\Adminhtml\Order\View;
class Items extends \Magento\Sales\Block\Adminhtml\Order\View\Items
{
    protected $_helper;
    public function __construct(
        \Company\Module\Helper\Data $helper
    ) {
        $this->_helper = $helper;
    }
    public function callhelper(){
        $this->_helper->somefuntion();
    }
}

Here is my code code of helper

<?php
namespace Company\Module\Helper;
class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
    public function somefuntion()
    {
        echo 'Hello';
    }
}
  • Copy the all constructor part from \Magento\Sales\Block\Adminhtml\Order\View\Items & past into your custom class (extended class Company\Module\Block\Adminhtml\Order\View\Items ;`) now add your helper class into the constructor it should work.. – Mohammad Mujassam Mar 12 '17 at 19:00
  • Unfortunately their is no constructor in vendor/magento/module-sales/Block/Adminhtml/Order/View/Items.php @MohammadMujassam – Aman Alam Mar 12 '17 at 23:17
  • did you define the overriding the class in di.xml ? – Mohammad Mujassam Mar 13 '17 at 6:46
up vote 1 down vote accepted

At first thank you for your helps. I have found way how can i call helper in override block in magento 2

In my case i have override block \Magento\Sales\Block\Adminhtml\Order\View\Items in my module and want to access helper function of my module.

First we need to check if their is constructor in main class i.e. \Magento\Sales\Block\Adminhtml\Order\View\Items.php in my case their is no constructor defined in Items.php so i have checked class Items from which it is extending and found that its extending from \Magento\Sales\Block\Adminhtml\Items\AbstractItems.php their is constructor defined i have copied that constructor in my override class i.e. Company\Module\Block\Adminhtml\Order\View\Items.php and set all original constructor arguments to parent constructor here is code

constructor code of \Magento\Sales\Block\Adminhtml\Items\AbstractItems class which is extended by \Magento\Sales\Block\Adminhtml\Order\View\Items.php

public function __construct(
    \Magento\Backend\Block\Template\Context $context,
    \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry,
    \Magento\CatalogInventory\Api\StockConfigurationInterface $stockConfiguration,
    \Magento\Framework\Registry $registry,
    array $data = []
) {
    $this->stockRegistry = $stockRegistry;
    $this->stockConfiguration = $stockConfiguration;
    $this->_coreRegistry = $registry;
    parent::__construct($context, $data);
}

constructor code of override block i.e. Company\Module\Block\Adminhtml\Order\View\Items.php

public function __construct(
    \Magento\Backend\Block\Template\Context $context,
    \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry,
    \Magento\CatalogInventory\Api\StockConfigurationInterface $stockConfiguration,
    \Magento\Framework\Registry $registry,
    \Company\Module\Helper\Data $helper,
    array $data = []
) {
    $this->_helper = $helper;
    parent::__construct($context, $stockRegistry, $stockConfiguration, $registry, $data);
}

Cleared cache and generations from var folder and its working fine.

Try replacing your block constructor with

public function __construct(
    \Magento\Backend\Block\Template\Context $context,
    \Company\Module\Helper\Data $helper,
    array data = []
) {
    $this->_helper = $helper;
    parent::__construct($context, $data);
}

Since you're ultimately extending \Magento\Sales\Block\Adminhtml\Items\AbstractItems you'll need to initiate that block for everything to load properly.

  • Thanks Michael but unfortunately its not working whenever i have updated my code according to your suggestion i am not able to open order view getting error "Exception occurred during order load". – Aman Alam Mar 12 '17 at 15:12

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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