我已经开发了一个自定义模块,并且我尝试通过遵循以下两个链接来覆盖产品视图块:Magento 2中的Overriding Block 和DI以及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
)
@ BriceC.that也无法正常工作
—
chirag dodia
删除方法
—
bchatard '16
C:\xampp\htdocs\Magento2\var\generation\TT\Helloworld\Block\Myproduct\Interceptor.php
并发表评论,__construct
然后重试