0

i am using some custom attributes in customer address section.The attribute values are getting in quote_address table but not getting that in sale_order_address table. The file i used to do that field.xml

 xsi:noNamespaceSchemaLocation="urn:magento:framework:DataObject/etc/fieldset.xsd">
<scope id="global">
 <!--quote convert to sales-->
 <fieldset id="sales_convert_quote_address">            
     <field name="address_label">
         <aspect name="to_customer_address" />
     </field>
     <field name="house_name">
         <aspect name="to_customer_address" />
     </field>
     <field name="locations">
         <aspect name="to_customer_address" />
     </field>
     <field name="landmark">
         <aspect name="to_customer_address" />
     </field>
 </fieldset>
</scope>
</config>

This file was not worked for me.

1 Answer 1

-1

The following example is copying the quote table to the sales_order table. You need to create a event.xml and Observer.

event.xml

<?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="sales_model_service_quote_submit_before">
        <observer name="your_module_sales_model_service_quote_submit_before" instance="Your\Module\Observer\SaveToOrder" />
    </event>
</config>

SaveToOrder.php

class SaveToOrder implements \Magento\Framework\Event\ObserverInterface
{
    protected $_objectCopyService;

    public function __construct(
        Copy $objectCopyService
    ) {
        $this->_objectCopyService = $objectCopyService;
    }

    /**
     * List of attributes that should be added to an order.
     *
     * @var array
     */
    private $attributes = [
        'attributes_1',
        'attributes_2',
    ];

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $event = $observer->getEvent();
        $quote = $event->getData('quote');
        $order = $event->getData('order');

        //set attributes
        foreach ($this->attributes as $attribute) {
            if ($quote->hasData($attribute)) {
            $order->setData($attribute, $quote->getData($attribute));
            }
        }

        //copy order to sale_order
        $this->_objectCopyService->copyFieldsetToTarget(
            'sales_convert_quote',
            'to_order',
            $quote,
            $order
        );

        return $this;
    }
}

This article may helpful for you ! https://www.magetrend.com/blog/magento-2-add-custom-checkout-field/

2
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
    – Piyush
    Jan 12, 2023 at 5:21
  • Thanks for your advice.
    – WanMin
    Jan 12, 2023 at 6:16

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.