/** * Retrieve gift message for item * * @return string */ public function getMessageText() { if ($this->getItem()->getGiftMessageId()) { $model = $this->_messageHelper->getGiftMessage($this->getItem()->getGiftMessageId()); return $this->escapeHtml($model->getMessage()); } return ''; }
/** * @param \Magento\Sales\Model\Convert\Quote $subject * @param Closure $proceed * @param \Magento\Sales\Model\Quote\Item\AbstractItem $item * @return Item * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function aroundItemToOrderItem(\Magento\Sales\Model\Convert\Quote $subject, Closure $proceed, \Magento\Sales\Model\Quote\Item\AbstractItem $item) { /** @var $orderItem Item */ $orderItem = $proceed($item); $isAvailable = $this->_helper->isMessagesAvailable('item', $item, $item->getStoreId()); $orderItem->setGiftMessageId($item->getGiftMessageId()); $orderItem->setGiftMessageAvailable($isAvailable); return $orderItem; }
/** * @param \Magento\Quote\Model\Quote\Item\ToOrderItem $subject * @param callable $proceed * @param \Magento\Quote\Model\Quote\Item\AbstractItem $item * @param array $additional * @return Item * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function aroundConvert(\Magento\Quote\Model\Quote\Item\ToOrderItem $subject, Closure $proceed, \Magento\Quote\Model\Quote\Item\AbstractItem $item, $additional = []) { /** @var $orderItem Item */ $orderItem = $proceed($item, $additional); $isAvailable = $this->_helper->isMessagesAllowed('item', $item, $item->getStoreId()); $orderItem->setGiftMessageId($item->getGiftMessageId()); $orderItem->setGiftMessageAvailable($isAvailable); return $orderItem; }
/** * Retrieve items allowed for gift messages. * * If no items available return false. * * @return array|false */ public function getItems() { $items = []; $allItems = $this->getQuote()->getAllItems(); foreach ($allItems as $item) { if ($this->_getGiftmessageSaveModel()->getIsAllowedQuoteItem($item) && $this->_messageHelper->isMessagesAllowed('item', $item, $this->getStore())) { // if item allowed $items[] = $item; } } if (sizeof($items)) { return $items; } return false; }
/** * Make sure that isMessagesAvailable is not called */ public function testGetInlineForCheckout() { $expectedHtml = '<a href="here">here</a>'; $layoutMock = $this->getMock('\\Magento\\Framework\\View\\Layout', array(), array(), '', false); $entityMock = $this->getMock('\\Magento\\Framework\\Object', array(), array(), '', false); $inlineMock = $this->getMock('Magento\\GiftMessage\\Block\\Message\\Inline', array('setId', 'setDontDisplayContainer', 'setEntity', 'setType', 'toHtml'), array(), '', false); $this->layoutFactoryMock->expects($this->once())->method('create')->will($this->returnValue($layoutMock)); $layoutMock->expects($this->once())->method('createBlock')->will($this->returnValue($inlineMock)); $inlineMock->expects($this->once())->method('setId')->will($this->returnSelf()); $inlineMock->expects($this->once())->method('setDontDisplayContainer')->will($this->returnSelf()); $inlineMock->expects($this->once())->method('setEntity')->with($entityMock)->will($this->returnSelf()); $inlineMock->expects($this->once())->method('setType')->will($this->returnSelf()); $inlineMock->expects($this->once())->method('toHtml')->will($this->returnValue($expectedHtml)); $this->assertEquals($expectedHtml, $this->helper->getInline('onepage_checkout', $entityMock)); }
/** * Check gift messages availability * * @param Item|null $item * @return bool|null|string */ public function isGiftMessagesAvailable($item = null) { if (is_null($item)) { return $this->_messageHelper->getIsMessagesAvailable('items', $this->getQuote(), $this->getStore()); } return $this->_messageHelper->getIsMessagesAvailable('item', $item, $this->getStore()); }
/** * {@inheritDoc} */ public function save($orderId, \Magento\GiftMessage\Api\Data\MessageInterface $giftMessage) { /** @var \Magento\Sales\Api\Data\OrderInterface $order */ $order = $this->orderFactory->create()->load($orderId); if (!$order->getEntityId()) { throw new NoSuchEntityException(__('There is no order with provided id')); } if (0 == $order->getTotalItemCount()) { throw new InputException(__('Gift Messages is not applicable for empty order')); } if ($order->getIsVirtual()) { throw new InvalidTransitionException(__('Gift Messages is not applicable for virtual products')); } if (!$this->helper->isMessagesAllowed('order', $order, $this->storeManager->getStore())) { throw new CouldNotSaveException(__('Gift Message is not available')); } $message = []; $message[$orderId] = ['type' => 'order', $giftMessage::CUSTOMER_ID => $giftMessage->getCustomerId(), $giftMessage::SENDER => $giftMessage->getSender(), $giftMessage::RECIPIENT => $giftMessage->getRecipient(), $giftMessage::MESSAGE => $giftMessage->getMessage()]; $this->giftMessageSaveModel->setGiftmessages($message); try { $this->giftMessageSaveModel->saveAllInOrder(); } catch (\Exception $e) { throw new CouldNotSaveException(__('Could not add gift message to order: "%1"', $e->getMessage()), $e); } return true; }
/** * Check gift messages availability * * @param Item|null $item * @return bool|null|string */ public function isGiftMessagesAvailable($item = null) { if ($item === null) { return $this->_messageHelper->isMessagesAllowed('items', $this->getQuote(), $this->getStore()); } return $this->_messageHelper->isMessagesAllowed('item', $item, $this->getStore()); }
/** * Duplicates giftmessage from order to quote on import or reorder * * @param \Magento\Framework\Event\Observer $observer * @return $this */ public function execute(\Magento\Framework\Event\Observer $observer) { $order = $observer->getEvent()->getOrder(); // Do not import giftmessage data if order is reordered if ($order->getReordered()) { return $this; } if (!$this->_giftMessageMessage->isMessagesAllowed('order', $order, $order->getStore())) { return $this; } $giftMessageId = $order->getGiftMessageId(); if ($giftMessageId) { $giftMessage = $this->_messageFactory->create()->load($giftMessageId)->setId(null)->save(); $observer->getEvent()->getQuote()->setGiftMessageId($giftMessage->getId()); } return $this; }
/** * Initialize gift message for entity * * @return $this */ protected function _initMessage() { $this->_giftMessage[$this->getItem()->getGiftMessageId()] = $this->_messageHelper->getGiftMessage($this->getItem()->getGiftMessageId()); // init default values for giftmessage form if (!$this->getMessage()->getSender()) { $this->getMessage()->setSender($this->getDefaultSender()); } if (!$this->getMessage()->getRecipient()) { $this->getMessage()->setRecipient($this->getDefaultRecipient()); } return $this; }
/** * Duplicates giftmessage from order item to quote item on import or reorder * * @param \Magento\Framework\Event\Observer $observer * @return $this */ public function execute(\Magento\Framework\Event\Observer $observer) { /** @var $orderItem \Magento\Sales\Model\Order\Item */ $orderItem = $observer->getEvent()->getOrderItem(); // Do not import giftmessage data if order is reordered $order = $orderItem->getOrder(); if ($order && $order->getReordered()) { return $this; } $isAvailable = $this->_giftMessageMessage->isMessagesAllowed('order_item', $orderItem, $orderItem->getStoreId()); if (!$isAvailable) { return $this; } /** @var $quoteItem \Magento\Quote\Model\Quote\Item */ $quoteItem = $observer->getEvent()->getQuoteItem(); if ($giftMessageId = $orderItem->getGiftMessageId()) { $giftMessage = $this->_messageFactory->create()->load($giftMessageId)->setId(null)->save(); $quoteItem->setGiftMessageId($giftMessage->getId()); } return $this; }
/** * Set gift message to item or quote * * @param \Magento\Sales\Model\Quote $quote * @param string $type * @param \Magento\GiftMessage\Service\V1\Data\Message $giftMessage * @param null|int $entityId * @return void * @throws \Magento\Framework\Exception\CouldNotSaveException * @throws \Magento\Framework\Exception\State\InvalidTransitionException */ protected function setMessage(\Magento\Sales\Model\Quote $quote, $type, $giftMessage, $entityId = null) { if (is_null($quote->getBillingAddress()->getCountryId())) { throw new InvalidTransitionException('Billing address is not set'); } // check if shipping address is set if (is_null($quote->getShippingAddress()->getCountryId())) { throw new InvalidTransitionException('Shipping address is not set'); } $configType = $type == 'quote' ? '' : 'items'; if (!$this->helper->getIsMessagesAvailable($configType, $quote, $this->storeManager->getStore())) { throw new CouldNotSaveException('Gift Message is not available'); } $message[$type][$entityId] = ['from' => $giftMessage->getSender(), 'to' => $giftMessage->getRecipient(), 'message' => $giftMessage->getMessage()]; try { $this->giftMessageManager->add($message, $quote); } catch (\Exception $e) { throw new CouldNotSaveException('Could not add gift message to shopping cart'); } }
/** * {@inheritDoc} */ public function save($cartId, \Magento\GiftMessage\Api\Data\MessageInterface $giftMessage) { /** * Quote. * * @var \Magento\Quote\Model\Quote $quote */ $quote = $this->quoteRepository->getActive($cartId); if (0 == $quote->getItemsCount()) { throw new InputException(__('Gift Messages is not applicable for empty cart')); } if ($quote->isVirtual()) { throw new InvalidTransitionException(__('Gift Messages is not applicable for virtual products')); } if (!$this->helper->isMessagesAllowed('quote', $quote, $this->storeManager->getStore())) { throw new CouldNotSaveException(__('Gift Message is not available')); } $this->giftMessageManager->setMessage($quote, 'quote', $giftMessage); return true; }
/** * {@inheritDoc} */ public function save($cartId, \Magento\GiftMessage\Api\Data\MessageInterface $giftMessage, $itemId) { /** * Quote. * * @var \Magento\Quote\Model\Quote $quote */ $quote = $this->quoteRepository->getActive($cartId); if (!($item = $quote->getItemById($itemId))) { throw new NoSuchEntityException(__('There is no product with provided itemId: %1 in the cart', $itemId)); } if ($item->getIsVirtual()) { throw new InvalidTransitionException(__('Gift Messages is not applicable for virtual products')); } if (!$this->helper->isMessagesAllowed('items', $quote, $this->storeManager->getStore())) { throw new CouldNotSaveException(__('Gift Message is not available')); } $this->giftMessageManager->setMessage($quote, 'quote_item', $giftMessage, $itemId); return true; }
/** * {@inheritDoc} */ public function save($orderId, $orderItemId, \Magento\GiftMessage\Api\Data\MessageInterface $giftMessage) { /** @var \Magento\Sales\Api\Data\OrderInterface $order */ $order = $this->orderFactory->create()->load($orderId); /** @var \Magento\Sales\Api\Data\OrderItemInterface $orderItem */ if (!($orderItem = $this->getItemById($orderId, $orderItemId))) { throw new NoSuchEntityException(__('There is no item with provided id in the order')); } if ($order->getIsVirtual()) { throw new InvalidTransitionException(__('Gift Messages is not applicable for virtual products')); } if (!$this->helper->isMessagesAllowed('order_item', $orderItem, $this->storeManager->getStore())) { throw new CouldNotSaveException(__('Gift Message is not available')); } $message = []; $message[$orderItemId] = ['type' => 'order_item', 'sender' => $giftMessage->getSender(), 'recipient' => $giftMessage->getRecipient(), 'message' => $giftMessage->getMessage()]; $this->giftMessageSaveModel->setGiftmessages($message); try { $this->giftMessageSaveModel->saveAllInOrder(); } catch (\Exception $e) { throw new CouldNotSaveException(__('Could not add gift message to order: "%1"', $e->getMessage()), $e); } return true; }
/** * Get items box message text for multishipping * * @param \Magento\Multishipping\Block\Checkout\Shipping $subject * @param callable $proceed * @param \Magento\Framework\DataObject $addressEntity * * @return string * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function aroundGetItemsBoxTextAfter(\Magento\Multishipping\Block\Checkout\Shipping $subject, \Closure $proceed, \Magento\Framework\DataObject $addressEntity) { $itemsBoxText = $proceed($addressEntity); return $itemsBoxText . $this->helper->getInline('multishipping_address', $addressEntity); }
/** * Initialize gift message for entity * * @return $this */ protected function _initMessage() { $this->_giftMessage = $this->_messageHelper->getGiftMessage($this->getEntity()->getGiftMessageId()); return $this; }
/** * Retrieve is gift message available for item (product) * * @param \Magento\Framework\Object $item * @return bool */ public function isGiftMessagesAvailable($item) { return $this->_giftMessageMessage->isMessagesAllowed('item', $item, $item->getStore()); }
/** * Indicates that block can display giftmessages form * * @return bool */ public function canDisplayGiftmessage() { return $this->_messageHelper->isMessagesAllowed('order', $this->getEntity(), $this->getEntity()->getStoreId()); }
/** * Indicates that block can display gift message form * * @return bool */ public function canDisplayGiftmessageForm() { $quote = $this->_sessionQuote->getQuote(); return $this->_messageHelper->isMessagesAllowed('items', $quote, $quote->getStore()); }
/** * Check availability of giftmessages for specified entity item * * @param \Magento\Framework\Object $item * @return bool */ public function isItemMessagesAvailable($item) { $type = substr($this->getType(), 0, 5) == 'multi' ? 'address_item' : 'item'; return $this->_giftMessageMessage->isMessagesAvailable($type, $item); }
/** * Indicates that block can display giftmessages form * * @return bool */ public function canDisplayGiftmessage() { return $this->_messageHelper->isMessagesAllowed('order_item', $this->getItem(), $this->getItem()->getOrder()->getStoreId()); }