Magento – Magento 2 : Event Observer For Shipping Address Selection

event-observermagento2shipping-address

I am working on custom extension and need to execute my code when the customer clicks on "Next" button available in checkout.

I need an event observer in which, used shipping and billing address selected or added by customer.

See screenshot.

enter image description here

Best Answer

You should use plugin to get the shipping info:

Your di should be under etc folder: app/code/Extension/Vendor/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">
    <type name="Magento\Checkout\Model\ShippingInformationManagement">
        <plugin name="get_shipping_info" type="Vendor\Module\Plugin\Checkout\Model\ShippingInformationManagement" sortOrder="1"/>
    </type>
</config>

Your Plugin:

/**
     * @param \Magento\Checkout\Model\ShippingInformationManagement $subject
     * @param $cartId
     * @param \Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation
     */
    public function beforeSaveAddressInformation(
        \Magento\Checkout\Model\ShippingInformationManagement $subject,
        $cartId,
        \Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation
    )
    {
       ......
    }

    public function afterSaveAddressInformation(
        \Magento\Checkout\Model\ShippingInformationManagement $shipping,
         $result
    )
    {
        ......
    }
Related Topic