0

I have created a custom column in order table of Magento 2 with the name:

order_detail_data

Now, I want to save some custom data in this column by using

sales_order_place_before

My Observer code against above-mentioned event is:

namespace Vendor\Module\Observer;

use Magento\Framework\Event\ObserverInterface;

class OrderPlacebeforeSaveData implements ObserverInterface
{

protected $_coreSession;

public function __construct(
    \Magento\Framework\Session\SessionManagerInterface $coreSession

) {
    $this->_coreSession = $coreSession;
}

public function execute(\Magento\Framework\Event\Observer $observer)
{
    $orderDetailData = $this->_coreSession->getOrderDetailSession();
    $order = $observer->getEvent()->getOrder();
    $order->setData('order_detail_data', json_encode($orderDetailData));
    $order->save();
}
}

But unfortunately, I am not able to save data in this column.

5
  • Did you get value in $orderDetailData ? Nov 26, 2018 at 7:35
  • yes i am getting an array in $orderDetailData and after that i convert into string Nov 26, 2018 at 7:37
  • Change your event to sales_order_place_after.
    – Sohel Rana
    Nov 26, 2018 at 8:17
  • my observer code is fine ? Nov 26, 2018 at 8:26
  • Is it working ? Nov 26, 2018 at 9:07

1 Answer 1

1

You can use sales_model_service_quote_submit_before event or as SohelRana said you can use sales_order_place_after and add this below code :

namespace Vendor\Module\Observer;

use Magento\Framework\Event\ObserverInterface;

class SalesOrderPlaceAfter implements ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        try {
            $orderDetailData = 'your value';
            $order = $observer->getEvent()->getOrder();
            $order->setData('order_detail_data', json_encode($orderDetailData));
            $order->save();
        } catch (\Exception $e) {
            error_log($e->getMessage());
        }
    }
}
5
  • sales_model_service_quote_submit_before and sales_order_place_after not saving my data to my custom database column. My data is saving in my logs but not in my database. Nov 26, 2018 at 10:15
  • In which table you create custom column? Nov 26, 2018 at 10:42
  • creating a column with name order_detail_data in database table sales_order_grid Nov 26, 2018 at 10:45
  • yes this is working. actually i was trying to store it in sales_order_grid table but it should be save in sales_order table. Nov 26, 2018 at 11:10
  • Yeah. Exactly I said to you. Happy coding !! Happy to Help :) Nov 26, 2018 at 11:11

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.