/** * Loads customer, quote and quote item by request params * * @return $this * @throws \Magento\Framework\Exception\LocalizedException */ protected function _initData() { $this->_customerId = (int) $this->getRequest()->getParam('customer_id'); if (!$this->_customerId) { throw new \Magento\Framework\Exception\LocalizedException(__('No customer ID defined.')); } $quoteItemId = (int) $this->getRequest()->getParam('id'); $websiteId = (int) $this->getRequest()->getParam('website_id'); try { $this->_quote = $this->quoteRepository->getForCustomer($this->_customerId); } catch (\Magento\Framework\Exception\NoSuchEntityException $e) { $this->_quote = $this->quoteRepository->create(); } $this->_quote->setWebsite($this->_objectManager->get('Magento\\Store\\Model\\StoreManagerInterface')->getWebsite($websiteId)); $this->_quoteItem = $this->_quote->getItemById($quoteItemId); if (!$this->_quoteItem) { throw new LocalizedException(__('Please correct the quote items and try again.')); } return $this; }
/** * @param array $giftMessages * @param \Magento\Quote\Model\Quote $quote * @return $this * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function add($giftMessages, $quote) { if (!is_array($giftMessages)) { return $this; } // types are 'quote', 'quote_item', etc foreach ($giftMessages as $type => $giftMessageEntities) { foreach ($giftMessageEntities as $entityId => $message) { $giftMessage = $this->messageFactory->create(); switch ($type) { case 'quote': $entity = $quote; break; case 'quote_item': $entity = $quote->getItemById($entityId); break; case 'quote_address': $entity = $quote->getAddressById($entityId); break; case 'quote_address_item': $entity = $quote->getAddressById($message['address'])->getItemById($entityId); break; default: $entity = $quote; break; } if ($entity->getGiftMessageId()) { $giftMessage->load($entity->getGiftMessageId()); } if (trim($message['message']) == '') { if ($giftMessage->getId()) { try { $giftMessage->delete(); $entity->setGiftMessageId(0)->save(); } catch (\Exception $e) { } } continue; } try { $giftMessage->setSender($message['from'])->setRecipient($message['to'])->setMessage($message['message'])->setCustomerId($quote->getCustomerId())->save(); $entity->setGiftMessageId($giftMessage->getId())->save(); } catch (\Exception $e) { } } } return $this; }