How To

Magento 2: How to Save Custom Field Value to quote_address for Multi-Shipping Orders

Hello Magento Friends,

In Magento 2, the checkout process allows customers to choose multiple shipping addresses for an order, which is a feature known as “multi-shipping.” When using this feature, it’s important to manage and store additional custom data for each shipping address in a structured way. A common requirement is saving a custom field value on the quote_address model for each shipping address during a multi-shipping order process.

Steps to Save Custom Field Value to quote_address for Multi-Shipping Orders in Magento 2:

Step 1: First, we need to create a “events.xml“ file inside our extension at the following path

app\code\Vendor\Extension\etc\events.xml

Then add the code as follows

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="checkout_controller_multishipping_shipping_post">
        <observer name="custom_multishipping_observer" instance="Vendor\Extension\Observer\SaveCustomFieldToQuoteAddress" />
    </event>
</config>

Step 2: After that, we need to create an “SaveCustomFieldToQuoteAddress.php” file inside our extension at the following path

app\code\Vendor\Extension\Observer\SaveCustomFieldToQuoteAddress.php

And add the code as given below

<?php

namespace Vendor\Extension\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Quote\Api\CartRepositoryInterface;

class SaveCustomFieldToQuoteAddress implements ObserverInterface
{
    protected $quoteRepository;

    public function __construct(
 CartRepositoryInterface $quoteRepository
    ) {
 $this->quoteRepository = $quoteRepository;
    }

    public function execute(Observer $observer)
    {
 $request = $observer->getEvent()->getRequest();
 $quote = $observer->getEvent()->getQuote();

 // Assuming your custom field is 'custom_field' and is sent via POST
 $customFieldValue = $request->getPostValue('customfield');
 
 // Check if value exists
 if (is_array($customFieldValue) && !empty($customFieldValue)) {
     // Iterate through each shipping address and assign the correct value
     foreach ($quote->getAllShippingAddresses() as $address) {
         $addressId = $address->getId();

         // Check if a custom field value exists for this address ID
         if (isset($customFieldValue[$addressId])) {
             $address->setData('customfield', $customFieldValue[$addressId]);
         }
     }
 }
    // Save the quote to persist the data
    $this->quoteRepository->save($quote);
    }
}
?>

Conclusion:

By following these steps, you can easily save and manage custom field values for multi-shipping orders in Magento 2. This is useful for scenarios where you need to capture additional data specific to each shipping address, such as special delivery instructions or custom notes. These steps ensure that you can capture and store custom information tied to shipping addresses for multi-shipping orders, and extend the functionality of Magento 2’s checkout process.

Happy Coding!

Click to rate this post!
[Total: 0 Average: 0]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

Recent Posts

Best Beginners Guide to Shopify Balance Account

If you are a Shopify admin, using a Shopify Balance Account for your business revenue…

5 hours ago

8 Best Social Login Apps for Shopify Store in 2024

Running an eCommerce business can be incredibly demanding, leaving entrepreneurs little time to focus on…

5 hours ago

Generating Thumbnails with Spatie Media Library in Laravel 11: A Step-by-Step Guide

Generating image thumbnails is a common requirement in web applications, especially when handling media-heavy content.…

1 day ago

Enhancing Web Application Security with Laravel’s Built-In Features

In today’s digital landscape, web application security is paramount. As a powerful PHP framework, Laravel…

2 days ago

Magento 2 Extensions Digest October 2024 (New Release & Updates)

October was an exciting month for MageComp! From significant updates across our Magento 2 extension…

2 days ago

Improving Error Handling and Transition Management in Remix with useRouteError and useViewTransitionState

In modern web development, seamless navigation and state management are crucial for delivering a smooth…

1 week ago