I am using sales_order_status_history_save_after
event for store data in my custom table.
I need also update comment with a prefix of commentId (entity_id)
in to sales_order_status_history
table.
While I am doing setComment data in observer than two times data stored in my custom table.
also, I have added log in to the observer file I found that my event call two times. I did not understand why its call two times and my data stored 2 times in my custom table.
is anyone knows how we can solve this issue??
here is the code:
public function execute(Observer $observer)
{
try {
$statusHistory = $observer->getEvent()->getStatusHistory();
$orderId = $statusHistory->getParentId();
$commentId = $statusHistory->getId();
$post = $this->request->getPostValue();
$oldComment = $statusHistory->getComment();
$newComment = "Comment Id : ".$commentId. $oldComment;
$statusHistory->setComment($newComment);
$_orderstatusdata = $post['order_reprint'];
$bulkInsert = [];
foreach ($_orderstatusdata as $orderstatusdata) {
if($orderstatusdata['is_reprint']){
$customdata = [];
$customdata['key1'] = 'val1';
$customdata['key2'] = 'val2';
$customdata['key3'] = 'val3';
$customdata['key4'] = 'val4';
$bulkInsert[] = $customdata;
}
}
$connection->insertMultiple('custom_table',$bulkInsert);
} catch (\Exception $e) {
$this->logger->error($e);
}
}