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?
Best Answer
To resume
__construct
method (temporary solution)Interceptor
generated (new one will be generated - need to be removed after each modification of__construct
method)template="TT_Helloworld::myproduct.phtml"