magento2 - 覆盖 \Magento\Payment\Block\Info\Cc 类的 _prepareSpecificInformation 方法
问题描述
我想覆盖Magento\Payment\Block\Info\Cc类的_prepareSpecificInformation方法
这是核心课程。
供应商/magento/module-payment/Block/Info/Cc.php
<?php
namespace Magento\Payment\Block\Info;
/**
* Credit card generic payment info
*
* @api
* @since 100.0.2
*/
class Cc extends \Magento\Payment\Block\Info
{
protected function _prepareSpecificInformation($transport = null)
{
if (null !== $this->_paymentSpecificInformation) {
return $this->_paymentSpecificInformation;
}
$transport = parent::_prepareSpecificInformation($transport);
$data = [];
if ($ccType = $this->getCcTypeName()) {
$data[(string)__('Credit Card Type')] = $ccType;
}
if ($this->getInfo()->getCcLast4()) {
$data[(string)__('Credit Card Number')] = sprintf('xxxx-%s', $this->getInfo()->getCcLast4());
}
if (!$this->getIsSecureMode()) {
if ($ccSsIssue = $this->getInfo()->getCcSsIssue()) {
$data[(string)__('Switch/Solo/Maestro Issue Number')] = $ccSsIssue;
}
$year = $this->getInfo()->getCcSsStartYear();
$month = $this->getInfo()->getCcSsStartMonth();
if ($year && $month) {
$data[(string)__('Switch/Solo/Maestro Start Date')] = $this->_formatCardDate($year, $month);
}
}
return $transport->setData(array_merge($data, $transport->getData()));
}
以下是我所做的。
Muk/OrderEmail/etc/di.xml
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Payment\Block\Info\Cc" type="Muk\OrderEmail\Block\Info\Cc"/>
</config>
我的自定义课程
app/code/Muk/OrderEmail/Block/Info/Cc.php
<?php
declare(strict_types=1);
namespace Muk\OrderEmail\Block\Info;
use Magento\Framework\DataObject;
use Magento\Framework\Exception\LocalizedException;
class Cc extends \Magento\Payment\Block\Info\Cc
{
public function __construct
(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Payment\Model\Config $paymentConfig,
array $data = [])
{
parent::__construct($context, $paymentConfig, $data);
}
/**
* Prepare credit card related payment info
*
* @param DataObject|array $transport
* @return DataObject
* @throws LocalizedException
*/
protected function _prepareSpecificInformation($transport = null)
{
if (null !== $this->_paymentSpecificInformation) {
return $this->_paymentSpecificInformation;
}
$transport = parent::_prepareSpecificInformation($transport);
$data = [];
if ($ccType = $this->getCcTypeName()) {
$data[(string)__('Credit Card Type')] = $ccType;
}
if ($this->getInfo()->getCcLast4()) {
$data[(string)__('Credit Card Number')] = sprintf('xxxx-%s', $this->getInfo()->getCcLast4());
}
// Custom information
if ($ccType = $this->getCcTypeName()) {
$data[(string)__('Name on the Card:')] = $this->getInfo()->getCcOwner();
}
// End Custom information
if (!$this->getIsSecureMode()) {
if ($ccSsIssue = $this->getInfo()->getCcSsIssue()) {
$data[(string)__('Switch/Solo/Maestro Issue Number')] = $ccSsIssue;
}
$year = $this->getInfo()->getCcSsStartYear();
$month = $this->getInfo()->getCcSsStartMonth();
if ($year && $month) {
$data[(string)__('Switch/Solo/Maestro Start Date')] = $this->_formatCardDate($year, $month);
}
}
return $transport->setData(array_merge($data, $transport->getData()));
}
}
模块.xml
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Muk_OrderEmail">
<sequence>
<module name="Magento_Payment"/>
<module name="Magento_Backend"/>
<module name="Magento_Sales"/>
<module name="Magento_Quote"/>
<module name="Magento_Checkout"/>
</sequence>
</module>
</config>
注册.php
<?php
declare(strict_types=1);
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Muk_OrderEmail',
__DIR__
);
但是这个覆盖对我不起作用。
解决方案
原来我覆盖了一个错误的文件。因为\Magento\Payment\Block\Info\Cc已经被自定义模块覆盖。
更正偏好后,它对我有用。
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="VendorName\Paymetric\Block\Xiintercept\Info\Iframe" type="Muk\OrderEmail\Block\Info\Cc"/>
</config>
推荐阅读
- python - 为什么我在 PyTorch 中用于降噪的自动编码器学会了将所有内容归零?
- c# - 读取 FileStream 超时
- git - ![远程拒绝] master -> master (pre-receive hook denied) git push 错误
- css - 作用域 CSS 选择器性能
- regex - 如何捕获具有逗号和引号的字符串之间的值?
- javascript - React-native 在发出发布请求后得到响应不起作用
- python - 我的消息框代码在我的窗口上不起作用。其他一切正常,但是当我运行它时,我的“关于”消息框不会出现
- python - 如何使用 JSD Cloud API 在 Jira Service Desk 中创建客户?
- cs50 - 如何缩小 CS50 IDE?不小心放大了不知道怎么恢复原来的大小
- html - 没有第三方导入的 Golang 中的 html 渲染器