1

1)I'm creating custom order comment text box in sales->order->create new order .

2) Created order_comment column in sales_ order grid/quote.

3) I need to save order comment text box value into db.How to save comment in sales_order grid/quote table?

If anyone knows please explain me?

2 Answers 2

0

What you can do is use the \Magento\Sales\Api\OrderRepositoryInterface $order

Either use the order, or load it separately with

$comment = 'Add the comment you need...';
$order = $this->order->get(1); // Load your order
$order->addStatusHistoryComment($comment)
    ->setIsCustomerNotified(false)
    ->setEntityName('order')
    ->save(); 

Hope this helps.

2
  • geting error "SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (magento22.sales_order_status_history, CONSTRAINT SALES_ORDER_STATUS_HISTORY_PARENT_ID_SALES_ORDER_ENTITY_ID FOREIGN KEY (parent_id) REFERENCES sales_order (entity_id) ON DELETE CAS), query was: INSERT INTO sales_order_status_history (is_customer_notified, comment, status, entity_name) VALUES (?, ?, ?, ?)", Commented Mar 1, 2018 at 12:22
  • This can't work, because addStatusHistoryComment() returns a OrderStatusHistoryInterface object, so the following methods in your snippets are not chainable.
    – ermannob
    Commented Oct 31, 2018 at 14:21
0

Alternative way

// better to init related instances in __construct
$factory = \Magento\Framework\App\ObjectManager::getInstance()
    ->get(\Magento\Sales\Model\Order\Status\HistoryFactory::class);
$repository = \Magento\Framework\App\ObjectManager::getInstance()
    ->get(\Magento\Sales\Api\OrderStatusHistoryRepositoryInterface::class);

// $order - your order instance
$history = $factory->create();
$history->setParentId($order->getId())
    ->setComment('Your Comment')
    ->setStatus($order->getStatus())
    ->setIsVisibleOnFront(0)
    ->setIsCustomerNotified(0);

$repository->save($history);

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.