3

How to get the gift message value in Magento2.1.7?

I am getting the gift message value as $getgiftMessage = $item->giftMessage();

But I am not able to get the value.

Any help would be appreciated.

3
  • Are you trying to fetch it from quote? Mar 22 '19 at 13:40
  • I am fetching it from order.
    – Amy
    Mar 22 '19 at 13:42
  • I think you will get only gift message id from there. Mar 22 '19 at 13:42
4

You can get Gift message for order item using below 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
}

I hope it will helpful :)

3
  • I want to call gift message in phtml file.
    – Amy
    Mar 22 '19 at 13:53
  • Then you can put this code in block file and call getGiftMessage() function Mar 22 '19 at 13:54
  • You can use \Magento\GiftMessage\Api\OrderRepositoryInterface to get the gift message for the whole order
    – Char
    Apr 20 at 12:45
0

In case if you need get message value on checkout/cart page you can use \Magento\GiftMessage\Model\MessageFactory

For example in your block:

protected $_messageFactory;

public function __construct(
    \Magento\GiftMessage\Model\MessageFactory $messageFactory
)
{
    $this->_messageFactory = $messageFactory;
}

public function getGiftMessage()
{
    $giftMessage = $this->messageFactory->create();
    $item = $this->getItem(); 
    if ($item->getGiftMessageId()) {
        $giftMessage->load($item->getGiftMessageId());
        return $giftMessage->getMessage();
    }

    return '';
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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