4

I want to show some extra data on product list page so I override the vendor\magento\module-catalog\Block\Product\ListProduct.php . For this I refer Add custom block on listing page : Magento2. It work.

But I need to call my helper function in my override block file. Created new file name ListProduct.php in app\code\Vendor\Module_Name\Block\Product

namespace Vendor\Module_Name\Block\Product;
 class ListProduct extends \Magento\Catalog\Block\Product\ListProduct
 {

  // I need to call my module helper function here:

   public function test(){

   }
 }

How can I call my helper function inside this block file. Thanks

7
  • plz show your helper file path so i can tell about it Commented Jul 9, 2016 at 4:35
  • My helper path is, app\code\Vendor\Mymodule\Helper\Data.php namespace Vendor\Mymodule\Helper; class Data extends \Magento\Framework\App\Helper\AbstractHelper { public function __construct( \Magento\Framework\App\Helper\Context $context, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig ) { parent::__construct($context); $this->_scopeConfig = $scopeConfig; } public function test(){ return "test"; } } I need to call test function on my block
    – Vigna S
    Commented Jul 9, 2016 at 4:36
  • plz show your helper path. Commented Jul 9, 2016 at 4:38
  • you have to extend helper function in your list.php file, right? Commented Jul 9, 2016 at 4:39
  • Please find my updated edit.
    – Vigna S
    Commented Jul 9, 2016 at 4:42

3 Answers 3

5

you can call your helper function as per below,

 namespace Vendor\Module_Name\Block\Product;
     class ListProduct extends \Magento\Catalog\Block\Product\ListProduct
     {
           public function __construct(
            \Magento\Catalog\Block\Product\Context $context,        
            \Magento\Framework\Data\Helper\PostHelper $postDataHelper,
            \Magento\Catalog\Model\Layer\Resolver $layerResolver,
            \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository,
            \Magento\Framework\Url\Helper\Data $urlHelper,
            \Vendor\Mymodule\Helper\Data $dataHelper,
            array $data = []
        ) {
            $this->dataHelper = $dataHelper;       
            parent::__construct(
                $context,
                $postDataHelper,
                $layerResolver,
                $categoryRepository,
                $urlHelper,
                $data
            );
        }

       public function test(){
            $this->dataHelper->getHelperFunction();
       }
    }

Remove var folder and refresh again.

4
  • It helped got some bug on var/generation/vendor\mymodule\Block\Product\ListProduct\Interceptor.php. So I deleted that folder and flushed cache again I refreshed the page then the bug rectified.
    – Vigna S
    Commented Jul 9, 2016 at 6:53
  • you have to remove var folder first and try again Commented Jul 9, 2016 at 7:01
  • Shall we delete the whole var folder? won't it create any issues I just flushed the cache alone, php bin/magento cache:flush
    – Vigna S
    Commented Jul 9, 2016 at 7:05
  • no you have to just delete whole var folder, all files are generated automatically when you can refresh page Commented Jul 9, 2016 at 7:07
2

You will have to overwrite the constructor of the parent class and add your extra class instance on to it something like:

<?php

namespace Vendor\Module_Name\Block\Product;

use Magento\Catalog\Block\Product\Context;
use Magento\Framework\Data\Helper\PostHelper;
use Magento\Catalog\Model\Layer\Resolver;
use Magento\Framework\Url\Helper\Data as UrlHelper;
use Vendor\Mymodule\Helper\Data as Helper;

use Magento\Catalog\Block\Product\ListProduct as MagentoListProduct

class ListProduct extends MagentoListProduct
{
    /**
     * @param Context $context
     * @param \Magento\Framework\Data\Helper\PostHelper $postDataHelper
     * @param \Magento\Catalog\Model\Layer\Resolver $layerResolver
     * @param CategoryRepositoryInterface $categoryRepository
     * @param \Magento\Framework\Url\Helper\Data $urlHelper
     * @param array $data
     */
    public function __construct(
        Helper $customHelper
        Context $context,
        PostHelper $postDataHelper,
        Resolver $layerResolver,
        CategoryRepositoryInterface $categoryRepository,
        UrlHelper $urlHelper,
        array $data = []
    ) {
        $this->customHelper = $customHelper;
        parent::__construct(
            $context,
            $postDataHelper,
            $layerResolver,
            $categoryRepository,
            $urlHelper,
            $data
        );
    }


    public function test()
    {
        $this->customHelper->yourCustomMethod();
    }
}

But if its only the helper you would like to use in the PHTML file you need not overwrite the ListProduct block you can use the following method as well.

First you can overwrite the template file using an custom theme.

In the new file you can get a instance of you helper using:

$customHelper = $this->helper('Vendor\Mymodule\Helper\Data');

And ofcourse you would need to clear the var folder either by

rm -rf ./var/*

or you can do it by

php bin/magento cache:clean
php bin/magento cache:flush

Hope this helps

2
  • Thanks, even if I used the use the class names inside constructor instead of defining before it works. In order to avoid the large code I used the class inside the constructor itself.
    – Vigna S
    Commented Jul 9, 2016 at 6:58
  • Thank you so much. Your answer helped me. I had used this Simple Answer. $customHelper = $this->helper('Vendor\Mymodule\Helper\Data'); :)
    – Ask Xah
    Commented Aug 3, 2019 at 17:50
2

Ugly way will be:

     namespace Vendor\Module_Name\Block\Product;
     class ListProduct extends \Magento\Catalog\Block\Product\ListProduct
     {
       public function test(){
           $dataHelper = \Magento\Framework\App\ObjectManager::getInstance()->get("\Vendor\Mymodule\Helper\Data");
           // Do some stuff with $dataHelper
       }
     }

It will be better in case you need test some things quickly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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