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
);
}
}