问题描述
我想覆盖 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()));
}
}
module.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>
registration.PHP
<?PHP
declare(strict_types=1);
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,'Muk_OrderEmail',__DIR__
);
但是这种替代对我不起作用。
解决方法
原来,我覆盖了一个错误的文件。因为 \ Magento \ Payment \ Block \ Info \ Cc 已被自定义模块覆盖。
更正偏好后,它对我有用。
=TEXT(A1,"mmm-yyyy")
,
在对此进行调查之后,我们找到了解决方案。您不能直接覆盖_prepareSpecificInformation方法,因为该方法被覆盖到另一个名为Magento \ Paypal \ Block \ Payment \ Info的类中。
您必须将此类覆盖到自定义模块中,然后才能对功能进行任何更改。
请参阅此内容以获取更多信息:
https://itcruncher.blogspot.com/2020/11/override-preparespecificinformation.html
如果您愿意,请给我