0

I am trying to override the Magento\Sales\Model\Order\Payment\Info.php to solve an error due to serialized migrated data from Magento 1 to Magento 2. The method I am trying to override is a protected method, so the plugin override option may not work.

protected function initAdditionalInformation()
{
    $additionalInfo = $this->getData('additional_information');
    if (empty($this->additionalInformation) && $additionalInfo) {
        $this->additionalInformation = $additionalInfo;
    }
}

So I created a a custom module Myvendorname_CoreFixes. Please note that I managed to override other files using this module, so I believe the my module setup is right.

Here is my app/code/Myvendorname/CoreFixes/etc/di.xml entry. (I am using couple of other overrides here, but stripping those off).

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Sales\Model\Order\Payment\Info" type="Myvendorname\CoreFixes\Model\Payment\Info" />
</config>

Here is the original code copied from the core file, which I need to override, once this works.

namespace Myvendorname\CoreFixes\Model\Payment;

use Magento\Sales\Model\Order\Payment\Info as CoreInfo;

/**
 *
 * Payment information model
 * @api
 * @since 100.0.2
 */
class Info extends CoreInfo
{
    /**
     * Check whether there is additional information by specified key
     *
     * @param mixed|null $key
     * @return bool
     */
    public function hasAdditionalInformation($key = null)
    {
        $this->initAdditionalInformation();
        return null === $key ? !empty($this->additionalInformation) : array_key_exists(
            $key,
            $this->additionalInformation
        );
    }

    /**
     * Initialize additional information container with data from model
     * if property empty
     *
     * @return void
     */
    protected function initAdditionalInformation()
    {
        $additionalInfo = $this->getData('additional_information');
        if (empty($this->additionalInformation) && $additionalInfo) {
            $this->additionalInformation = $additionalInfo;
        }
    }
}

When I try to edit an order with migrated serialized data, I am getting the below exception.

1 exception(s):
Exception #0 (Exception): Warning: array_key_exists() expects parameter 2 to be array, string given in /vendor/magento/module-sales/Model/Order/Payment/Info.php on line 217

Exception #0 (Exception): Warning: array_key_exists() expects parameter 2 to be array, string given in /vendor/magento/module-sales/Model/Order/Payment/Info.php on line 217
#0 [internal function]: Magento\Framework\App\ErrorHandler->handler(2, 'array_key_exist...', '...', 217, Array)
#1 /vendor/magento/module-sales/Model/Order/Payment/Info.php(217): array_key_exists('paypal_payer_id', 'a:12:{s:39:"pay...')
#2 /generated/code/Magento/Sales/Model/Order/Payment/Interceptor.php(2143): Magento\Sales\Model\Order\Payment\Info->hasAdditionalInformation('paypal_payer_id')
#3 /vendor/magento/module-paypal/Model/Info.php(560): Magento\Sales\Model\Order\Payment\Interceptor->hasAdditionalInformation('paypal_payer_id')
#4 /vendor/magento/module-paypal/Model/Info.php(273): Magento\Paypal\Model\Info->_getFullInfo(Array, Object(Magento\Sales\Model\Order\Payment\Interceptor), true)
#5 /vendor/magento/module-paypal/Block/Payment/Info.php(61): Magento\Paypal\Model\Info->getPaymentInfo(Object(Magento\Sales\Model\Order\Payment\Interceptor), true)
#6 /vendor/magento/module-payment/Block/Info.php(90): Magento\Paypal\Block\Payment\Info->_prepareSpecificInformation()
#7 /vendor/magento/module-payment/view/adminhtml/templates/info/default.phtml(12): Magento\Payment\Block\Info->getSpecificInformation()

Clearly, my override is not working as it is still referencing the core file rather than, my override.

Any idea what would be the issue?

Once this is working, my plan is to convert the serialised data to json data, so that I can load the order.

PS: I have read that ideally the old serialised data should be converted to json data. But I need to do this as a quick work around.

Many thanks

UPDATE: (Created as per @Hitesh's suggestion, but still not working)

I have create the module as below:

app/code/Absolute/OrderInfoFix/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Sales\Model\Order\Payment\Info" type="Absolute\OrderInfoFix\Model\Magento\Sales\Order\Payment\Info" />
</config>

app/code/Absolute/OrderInfoFix/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Absolute_OrderInfoFix" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Backend"/>
            <module name="Magento_Sales"/>
            <module name="Magento_Quote"/>
            <module name="Magento_Checkout"/>
            <module name="Magento_Cms"/>
        </sequence>
    </module>
</config>

app/code/Absolute/OrderInfoFix/registration.php

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Absolute_OrderInfoFix',
    __DIR__
);

app/code/Absolute/OrderInfoFix/Model/Magento/Sales/Order/Payment/Info.php

<?php

namespace Absolute\OrderInfoFix\Model\Magento\Sales\Order\Payment;

class Info extends \Magento\Sales\Model\Order\Payment\Info
{
    /**
     * Check whether there is additional information by specified key
     *
     * @param mixed|null $key
     * @return bool
     */
    public function hasAdditionalInformation($key = null)
    {
        $this->initAdditionalInformation();
        return false;
        return null === $key ? !empty($this->additionalInformation) : array_key_exists(
            $key,
            $this->additionalInformation
        );
    }
}
3
  • Try it by adding sequence of sales module in your custom module's module.xml file. Commented Nov 21, 2018 at 17:29
  • @RamkishanSuthar I tried that, but that is not working either. Still loading the core file! Thanks.
    – bjjn
    Commented Nov 21, 2018 at 23:54
  • getting any solution ???? Commented Jul 16, 2020 at 8:55

2 Answers 2

1

I just create one module online but didn't test it.

Try below code it will solve your issue

your di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

    <preference for="Magento\Sales\Model\Order\Payment\Info" type="SilkSoftware\ModuleName\Model\Magento\Sales\Order\Payment\Info" />

</config>

your module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="SilkSoftware_ModuleName" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Backend"/>
             <module name="Magento_Sales"/>
            <module name="Magento_Quote"/>
            <module name="Magento_Checkout"/>
            <module name="Magento_Cms"/>
        </sequence>
    </module>
</config>

your Info.php do whatever you want

namespace SilkSoftware\ModuleName\Model\Magento\Sales\Order\Payment;


class Info extends \Magento\Sales\Model\Order\Payment\Info
{


}

Run below commands after creating above files in particular folders

php bin/magento setup:upgrade && php bin/magento setup:static-content:deploy -f

1
  • Thanks @Hitesh, still no luck. Please see my updated question to see the code I am trying.
    – bjjn
    Commented Nov 22, 2018 at 10:39
0

You can override this file (Magento\Sales\Model\Order\Payment\Info.php) By using Plugin method. File: Vendor/Module/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
     <type name="Magento\Sales\Model\Order\Payment\Info">
        <plugin name="Payment" type="Vendor\Module\Model\Rewrite\Order\Payment\Info" sortOrder="1" />
    </type>
</config>

File: Vendor/Module/Model/Rewrite/Order/Payment/Info.php

<?php
    
    namespace Vendor\Module\Model\Rewrite\Order\Payment;
    
    class Info 
    {
        public function aroundHasAdditionalInformation(
               \Magento\Sales\Model\Order\Payment\Info $subject,
               callable $proceed
            ) {
            $this->additionalInformation = $subject->getAdditionalInformation();
            if(!empty($this->additionalInformation) && !is_array($this->additionalInformation)){
                $this->additionalInformation = unserialize($this->additionalInformation);
            }
            return $this->additionalInformation;
        }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.