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.
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.
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 :)
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 '';
}