I want to SET gift message programmatically in current order(one message for whole order) during Checkout. Currently, I trying using sales_order_save_after event to edit order but didn't get succeed.

any another approach to do the same?

3 Answers 3


Finally, I got the easiest way.

public function __construct(
    \Magento\Checkout\Model\Session $session,
    \Magento\Quote\Model\QuoteRepository $quoteRepository,
    \Magento\GiftMessage\Model\MessageFactory $messageFactory
) {
    $this->session = $session;
    $this->quoteRepository = $quoteRepository;
    $this->messageFactory = $messageFactory;
public function test(){
  $quoteId = $this->session->getQuote()->getId();
  $giftMessage = $this->messageFactory->create();
  $giftMessage->setMessage('your text');
  $giftObj = $giftMessage->save();

if you want to add information after placing order then you may use "checkout_onepage_controller_success_action" this event to add your custom data


You can get Gift message for order item using this code.

public function __construct(
    \Magento\GiftMessage\Api\OrderItemRepositoryInterface $orderItemGiftRepo
) {
    $this->orderItemGiftRepo = $orderItemGiftRepo;

public function getGiftMessage($orderId, $orderItemId)
    $giftMessage = $this->orderItemGiftRepo->get($orderId, $orderItemId);
    $giftMessage->getMessage(); // This will give you message

public function setGiftMessage($orderId)
 $giftMessage = $this->orderItemGiftRepo->get($orderId);

Hope this will help you !!

  • how do I set gift message for the whole order? Sep 30, 2019 at 8:32
  • you don't need to pass order item id then, skip that field. Sep 30, 2019 at 8:34
  • how can I set my message $giftMessage = $this->orderGiftRepo->get($order); $giftMessage->setMessage('custommessage'); Sep 30, 2019 at 9:39
  • code updated, please check Sep 30, 2019 at 10:11
  • this is not working as I try to do this just after order place and got below error No item with the provided ID was found in the Order. Verify the ID and try again Sep 30, 2019 at 10:27

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.