覆盖 \Magento\Payment\Block\Info\Cc class 的 _prepareSpecificInformation 方法

Override _prepareSpecificInformation method of \Magento\Payment\Block\Info\Cc class

我想覆盖Magento\Payment\Block\Info\Cc_prepareSpecificInformation方法class

这是核心 class。

vendor/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>

我的习惯class

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 已被自定义模块覆盖。

更正偏好后它对我有用。

<?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>

经过调查,我们找到了解决方案。您不能直接覆盖 _prepareSpecificInformation 方法,因为此方法已覆盖到名称为 Magento\Paypal\Block\Payment\Info.

的另一个 class

您必须将此 class 覆盖到自定义模块中,然后您才能对函数进行任何您想要的更改。

更多信息请参考此内容:

https://itcruncher.blogspot.com/2020/11/override-preparespecificinformation.html

喜欢请给kund