8

I have developed one custom module and i have tried to override product view block by following these two links Overriding Block in Magento 2 and DI & Extending a Block on Magento 2 but when i hit product view page its gives me 404 page. what i have done so far is below

di.xml

  <?xml version="1.0"?>
   <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
       <preference for="Magento\Catalog\Block\Product\View" type="TT\Helloworld\Block\Myproduct"/>
   </config>

Myproduct.php

<?php
namespace TT\Helloworld\Block;

use Magento\Framework\View\Element\Template;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Product;
class Myproduct extends \Magento\Catalog\Block\Product\View
{

protected $_helper;

protected $_objectManager;

public function __construct(
        \Magento\Catalog\Block\Product\Context $context,
        \Magento\Framework\Url\EncoderInterface $urlEncoder,
        \Magento\Framework\Json\EncoderInterface $jsonEncoder,
        \Magento\Framework\Stdlib\StringUtils $string,
        \Magento\Catalog\Helper\Product $productHelper,
        \Magento\Catalog\Model\ProductTypes\ConfigInterface $productTypeConfig,
        \Magento\Framework\Locale\FormatInterface $localeFormat,
        \Magento\Customer\Model\Session $customerSession,
        ProductRepositoryInterface $productRepository,
        \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency,
        array $data = [],
        \TT\Helloworld\Helper\Data $helper  
) {
    parent::__construct($context, $urlEncoder, $jsonEncoder, $string, $productHelper, $productTypeConfig, $localeFormat, $customerSession, $productRepository, $priceCurrency, $data,$helper);

    $this->_helper = $helper;

}

protected function _toHtml()
{
    $this->setModuleName($this->extractModuleName('Magento\Catalog\Block\Product\View'));
    return parent::_toHtml();
}

As per DI & Extending a Block on Magento 2 link i have also included all the parent class construct parameter in Myproduct.php constructer.

anyone one know where i'm wrong? or what is the correct way to override this?

  • you will find your solution follow the link. magento.stackexchange.com/questions/86497/… – Anand Ontigeri Jan 2 '16 at 9:40
  • @AnandOntigeri that solution also not working can you add solution here? – chirag dodia Jan 2 '16 at 10:00
  • TT\Helloworld\Block\Myproduct.php ==> TT\Helloworld\Block\Myproduct (remove .php) – bchatard Jan 2 '16 at 10:53
  • @BriceC.that also not working – chirag dodia Jan 2 '16 at 11:01
  • 1
    remove C:\xampp\htdocs\Magento2\var\generation\TT\Helloworld\Block\Myproduct\Intercepto‌​r.php and comment the __construct method and try again please – bchatard Jan 2 '16 at 12:30
5

To resume

  • comment __construct method (temporary solution)
  • remove Interceptor generated (new one will be generated - need to be removed after each modification of __construct method)
  • in your layout use template="TT_Helloworld::myproduct.phtml"
  • i don't understand what is the reason behind remove __construct method from my class? – chirag dodia Jan 2 '16 at 13:32
  • it was for debug, you can now try to uncomment the method. – bchatard Jan 2 '16 at 13:40
2

you need to enter \TT\Helloworld\Helper\Data $helper before array $data = [] in the __contruct

public function __construct(
    \Magento\Catalog\Block\Product\Context $context,
    \Magento\Framework\Url\EncoderInterface $urlEncoder,
    \Magento\Framework\Json\EncoderInterface $jsonEncoder,
    \Magento\Framework\Stdlib\StringUtils $string,
    \Magento\Catalog\Helper\Product $productHelper,
    \Magento\Catalog\Model\ProductTypes\ConfigInterface $productTypeConfig,
    \Magento\Framework\Locale\FormatInterface $localeFormat,
    \Magento\Customer\Model\Session $customerSession,
    ProductRepositoryInterface $productRepository,
    \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency,
    \TT\Helloworld\Helper\Data $helper ,
    array $data = []
) {
    parent::__construct($context, $urlEncoder, $jsonEncoder, $string, $productHelper, $productTypeConfig, $localeFormat, $customerSession, $productRepository, $priceCurrency, $data,$helper);

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.