Skip to main content
Mod Moved Comments To Chat
deleted 237 characters in body
Source Link
Amit Bera
  • 77.7k
  • 20
  • 124
  • 239
<?php
namespace Vendorname\ModuleName\Plugin\Magento\Sales\Api;

//use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\Data\OrderAddressExtensionInterfaceFactory;
use Magento\Sales\Api\Data\OrderAddressExtension;
use Magento\Sales\Api\Data\OrderAddressInterface;

class OrderRepositoryPluginOrderRepositoryInterface
{
    /**
     * @var OrderAddressExtensionInterfaceFactory
     */
    private $addressExtensionInterfaceFactory;

    public function __construct(
        OrderAddressExtensionInterfaceFactory $addressExtensionInterfaceFactory
    ) {
        $this->addressExtensionInterfaceFactory = $addressExtensionInterfaceFactory;
    }

    public function afterGet(
        OrderRepositoryInterface\Magento\Sales\Api\OrderRepositoryInterface $subject,
        OrderInterface $order
    ) {
        /**
         * @var OrderAddressInterface  $billingAddress
         */
        $billingAddress = $order->getBillingAddress();

        $billingAddressExtensionAttributes = (null !== $billingAddress->getExtensionAttributes())?
            $billingAddress->getExtensionAttributes():
            $this->addressExtensionInterfaceFactory->create();
        $billingAddressExtensionAttributes->setDoorNo($billingAddress->getDoorNo());

        $billingAddress->setExtensionAttributes($billingAddressExtensionAttributes);

        if (!$order->getIsVirtual()) {
            $shippingAddress = $order->getShippingAddress();
            $shippingAddressExtensionAttributes = (null!== $shippingAddress->getExtensionAttributes())?
                $shippingAddress->getExtensionAttributes():
                $this->addressExtensionInterfaceFactory->create();

            $shippingAddressExtensionAttributes->setDoorNo($shippingAddress->getDoorNo());
            $shippingAddress->setExtensionAttributes($shippingAddressExtensionAttributes);
        }

        return $order;
    }
}

Create di.xml at app/code/{Vendorname}/{ModuleName}/etc/

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

    <type name="Magento\Sales\Api\OrderRepositoryInterface">
        <plugin disabled="false" name="add_door_no_to_address" sortOrder="10"
                type="Vendorname\ModuleName\Plugin\Magento\Sales\Api\OrderRepositoryInterface"/>
    </type>
</config>
<?php
namespace Vendorname\ModuleName\Plugin\Magento\Sales\Api;

//use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\Data\OrderAddressExtensionInterfaceFactory;
use Magento\Sales\Api\Data\OrderAddressExtension;
use Magento\Sales\Api\Data\OrderAddressInterface;

class OrderRepositoryPlugin
{
    /**
     * @var OrderAddressExtensionInterfaceFactory
     */
    private $addressExtensionInterfaceFactory;

    public function __construct(
        OrderAddressExtensionInterfaceFactory $addressExtensionInterfaceFactory
    ) {
        $this->addressExtensionInterfaceFactory = $addressExtensionInterfaceFactory;
    }

    public function afterGet(
        OrderRepositoryInterface $subject,
        OrderInterface $order
    ) {
        /**
         * @var OrderAddressInterface  $billingAddress
         */
        $billingAddress = $order->getBillingAddress();

        $billingAddressExtensionAttributes = (null !== $billingAddress->getExtensionAttributes())?
            $billingAddress->getExtensionAttributes():
            $this->addressExtensionInterfaceFactory->create();
        $billingAddressExtensionAttributes->setDoorNo($billingAddress->getDoorNo());

        $billingAddress->setExtensionAttributes($billingAddressExtensionAttributes);

        if (!$order->getIsVirtual()) {
            $shippingAddress = $order->getShippingAddress();
            $shippingAddressExtensionAttributes = (null!== $shippingAddress->getExtensionAttributes())?
                $shippingAddress->getExtensionAttributes():
                $this->addressExtensionInterfaceFactory->create();

            $shippingAddressExtensionAttributes->setDoorNo($shippingAddress->getDoorNo());
            $shippingAddress->setExtensionAttributes($shippingAddressExtensionAttributes);
        }

        return $order;
    }
}
<?php
namespace Vendorname\ModuleName\Plugin\Magento\Sales\Api;

