phpmagento2magento-2.3

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


I want to override _prepareSpecificInformation method of the Magento\Payment\Block\Info\Cc class

This is Core 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()));
}

Following is what I have done.

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>

My custom 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__
);

But this override is not working for me.


Solution

  • It turned out that I was overriding a wrong file. Because the \Magento\Payment\Block\Info\Cc was already overridden by a custom module.

    After correcting the preference it worked for me.

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