Tôi đã phát triển một mô-đun tùy chỉnh và tôi đã cố gắng ghi đè khối chế độ xem sản phẩm bằng cách theo hai liên kết này Ghi đè Khối trong Magento 2 và DI & Mở rộng Khối trên Magento 2 nhưng khi tôi nhấn trang xem sản phẩm, trang này sẽ cung cấp cho tôi trang 404. những gì tôi đã làm cho đến nay là dưới đây
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();
}
Theo DI & Mở rộng liên kết Khối trên Magento 2, tôi cũng đã bao gồm tất cả tham số cấu trúc lớp cha trong trình xây dựng Myproduct.php.
có ai biết tôi sai ở đâu không? hoặc cách chính xác để ghi đè này là gì?
bạn sẽ tìm thấy giải pháp của bạn theo liên kết. magento.stackexchange.com/questions/86497/ từ
—
Anand Onigeri
@AnandOntigeri giải pháp đó cũng không hoạt động bạn có thể thêm giải pháp ở đây không?
—
chirag dodia
TT\Helloworld\Block\Myproduct.php
==> TT\Helloworld\Block\Myproduct
(xóa .php
)
@ BriceC.that cũng không hoạt động
—
chirag dodia
xóa
—
bchatard
C:\xampp\htdocs\Magento2\var\generation\TT\Helloworld\Block\Myproduct\Interceptor.php
và nhận xét __construct
phương pháp và thử lại xin vui lòng