use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\Data\OrderAddressExtensionInterfaceFactory;
use Magento\Sales\Api\Data\OrderAddressExtension;
use Magento\Sales\Api\Data\OrderAddressInterface;

class OrderRepositoryInterface
{
    /**
     * @var OrderAddressExtensionInterfaceFactory
     */
    private $addressExtensionInterfaceFactory;

    public function __construct(
        OrderAddressExtensionInterfaceFactory $addressExtensionInterfaceFactory
    ) {
        $this->addressExtensionInterfaceFactory = $addressExtensionInterfaceFactory;
    }

    public function afterGet(
        \Magento\Sales\Api\OrderRepositoryInterface $subject,
        OrderInterface $order
    ) {
        /**
         * @var OrderAddressInterface  $billingAddress
         */
        $billingAddress = $order->getBillingAddress();

        $billingAddressExtensionAttributes = (null !== $billingAddress->getExtensionAttributes())?
            $billingAddress->getExtensionAttributes():
            $this->addressExtensionInterfaceFactory->create();
        $billingAddressExtensionAttributes->setDoorNo($billingAddress->getDoorNo());

        $billingAddress->setExtensionAttributes($billingAddressExtensionAttributes);

        if (!$order->getIsVirtual()) {
            $shippingAddress = $order->getShippingAddress();
            $shippingAddressExtensionAttributes = (null!== $shippingAddress->getExtensionAttributes())?
                $shippingAddress->getExtensionAttributes():
                $this->addressExtensionInterfaceFactory->create();

            $shippingAddressExtensionAttributes->setDoorNo($shippingAddress->getDoorNo());
            $shippingAddress->setExtensionAttributes($shippingAddressExtensionAttributes);
        }

        return $order;
    }
}

Create di.xml at app/code/{Vendorname}/{ModuleName}/etc/

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

    <type name="Magento\Sales\Api\OrderRepositoryInterface">
        <plugin disabled="false" name="add_door_no_to_address" sortOrder="10"
                type="Vendorname\ModuleName\Plugin\Magento\Sales\Api\OrderRepositoryInterface"/>
    </type>
</config>
added 2 characters in body
Source Link
Amit Bera
  • 77.7k
  • 20
  • 124
  • 239
<?php
namespace Vendorname\ModuleName\Plugin\Magento\Sales\Api;

//use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\Data\OrderAddressExtensionInterfaceFactory;
use Magento\Sales\Api\Data\OrderAddressExtension;
use Magento\Sales\Api\Data\OrderAddressInterface;

class OrderRepositoryPlugin
{
    /**
     * @var OrderAddressExtensionInterfaceFactory
     */
    private $addressExtensionInterfaceFactory;

    public function __construct(
        OrderAddressExtensionInterfaceFactory $addressExtensionInterfaceFactory
    ) {
        $this->addressExtensionInterfaceFactory = $addressExtensionInterfaceFactory;
    }

    public function afterGet(
        OrderRepositoryInterface $subject,
        OrderInterface $order
    ) {
        /**
         * @var OrderAddressInterface  $billingAddress
         */
        $billingAddress = $order->getBillingAddress();

        $billingAddressExtensionAttributes = (null !== $billingAddress->getExtensionAttributes())?
            $billingAddress->getExtensionAttributes():
            $this->addressExtensionInterfaceFactory->create();
        $billingAddressExtensionAttributes->setDoorNo($billingAddress->getDoorNo());

        $billingAddress->setExtensionAttributes($billingAddressExtensionAttributes);

        if (!$order->getIsVirtual()) {
            $shippingAddress = $order->getShippingAddress();
            $shippingAddressExtensionAttributes = (null!== $shippingAddress->getExtensionAttributes())?
                $shippingAddress->getExtensionAttributes():
                $this->addressExtensionInterfaceFactory->create();

            $shippingAddressExtensionAttributes->setDoorNo($shippingAddress->getDoorNo());
            $shippingAddress->setExtensionAttributes($shippingAddressExtensionAttributes);
        }

        return $order;
    }
}
<?php
namespace Vendorname\ModuleName\Plugin\Magento\Sales\Api;

