1

I am sending ajax request to save data in my custom field in sales_order table , It saves data successfully in case of exit, but not with out exit, any suggestion

//Not working
public function updateCommentSection($order)
{     
    if ($order) {
            $orderId =  $order->getEntityId();
            $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 

            $order = $objectManager->create('Magento\Sales\Model\Order')->load($orderId);
            $order->setSkCanceledAt('13');
            $order->save();

        }
}


//Working
public function updateCommentSection($order)
{     
    if ($order) {
            $orderId =  $order->getEntityId();
            $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 

            $order = $objectManager->create('Magento\Sales\Model\Order')->load($orderId);
            $order->setSkCanceledAt('13');
            $order->save();
            exit;
        }
}
  • did you used return statement? – i_ali Feb 8 at 13:43
  • 1
    where is your method called from and what is the $order object you get as parameter? It might be that you are somewhere in the process where some other method which has loaded the order object before your method, is saving it after. – HelgeB Feb 8 at 13:46
  • tried but not working – sheraz khan Feb 8 at 13:46
  • @HelgeB i get order object using $this->orderData->load($this->getOrderId()); – sheraz khan Feb 8 at 13:47
  • 1
    the exit means that nothing else can save the order object after your action. For me it looks like the order object is already loaded in the process, you are loading it again and save it directly. The other order object loaded before does not have your data and is most probably saved after your code. If you hook in the observer order_save_before, implement some checks and just add your attribute value without saving the order, it should work – HelgeB Feb 8 at 13:55
1

Actually Order Object is already loaded in the process from where i call this function , I was loading it again and save it directly , The other object loaded before does not have my data which i was saving, after saving new data,my older order object overrides the new data of this function.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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