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

Vigna S
  • 627
  • 7
  • 22

3 Answers3

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.

Rakesh Jesadiya
  • 42,221
  • 18
  • 132
  • 183
  • 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 Jul 09 '16 at 06:53
  • you have to remove var folder first and try again – Rakesh Jesadiya Jul 09 '16 at 07: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 Jul 09 '16 at 07:05
  • no you have to just delete whole var folder, all files are generated automatically when you can refresh page – Rakesh Jesadiya Jul 09 '16 at 07: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

Atish Goswami
  • 5,551
  • 7
  • 34
  • 63
  • 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 Jul 09 '16 at 06:58
  • Thank you so much. Your answer helped me. I had used this Simple Answer. $customHelper = $this->helper('Vendor\Mymodule\Helper\Data'); :) – Ask Xah Aug 03 '19 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.