use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\Data\OrderAddressExtensionInterfaceFactory;
use Magento\Sales\Api\Data\OrderAddressExtension;
use Magento\Sales\Api\Data\OrderAddressInterface;

class OrderRepositoryPlugin
{
    /**
     * @var OrderAddressExtensionInterfaceFactory
     */
    private $addressExtensionInterfaceFactory;

    public function __construct(
        OrderAddressExtensionInterfaceFactory $addressExtensionInterfaceFactory
    ) {
        $this->addressExtensionInterfaceFactory = $addressExtensionInterfaceFactory;
    }

    public function afterGet(
        OrderRepositoryInterface $subject,
        OrderInterface $order
    ) {
        /**
         * @var OrderAddressInterface  $billingAddress
         */
        $billingAddress = $order->getBillingAddress();

        $billingAddressExtensionAttributes = (null !== $billingAddress->getExtensionAttributes())?
            $billingAddress->getExtensionAttributes():
            $this->addressExtensionInterfaceFactory->create();
        $billingAddressExtensionAttributes->setDoorNo($billingAddress->getDoorNo());

        $billingAddress->setExtensionAttributes($billingAddressExtensionAttributes);

        if (!$order->getIsVirtual()) {
            $shippingAddress = $order->getShippingAddress();
            $shippingAddressExtensionAttributes = (null!== $shippingAddress->getExtensionAttributes())?
                $shippingAddress->getExtensionAttributes():
                $this->addressExtensionInterfaceFactory->create();

            $shippingAddressExtensionAttributes->setDoorNo($shippingAddress->getDoorNo());
            $shippingAddress->setExtensionAttributes($shippingAddressExtensionAttributes);
        }

        return $order;
    }
}
<?php
namespace Vendorname\ModuleName\Plugin\Magento\Sales\Api;

//use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\Data\OrderAddressExtensionInterfaceFactory;
use Magento\Sales\Api\Data\OrderAddressExtension;
use Magento\Sales\Api\Data\OrderAddressInterface;

class OrderRepositoryPlugin
{
    /**
     * @var OrderAddressExtensionInterfaceFactory
     */
    private $addressExtensionInterfaceFactory;

    public function __construct(
        OrderAddressExtensionInterfaceFactory $addressExtensionInterfaceFactory
    ) {
        $this->addressExtensionInterfaceFactory = $addressExtensionInterfaceFactory;
    }

    public function afterGet(
        OrderRepositoryInterface $subject,
        OrderInterface $order
    ) {
        /**
         * @var OrderAddressInterface  $billingAddress
         */
        $billingAddress = $order->getBillingAddress();

        $billingAddressExtensionAttributes = (null !== $billingAddress->getExtensionAttributes())?
            $billingAddress->getExtensionAttributes():
            $this->addressExtensionInterfaceFactory->create();
        $billingAddressExtensionAttributes->setDoorNo($billingAddress->getDoorNo());

        $billingAddress->setExtensionAttributes($billingAddressExtensionAttributes);

        if (!$order->getIsVirtual()) {
            $shippingAddress = $order->getShippingAddress();
            $shippingAddressExtensionAttributes = (null!== $shippingAddress->getExtensionAttributes())?
                $shippingAddress->getExtensionAttributes():
                $this->addressExtensionInterfaceFactory->create();

            $shippingAddressExtensionAttributes->setDoorNo($shippingAddress->getDoorNo());
            $shippingAddress->setExtensionAttributes($shippingAddressExtensionAttributes);
        }

        return $order;
    }
}
deleted 22 characters in body
Source Link
Amit Bera
  • 77.7k
  • 20
  • 124
  • 239

After that, need to expose this field to using a plugin on on \Magento\Sales\Model\Order\Magento\Sales\Api\OrderRepositoryInterface . Create afterafter plugin on method getBillingAddresssave and getShippingAddress..

