public function testSaveAllInOrder() { $message = [1 => ['from' => 'John Doe', 'to' => 'Jane Doe', 'message' => 'I love Magento', 'type' => 'order']]; $this->model->setGiftmessages($message); $messageMock = $this->getMock('\\Magento\\GiftMessage\\Model\\Message', [], [], '', false); $entityModelMock = $this->getMock('\\Magento\\Sales\\Model\\Order', [], [], '', false); $this->messageFactoryMock->expects($this->once())->method('create')->willReturn($messageMock); $messageMock->expects($this->once())->method('getEntityModelByType')->with('order')->willReturnSelf(); $messageMock->expects($this->once())->method('load')->with(1)->willReturn($entityModelMock); $messageMock->expects($this->atLeastOnce())->method('isMessageEmpty')->willReturn(false); $messageMock->expects($this->once())->method('save'); $entityModelMock->expects($this->once())->method('save'); $this->assertEquals($this->model, $this->model->saveAllInOrder()); }
/** * {@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; }
/** * {@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; }
/** * Check if allowed for gift message * * @param Item $item * @return bool */ public function isAllowedForGiftMessage($item) { return $this->_giftMessageSave->getIsAllowedQuoteItem($item); }