/** * @param \Magento\Checkout\Model\Type\Onepage $subject * @param array $result * @return $this */ public function afterSaveShippingMethod(\Magento\Checkout\Model\Type\Onepage $subject, array $result) { if (!$result) { $giftMessages = $this->request->getParam('giftmessage'); $quote = $subject->getQuote(); $this->message->add($giftMessages, $quote); } return $result; }
/** * 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'); } }
public function testAddWithQuoteAddressItem() { $entityId = 1; $giftMessages = ['quote_address_item' => [1 => ['from' => 'sender', 'to' => 'recipient', 'message' => 'message', 'address' => 'address']]]; $customerId = 42; $this->messageFactoryMock->expects($this->once())->method('create')->will($this->returnValue($this->giftMessageMock)); $this->quoteMock->expects($this->once())->method('getAddressById')->with('address')->will($this->returnValue($this->quoteAddressMock)); $this->quoteAddressMock->expects($this->once())->method('getItemById')->with($entityId)->will($this->returnValue($this->quoteAddressItemMock)); $this->quoteAddressItemMock->expects($this->once())->method('getGiftMessageId')->will($this->returnValue(0)); $this->giftMessageMock->expects($this->once())->method('setSender')->with('sender')->will($this->returnValue($this->giftMessageMock)); $this->giftMessageMock->expects($this->once())->method('setRecipient')->with('recipient')->will($this->returnValue($this->giftMessageMock)); $this->giftMessageMock->expects($this->once())->method('setMessage')->with('message')->will($this->returnValue($this->giftMessageMock)); $this->quoteMock->expects($this->once())->method('getCustomerId')->willReturn($customerId); $this->giftMessageMock->expects($this->once())->method('setCustomerId')->with($customerId)->will($this->returnValue($this->giftMessageMock)); $this->giftMessageMock->expects($this->once())->method('save'); $this->giftMessageMock->expects($this->once())->method('getId')->will($this->returnValue(33)); $this->quoteAddressItemMock->expects($this->once())->method('setGiftMessageId')->with(33)->will($this->returnValue($this->quoteAddressItemMock)); $this->quoteAddressItemMock->expects($this->once())->method('save')->will($this->returnValue($this->quoteAddressItemMock)); $this->model->add($giftMessages, $this->quoteMock); }
/** * @param \Magento\Multishipping\Model\Checkout\Type\Multishipping $subject * @param array|null $methods * @return void * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function beforeSetShippingMethods(\Magento\Multishipping\Model\Checkout\Type\Multishipping $subject, $methods) { $giftMessages = $this->request->getParam('giftmessage'); $quote = $subject->getQuote(); $this->message->add($giftMessages, $quote); }