Skip to main content
added 1265 characters in body
Source Link

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);
    }
}

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??

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);
    }
}

Issue: Multipal time store data with event - Magento 2 - event observer called multiple time

Source Link

Issue: Multipal time store data with event - Magento 2

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??