skip to Main Content

I want to override _prepareSpecificInformation method of the MagentoPaymentBlockInfoCc class

This is Core class.

vendor/magento/module-payment/Block/Info/Cc.php

<?php
namespace MagentoPaymentBlockInfo;

/**
 * Credit card generic payment info
 *
 * @api
 * @since 100.0.2
 */
class Cc extends MagentoPaymentBlockInfo
{
  
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="MagentoPaymentBlockInfoCc" type="MukOrderEmailBlockInfoCc"/>
</config>

My custom class

app/code/Muk/OrderEmail/Block/Info/Cc.php

<?php
declare(strict_types=1);

namespace MukOrderEmailBlockInfo;

use MagentoFrameworkDataObject;
use MagentoFrameworkExceptionLocalizedException;

class Cc extends MagentoPaymentBlockInfoCc
{
    public function __construct
    (
        MagentoFrameworkViewElementTemplateContext $context,
        MagentoPaymentModelConfig $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);

MagentoFrameworkComponentComponentRegistrar::register(
    MagentoFrameworkComponentComponentRegistrar::MODULE,
    'Muk_OrderEmail',
    __DIR__
);

But this override is not working for me.

2

Answers


  1. Chosen as BEST ANSWER

    It turned out that I was overriding a wrong file. Because the MagentoPaymentBlockInfoCc 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="VendorNamePaymetricBlockXiinterceptInfoIframe" type="MukOrderEmailBlockInfoCc"/>
    </config>
    

  2. After investigating in this, we have found solution for this. You can not override _prepareSpecificInformation method directly because this method is overided into another class that name is MagentoPaypalBlockPaymentInfo.

    You have to override this class into custom module than you can do whatever changes into function that you want do.

    Please refer this content for more info:

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

    If you like please give kund

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search