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?
2 Answers 2
To resume
- comment
__constructmethod (temporary solution) - remove
Interceptorgenerated (new one will be generated - need to be removed after each modification of__constructmethod) - 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– chirag dodia2016年01月02日 13:32:55 +00:00Commented Jan 2, 2016 at 13:32
-
it was for debug, you can now try to uncomment the method.bchatard– bchatard2016年01月02日 13:40:24 +00:00Commented Jan 2, 2016 at 13:40
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);
TT\Helloworld\Block\Myproduct.php==>TT\Helloworld\Block\Myproduct(remove.php)C:\xampp\htdocs\Magento2\var\generation\TT\Helloworld\Block\Myproduct\Interceptor.phpand comment the__constructmethod and try again please