<?php
namespace Vendorname\ModuleName\Plugin\Magento\Sales\Api;

use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\Data\OrderAddressExtensionInterfaceFactory;
use Magento\Sales\Api\Data\OrderAddressExtension;
use Magento\Sales\Api\Data\OrderAddressInterface;

class OrderRepositoryPlugin
{
    /**
     * @var OrderAddressExtensionInterfaceFactory
     */
    private $addressExtensionInterfaceFactory;

    public function __construct(
        OrderAddressExtensionInterfaceFactory $addressExtensionInterfaceFactory
    ) {
        $this->addressExtensionInterfaceFactory = $addressExtensionInterfaceFactory;
    }

    public function afterGet(
        OrderRepositoryInterface $subject,
        OrderInterface $order
    ) {
        /**
         * @var OrderAddressInterface  $billingAddress
         */
        $billingAddress = $order->getBillingAddress();

        $billingAddressExtensionAttributes = (null !== $billingAddress->getExtensionAttributes())?
            $billingAddress->getExtensionAttributes():
            $this->addressExtensionInterfaceFactory->create();
        $billingAddressExtensionAttributes->setDoorNo($billingAddress->getDoorNo());

        $billingAddress->setExtensionAttributes($billingAddressExtensionAttributes);

        if (!$order->getIsVirtual()) {
            $shippingAddress = $order->getShippingAddress();
            $shippingAddressExtensionAttributes = (null!== $shippingAddress->getExtensionAttributes())?
                $shippingAddress->getExtensionAttributes():
                $this->addressExtensionInterfaceFactory->create();

            $shippingAddressExtensionAttributes->setDoorNo($shippingAddress->getDoorNo());
            $shippingAddress->setExtensionAttributes($shippingAddressExtensionAttributes);
        }

        return $order;
    }
}

After that, need to expose this field to using a plugin on on \Magento\Sales\Model\Order . Create after plugin on method getBillingAddress and getShippingAddress..

After that, need to expose this field to using a plugin on on \Magento\Sales\Api\OrderRepositoryInterface . Create after plugin on method save .

<?php
namespace Vendorname\ModuleName\Plugin\Magento\Sales\Api;

use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\Data\OrderAddressExtensionInterfaceFactory;
use Magento\Sales\Api\Data\OrderAddressExtension;
use Magento\Sales\Api\Data\OrderAddressInterface;

class OrderRepositoryPlugin
{
    /**
     * @var OrderAddressExtensionInterfaceFactory
     */
    private $addressExtensionInterfaceFactory;

    public function __construct(
        OrderAddressExtensionInterfaceFactory $addressExtensionInterfaceFactory
    ) {
        $this->addressExtensionInterfaceFactory = $addressExtensionInterfaceFactory;
    }

    public function afterGet(
        OrderRepositoryInterface $subject,
        OrderInterface $order
    ) {
        /**
         * @var OrderAddressInterface  $billingAddress
         */
        $billingAddress = $order->getBillingAddress();

        $billingAddressExtensionAttributes = (null !== $billingAddress->getExtensionAttributes())?
            $billingAddress->getExtensionAttributes():
            $this->addressExtensionInterfaceFactory->create();
        $billingAddressExtensionAttributes->setDoorNo($billingAddress->getDoorNo());

        $billingAddress->setExtensionAttributes($billingAddressExtensionAttributes);

        if (!$order->getIsVirtual()) {
            $shippingAddress = $order->getShippingAddress();
            $shippingAddressExtensionAttributes = (null!== $shippingAddress->getExtensionAttributes())?
                $shippingAddress->getExtensionAttributes():
                $this->addressExtensionInterfaceFactory->create();

            $shippingAddressExtensionAttributes->setDoorNo($shippingAddress->getDoorNo());
            $shippingAddress->setExtensionAttributes($shippingAddressExtensionAttributes);
        }

        return $order;
    }
}
added 117 characters in body
Source Link
Amit Bera
  • 77.7k
  • 20
  • 124
  • 239
Loading
Source Link
Amit Bera
  • 77.7k
  • 20
  • 124
  • 239
Loading