1

I have create a custom field(zipcode) in sales_order and sales_order_grid, after that i try to save to database while click placeorder the order in frontend,so try to use checkout_submit_all_after event, But its not save my custom field value.

what i did:

vendor/module/etc/frontend/events.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="checkout_submit_all_after">
          <observer name="custom_checkout_submit_all_after"
                    instance="vendor\Module\Observer\SalesOrderPlaceAfter"
                    />
      </event>
  </config>

Then create a Observer to to save Custom Field Value.

vendor/module/Observer/SalesOrderPlaceAfter.php:

    use Magento\Framework\Event\ObserverInterface;

    class SalesOrderPlaceAfter implements ObserverInterface
    {

        public function execute(\Magento\Framework\Event\Observer $observer)
        {
            try {
                $order = $observer->getEvent()->getOrder();
                $order->setZipcode("604503");
                $order->save();
            } catch (\Exception $e) {
                error_log($e->getMessage());
            }
        }
    }

But its have save My custom Field value, Suggest me How to fix this.

  • 1
    Move the events.xml to vendor\module\etc\events.xml. – Khoa TruongDinh Jul 19 '17 at 12:08
1

If we want to set the custom attribute data to order, it's better if we use before saving observer. For example: sales_model_service_quote_submit_before.

    $order = $observer->getOrder();
    $order->setZipcode($feeAmount);

Order table and order grid are in sync:

app/code/Vendor/Module/etc/adminhtml/di.xml

   <virtualType name="Magento\Sales\Model\ResourceModel\Order\Grid">
        <arguments>
            <argument name="columns" xsi:type="array">
                <item name="your_column" xsi:type="string">
                     sales_order.your_column
                </item>
            </argument>
        </arguments>
    </virtualType>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.