Magento 2:覆盖Magento \ Catalog \ Block \ Product \ View块


8

我已经开发了一个自定义模块,并且我尝试通过遵循以下两个链接来覆盖产品视图块:Magento 2中的Overriding BlockDI以及Magento 2上的Extended Block,但是当我点击产品视图页面时,它会显示404页面。我到目前为止所做的是下面

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();
}

根据DI&Magento 2上的扩展块链接,我还在Myproduct.php构造函数中包括了所有父类构造参数。

有人知道我错了吗?或什么是重写此方法的正确方法?


您将通过链接找到您的解决方案。magento.stackexchange.com/questions/86497/...
阿南德Ontigeri

@AnandOntigeri该解决方案也不起作用,您可以在此处添加解决方案吗?
chirag dodia

TT\Helloworld\Block\Myproduct.php==> TT\Helloworld\Block\Myproduct(删除.php
bchatard'1

@ BriceC.that也无法正常工作
chirag dodia

1
删除方法C:\xampp\htdocs\Magento2\var\generation\TT\Helloworld\Block\Myproduct\Intercepto‌​r.php并发表评论,__construct然后重试
bchatard '16

Answers:


5

恢复

  • 评论__construct方法(临时解决方案)
  • 删除Interceptor生成的(将生成新的-需要在__construct方法的每次修改后删除)
  • 在您的布局中使用 template="TT_Helloworld::myproduct.phtml"

我不明白从类中删除__construct方法的原因是什么?
chirag dodia'1

它是用于调试的,您现在可以尝试取消注释该方法。
bchatard '16

2

您需要在__contruct中的数组$ data = []之前输入\ TT \ Helloworld \ Helper \ Data $ helper

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);
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.