0

Hi I am facing issue deprecating issue of below explode function,

public function getStreet()
    {
        if (is_array($this->getData(OrderAddressInterface::STREET))) {
            return $this->getData(OrderAddressInterface::STREET);
        }
        return explode(PHP_EOL, $this->getData(OrderAddressInterface::STREET));
    }

And for this i want to override this file and following below steps, 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" type="Vendor\Coreextended\Model\Order" />
</config>

file path Vendor/Coreextended/Model/Order/Address.php

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Vendor\Coreextended\Model\Order;
use Magento\Customer\Model\Address\AddressModelInterface;
use Magento\Sales\Api\Data\OrderAddressInterface;
use Magento\Sales\Model\AbstractModel;
class Address extends \Magento\Sales\Model\Order implements OrderAddressInterface, AddressModelInterface
{
    

    /**
     * Retrieve street field of an address
     *
     * @return string[]
     */
    public function getStreet()
    {
        if (is_array($this->getData(OrderAddressInterface::STREET))) {
            return $this->getData(OrderAddressInterface::STREET);
        }
        return explode(PHP_EOL, $this->getData(OrderAddressInterface::STREET) ?? '');
    }

    
}

but getting error

[message] => Class "Vendor\Coreextended\Model\Order" does not exist [code] => -1 [trace] => #0 /home/theboozybutton/public_html/vendor/magento/framework/Code/Reader/ClassReader.php(34): ReflectionClass->__construct() #1 /home/theboozybutton/public_html/vendor/magento/framework/ObjectManager/Definition/Runtime.php(54): Magento\Framework\Code\Reader\ClassReader->getConstructor() #2 /home/theboozybutton/public_html/vendor/magento/framework/ObjectManager/Factory/Compiled.php(100): Magento\Framework\ObjectManager\Definition\Runtime->getParameters() #3 /home/theboozybutton/public_html/vendor/magento/framework/Object

1 Answer 1

0

It looks like there is an error in your di.xml file. The preference for should be Magento\Sales\Model\Order\Address, not Magento\Sales\Model\Order and type should be Vendor\Coreextended\Model\Order\Address, not Vendor\Coreextended\Model\Order.

Also, in your custom Address class Vendor\Coreextended\Model\Order\Address, you are extending \Magento\Sales\Model\Order which is not correct, it should be extend from \Magento\Sales\Model\Order\Address

Here is how the corrected di.xml file should look like:

<?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\Address" type="Vendor\Coreextended\Model\Order\Address"/>
</config>

And the corrected custom class should look like:

<?php
namespace Vendor\Coreextended\Model\Order;

use Magento\Sales\Api\Data\OrderAddressInterface;

class Address extends \Magento\Sales\Model\Order\Address
{
    public function getStreet()
    {
        if (is_array($this->getData(OrderAddressInterface::STREET))) {
            return $this->getData(OrderAddressInterface::STREET);
        }
        return explode(PHP_EOL, $this->getData(OrderAddressInterface::STREET) ?? '');
    }
}

Clear the cache by running the command:

php bin/magento cache:clean

and re-run the code, and it should work without any issues.

Happy coding!

1
  • Thanks working fine
    – shad khan
    Jan 24, 2023 at 7:41

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.