Skip to content
Snippets Groups Projects
Commit 7679766f authored by Cari Spruiell's avatar Cari Spruiell
Browse files

Merge branches 'MAGETWO-27932-Create-Gift-Message-Extension-For-Order' and...

Merge branches 'MAGETWO-27932-Create-Gift-Message-Extension-For-Order' and 'develop' of github.scm.corp.ebay.com:magento-api/magento2ce into develop
parents 2d1bc646 9d1dcefc
Branches
No related merge requests found
Showing
with 636 additions and 49 deletions
......@@ -8,7 +8,7 @@ namespace Magento\GiftMessage\Api;
interface CartRepositoryInterface
{
/**
* Returns the gift message for a specified order.
* Return the gift message for a specified order.
*
* @param int $cartId The shopping cart ID.
* @return \Magento\GiftMessage\Api\Data\MessageInterface Gift message.
......@@ -16,7 +16,7 @@ interface CartRepositoryInterface
public function get($cartId);
/**
* Sets the gift message for an entire order.
* Set the gift message for an entire order.
*
* @param int $cartId The cart ID.
* @param \Magento\GiftMessage\Api\Data\MessageInterface $giftMessage The gift message.
......
......@@ -18,14 +18,14 @@ interface MessageInterface extends \Magento\Framework\Api\ExtensibleDataInterfac
/**#@-*/
/**
* Returns the gift message ID.
* Return the gift message ID.
*
* @return int|null Gift message ID. Otherwise, null.
*/
public function getGiftMessageId();
/**
* Sets the gift message ID.
* Set the gift message ID.
*
* @param int|null $id
* @return $this
......@@ -33,14 +33,14 @@ interface MessageInterface extends \Magento\Framework\Api\ExtensibleDataInterfac
public function setGiftMessageId($id);
/**
* Returns the customer ID.
* Return the customer ID.
*
* @return int|null Customer ID. Otherwise, null.
*/
public function getCustomerId();
/**
* Sets the customer ID.
* Set the customer ID.
*
* @param int|null $id
* @return $this
......@@ -48,14 +48,14 @@ interface MessageInterface extends \Magento\Framework\Api\ExtensibleDataInterfac
public function setCustomerId($id);
/**
* Returns the sender name.
* Return the sender name.
*
* @return string Sender name.
*/
public function getSender();
/**
* Sets the sender name.
* Set the sender name.
*
* @param string $sender
* @return $this
......@@ -63,14 +63,14 @@ interface MessageInterface extends \Magento\Framework\Api\ExtensibleDataInterfac
public function setSender($sender);
/**
* Returns the recipient name.
* Return the recipient name.
*
* @return string Recipient name.
*/
public function getRecipient();
/**
* Gets the recipient name.
* Get the recipient name.
*
* @param string $recipient
* @return $this
......@@ -78,14 +78,14 @@ interface MessageInterface extends \Magento\Framework\Api\ExtensibleDataInterfac
public function setRecipient($recipient);
/**
* Returns the message text.
* Return the message text.
*
* @return string Message text.
*/
public function getMessage();
/**
* Sets the message text.
* Set the message text.
*
* @param string $message
* @return $this
......
......@@ -8,7 +8,7 @@ namespace Magento\GiftMessage\Api;
interface ItemRepositoryInterface
{
/**
* Returns the gift message for a specified item in a specified shopping cart.
* Return the gift message for a specified item in a specified shopping cart.
*
* @param int $cartId The shopping cart ID.
* @param int $itemId The item ID.
......@@ -18,7 +18,7 @@ interface ItemRepositoryInterface
public function get($cartId, $itemId);
/**
* Sets the gift message for a specified item in a specified shopping cart.
* Set the gift message for a specified item in a specified shopping cart.
*
* @param int $cartId The cart ID.
* @param \Magento\GiftMessage\Api\Data\MessageInterface $giftMessage The gift message.
......
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\GiftMessage\Api;
interface OrderItemRepositoryInterface
{
/**
* Return the gift message for a specified item in a specified order.
*
* @param int $orderId The order ID.
* @param int $orderItemId The item ID.
* @return \Magento\GiftMessage\Api\Data\MessageInterface Gift message.
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function get($orderId, $orderItemId);
/**
* Set the gift message for a specified item in a specified order.
*
* @param int $orderId The order ID.
* @param int $orderItemId The item ID.
* @param \Magento\GiftMessage\Api\Data\MessageInterface $giftMessage The gift message.
* @return bool
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @throws \Magento\Framework\Exception\State\InvalidTransitionException
* @throws \Magento\Framework\Exception\CouldNotSaveException
*/
public function save($orderId, $orderItemId, \Magento\GiftMessage\Api\Data\MessageInterface $giftMessage);
}
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\GiftMessage\Api;
interface OrderRepositoryInterface
{
/**
* Return the gift message for a specified order.
*
* @param int $orderId The order ID.
* @return \Magento\GiftMessage\Api\Data\MessageInterface Gift message.
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
public function get($orderId);
/**
* Set the gift message for an entire order.
*
* @param int $orderId The order ID.
* @param \Magento\GiftMessage\Api\Data\MessageInterface $giftMessage The gift message.
* @return bool
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @throws \Magento\Framework\Exception\InputException
* @throws \Magento\Framework\Exception\CouldNotSaveException
* @throws \Magento\Framework\Exception\State\InvalidTransitionException
*/
public function save($orderId, \Magento\GiftMessage\Api\Data\MessageInterface $giftMessage);
}
......@@ -47,6 +47,6 @@ class Form extends \Magento\Backend\Block\Template
public function canDisplayGiftmessageForm()
{
$quote = $this->_sessionQuote->getQuote();
return $this->_messageHelper->getIsMessagesAvailable('items', $quote, $quote->getStore());
return $this->_messageHelper->isMessagesAllowed('items', $quote, $quote->getStore());
}
}
......@@ -52,7 +52,7 @@ class Items extends \Magento\Backend\Block\Template
if (!$item) {
return false;
}
return $this->_messageHelper->getIsMessagesAvailable('item', $item, $item->getStoreId());
return $this->_messageHelper->isMessagesAllowed('item', $item, $item->getStoreId());
}
/**
......
......@@ -317,7 +317,7 @@ class Inline extends \Magento\Framework\View\Element\Template
*/
public function isMessagesAvailable()
{
return $this->_giftMessageMessage->isMessagesAvailable('quote', $this->getEntity());
return $this->_giftMessageMessage->isMessagesAllowed('quote', $this->getEntity());
}
/**
......@@ -329,7 +329,7 @@ class Inline extends \Magento\Framework\View\Element\Template
public function isItemMessagesAvailable($item)
{
$type = substr($this->getType(), 0, 5) == 'multi' ? 'address_item' : 'item';
return $this->_giftMessageMessage->isMessagesAvailable($type, $item);
return $this->_giftMessageMessage->isMessagesAllowed($type, $item);
}
/**
......
......@@ -109,7 +109,7 @@ class Message extends \Magento\Framework\App\Helper\AbstractHelper
*/
public function getInline($type, \Magento\Framework\Object $entity, $dontDisplayContainer = false)
{
if (!$this->skipPage($type) && !$this->isMessagesAvailable($type, $entity)) {
if (!$this->skipPage($type) && !$this->isMessagesAllowed($type, $entity)) {
return '';
}
return $this->_layoutFactory->create()->createBlock('Magento\GiftMessage\Block\Message\Inline')
......@@ -129,7 +129,7 @@ class Message extends \Magento\Framework\App\Helper\AbstractHelper
}
/**
* Check availability of giftmessages for specified entity.
* Check if giftmessages is allowed for specified entity.
*
* @param string $type
* @param \Magento\Framework\Object $entity
......@@ -137,12 +137,16 @@ class Message extends \Magento\Framework\App\Helper\AbstractHelper
* @return bool|string|null
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function isMessagesAvailable($type, \Magento\Framework\Object $entity, $store = null)
public function isMessagesAllowed($type, \Magento\Framework\Object $entity, $store = null)
{
if ($type == 'items') {
$items = $entity->getAllItems();
if (!is_array($items) || empty($items)) {
return $this->scopeConfig->getValue(self::XPATH_CONFIG_GIFT_MESSAGE_ALLOW_ITEMS, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store);
return $this->scopeConfig->getValue(
self::XPATH_CONFIG_GIFT_MESSAGE_ALLOW_ITEMS,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
$store
);
}
if ($entity instanceof \Magento\Quote\Model\Quote) {
$_type = $entity->getIsMultiShipping() ? 'address_item' : 'item';
......@@ -153,7 +157,7 @@ class Message extends \Magento\Framework\App\Helper\AbstractHelper
if ($item->getParentItem()) {
continue;
}
if ($this->isMessagesAvailable($_type, $item, $store)) {
if ($this->isMessagesAllowed($_type, $item, $store)) {
return true;
}
}
......@@ -177,7 +181,11 @@ class Message extends \Magento\Framework\App\Helper\AbstractHelper
$store
);
} else {
return $this->scopeConfig->getValue(self::XPATH_CONFIG_GIFT_MESSAGE_ALLOW_ORDER, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store);
return $this->scopeConfig->getValue(
self::XPATH_CONFIG_GIFT_MESSAGE_ALLOW_ORDER,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
$store
);
}
return false;
}
......@@ -191,7 +199,10 @@ class Message extends \Magento\Framework\App\Helper\AbstractHelper
*/
protected function _getDependenceFromStoreConfig($productGiftMessageAllow, $store = null)
{
$result = $this->scopeConfig->getValue(self::XPATH_CONFIG_GIFT_MESSAGE_ALLOW_ITEMS, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store);
$result = $this->scopeConfig->getValue(
self::XPATH_CONFIG_GIFT_MESSAGE_ALLOW_ITEMS,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store
);
if ($productGiftMessageAllow === '' || is_null($productGiftMessageAllow)) {
return $result;
} else {
......@@ -199,19 +210,6 @@ class Message extends \Magento\Framework\App\Helper\AbstractHelper
}
}
/**
* Alias for isMessagesAvailable(...)
*
* @param string $type
* @param \Magento\Framework\Object $entity
* @param \Magento\Store\Model\Store|int|null $store
* @return bool|null|string
*/
public function getIsMessagesAvailable($type, \Magento\Framework\Object $entity, $store = null)
{
return $this->isMessagesAvailable($type, $entity, $store);
}
/**
* Retrieve escaped and preformated gift message text for specified entity
*
......@@ -293,7 +291,7 @@ class Message extends \Magento\Framework\App\Helper\AbstractHelper
public function getAvailableForQuoteItems($quote, $store = null)
{
foreach ($quote->getAllItems() as $item) {
if ($this->isMessagesAvailable('item', $item, $store)) {
if ($this->isMessagesAllowed('item', $item, $store)) {
return true;
}
}
......@@ -311,7 +309,7 @@ class Message extends \Magento\Framework\App\Helper\AbstractHelper
public function getAvailableForAddressItems($items, $store = null)
{
foreach ($items as $item) {
if ($this->isMessagesAvailable('address_item', $item, $store)) {
if ($this->isMessagesAllowed('address_item', $item, $store)) {
return true;
}
}
......
......@@ -107,7 +107,7 @@ class CartRepository implements \Magento\GiftMessage\Api\CartRepositoryInterface
if ($quote->isVirtual()) {
throw new InvalidTransitionException(__('Gift Messages is not applicable for virtual products'));
}
if (!$this->helper->getIsMessagesAvailable('quote', $quote, $this->storeManager->getStore())) {
if (!$this->helper->isMessagesAllowed('quote', $quote, $this->storeManager->getStore())) {
throw new CouldNotSaveException(__('Gift Message is not available'));
}
$this->giftMessageManager->setMessage($quote, 'quote', $giftMessage);
......
......@@ -121,7 +121,7 @@ class ItemRepository implements \Magento\GiftMessage\Api\ItemRepositoryInterface
if ($item->getIsVirtual()) {
throw new InvalidTransitionException(__('Gift Messages is not applicable for virtual products'));
}
if (!$this->helper->getIsMessagesAvailable('items', $quote, $this->storeManager->getStore())) {
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);
......
......@@ -74,7 +74,7 @@ class Observer extends \Magento\Framework\Object
return $this;
}
if (!$this->_giftMessageMessage->isMessagesAvailable('order', $order, $order->getStore())) {
if (!$this->_giftMessageMessage->isMessagesAllowed('order', $order, $order->getStore())) {
return $this;
}
$giftMessageId = $order->getGiftMessageId();
......@@ -102,7 +102,7 @@ class Observer extends \Magento\Framework\Object
return $this;
}
$isAvailable = $this->_giftMessageMessage->isMessagesAvailable(
$isAvailable = $this->_giftMessageMessage->isMessagesAllowed(
'order_item',
$orderItem,
$orderItem->getStoreId()
......
<?php
/**
*
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\GiftMessage\Model;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Exception\State\InvalidTransitionException;
/**
* Order item gift message repository object.
*/
class OrderItemRepository implements \Magento\GiftMessage\Api\OrderItemRepositoryInterface
{
/**
* Order factory.
*
* @var \Magento\Sales\Model\OrderFactory
*/
protected $orderFactory;
/**
* Store manager interface.
*
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $storeManager;
/**
* Gift message save model.
*
* @var \Magento\GiftMessage\Model\Save
*/
protected $giftMessageSaveModel;
/**
* Message helper.
*
* @var \Magento\GiftMessage\Helper\Message
*/
protected $helper;
/**
* Message factory.
*
* @var \Magento\GiftMessage\Model\MessageFactory
*/
protected $messageFactory;
/**
* @param \Magento\Sales\Model\OrderFactory $orderFactory
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\GiftMessage\Model\Save $giftMessageSaveModel
* @param \Magento\GiftMessage\Helper\Message $helper
* @param MessageFactory $messageFactory
*/
public function __construct(
\Magento\Sales\Model\OrderFactory $orderFactory,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\GiftMessage\Model\Save $giftMessageSaveModel,
\Magento\GiftMessage\Helper\Message $helper,
\Magento\GiftMessage\Model\MessageFactory $messageFactory
) {
$this->orderFactory = $orderFactory;
$this->giftMessageSaveModel = $giftMessageSaveModel;
$this->storeManager = $storeManager;
$this->helper = $helper;
$this->messageFactory = $messageFactory;
}
/**
* {@inheritDoc}
*/
public function get($orderId, $orderItemId)
{
/** @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 (!$this->helper->isMessagesAllowed('order_item', $orderItem, $this->storeManager->getStore())) {
throw new NoSuchEntityException(
__('There is no item with provided id in the order or gift message isn\'t allowed')
);
}
$messageId = $orderItem->getGiftMessageId();
if (!$messageId) {
throw new NoSuchEntityException(__('There is no item with provided id in the order'));
}
return $this->messageFactory->create()->load($messageId);
}
/**
* {@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 order item by id
*
* @param int $orderId
* @param int $orderItemId
* @return \Magento\Sales\Api\Data\OrderItemInterface|bool
*/
protected function getItemById($orderId, $orderItemId)
{
/** @var \Magento\Sales\Api\Data\OrderInterface $order */
$order = $this->orderFactory->create()->load($orderId);
/** @var \Magento\Sales\Api\Data\OrderItemInterface $item */
foreach ($order->getItems() as $item) {
if ($item->getItemId() === $orderItemId) {
return $item;
}
}
return false;
}
}
<?php
/**
*
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\GiftMessage\Model;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\State\InvalidTransitionException;
use Magento\Framework\Exception\NoSuchEntityException;
/**
* Order gift message repository object.
*/
class OrderRepository implements \Magento\GiftMessage\Api\OrderRepositoryInterface
{
/**
* Order factory.
*
* @var \Magento\Sales\Model\OrderFactory
*/
protected $orderFactory;
/**
* Store manager interface.
*
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $storeManager;
/**
* Gift message save model.
*
* @var \Magento\GiftMessage\Model\Save
*/
protected $giftMessageSaveModel;
/**
* Message helper.
*
* @var \Magento\GiftMessage\Helper\Message
*/
protected $helper;
/**
* Message factory.
*
* @var \Magento\GiftMessage\Model\MessageFactory
*/
protected $messageFactory;
/**
* @param \Magento\Sales\Model\OrderFactory $orderFactory
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\GiftMessage\Model\Save $giftMessageSaveModel
* @param \Magento\GiftMessage\Helper\Message $helper
* @param MessageFactory $messageFactory
*/
public function __construct(
\Magento\Sales\Model\OrderFactory $orderFactory,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\GiftMessage\Model\Save $giftMessageSaveModel,
\Magento\GiftMessage\Helper\Message $helper,
\Magento\GiftMessage\Model\MessageFactory $messageFactory
) {
$this->orderFactory = $orderFactory;
$this->giftMessageSaveModel = $giftMessageSaveModel;
$this->storeManager = $storeManager;
$this->helper = $helper;
$this->messageFactory = $messageFactory;
}
/**
* {@inheritDoc}
*/
public function get($orderId)
{
/** @var \Magento\Sales\Api\Data\OrderInterface $order */
$order = $this->orderFactory->create()->load($orderId);
if (!$this->helper->isMessagesAllowed('order', $order, $this->storeManager->getStore())) {
throw new NoSuchEntityException(
__('There is no order with provided id or gift message isn\'t allowed')
);
}
$messageId = $order->getGiftMessageId();
if (!$messageId) {
throw new NoSuchEntityException(__('There is no item with provided id in the order'));
}
return $this->messageFactory->create()->load($messageId);
}
/**
* {@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;
}
}
<?php
/**
*
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\GiftMessage\Model\Plugin;
use Magento\Framework\Exception\NoSuchEntityException;
class OrderGet
{
/** @var \Magento\GiftMessage\Api\OrderRepositoryInterface */
protected $giftMessageOrderRepository;
/** @var \Magento\GiftMessage\Api\OrderItemRepositoryInterface */
protected $giftMessageOrderItemRepository;
/** @var \Magento\Sales\Api\Data\OrderExtensionFactory */
protected $orderExtensionFactory;
/** @var \Magento\Sales\Api\Data\OrderItemExtensionFactory */
protected $orderItemExtensionFactory;
/**
* Init plugin
*
* @param \Magento\GiftMessage\Api\OrderRepositoryInterface $giftMessageOrderRepository
* @param \Magento\GiftMessage\Api\OrderItemRepositoryInterface $giftMessageOrderItemRepository
* @param \Magento\Sales\Api\Data\OrderExtensionFactory $orderExtensionFactory
* @param \Magento\Sales\Api\Data\OrderItemExtensionFactory $orderItemExtensionFactory
*/
public function __construct(
\Magento\GiftMessage\Api\OrderRepositoryInterface $giftMessageOrderRepository,
\Magento\GiftMessage\Api\OrderItemRepositoryInterface $giftMessageOrderItemRepository,
\Magento\Sales\Api\Data\OrderExtensionFactory $orderExtensionFactory,
\Magento\Sales\Api\Data\OrderItemExtensionFactory $orderItemExtensionFactory
) {
$this->giftMessageOrderRepository = $giftMessageOrderRepository;
$this->giftMessageOrderItemRepository = $giftMessageOrderItemRepository;
$this->orderExtensionFactory = $orderExtensionFactory;
$this->orderItemExtensionFactory = $orderItemExtensionFactory;
}
/**
* Get gift message
*
* @param \Magento\Sales\Api\OrderRepositoryInterface $subject
* @param callable $proceed
* @param int $orderId
* @return \Magento\Sales\Api\Data\OrderInterface
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function aroundGet(
\Magento\Sales\Api\OrderRepositoryInterface $subject,
\Closure $proceed,
$orderId
) {
/** @var \Magento\Sales\Api\Data\OrderInterface $resultOrder */
$resultOrder = $proceed($orderId);
$resultOrder = $this->getOrderGiftMessage($resultOrder);
$resultOrder = $this->getOrderItemGiftMessage($resultOrder);
return $resultOrder;
}
/**
* Get gift message for order
*
* @param \Magento\Sales\Api\Data\OrderInterface $order
* @return \Magento\Sales\Api\Data\OrderInterface
*/
protected function getOrderGiftMessage(\Magento\Sales\Api\Data\OrderInterface $order)
{
if ($order->getExtensionAttributes() && $order->getExtensionAttributes()->getGiftMessage()) {
return $order;
}
try {
/** @var \Magento\GiftMessage\Api\Data\MessageInterface $giftMessage */
$giftMessage = $this->giftMessageOrderRepository->get($order->getEntityId());
} catch (NoSuchEntityException $e) {
return $order;
}
/** @var \Magento\Sales\Api\Data\OrderExtension $orderExtension */
$orderExtension = $this->orderExtensionFactory->create();
$orderExtension->setGiftMessage($giftMessage);
$order->setExtensionAttributes($orderExtension);
return $order;
}
/**
* Get gift message for items of order
*
* @param \Magento\Sales\Api\Data\OrderInterface $order
* @return \Magento\Sales\Api\Data\OrderInterface
*/
protected function getOrderItemGiftMessage(\Magento\Sales\Api\Data\OrderInterface $order)
{
if (null !== $order->getItems()) {
/** @var \Magento\Sales\Api\Data\OrderItemInterface $orderItem */
foreach ($order->getItems() as $orderItem) {
if ($orderItem->getExtensionAttributes() && $orderItem->getExtensionAttributes()->getGiftMessage()) {
continue;
}
try {
/* @var \Magento\GiftMessage\Api\Data\MessageInterface $giftMessage */
$giftMessage = $this->giftMessageOrderItemRepository->get(
$order->getEntityId(),
$orderItem->getItemId()
);
} catch (NoSuchEntityException $e) {
continue;
}
/** @var \Magento\Sales\Api\Data\OrderItemExtension $orderItemExtension */
$orderItemExtension = $this->orderItemExtensionFactory->create();
$orderItemExtension->setGiftMessage($giftMessage);
$orderItem->setExtensionAttributes($orderItemExtension);
}
}
return $order;
}
}
<?php
/**
*
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\GiftMessage\Model\Plugin;
use Magento\Framework\Exception\CouldNotSaveException;
class OrderSave
{
/** @var \Magento\GiftMessage\Api\OrderRepositoryInterface */
protected $giftMessageOrderRepository;
/** @var \Magento\GiftMessage\Api\OrderItemRepositoryInterface */
protected $giftMessageOrderItemRepository;
/**
* Init plugin
*
* @param \Magento\GiftMessage\Api\OrderRepositoryInterface $giftMessageOrderRepository
* @param \Magento\GiftMessage\Api\OrderItemRepositoryInterface $giftMessageOrderItemRepository
*/
public function __construct(
\Magento\GiftMessage\Api\OrderRepositoryInterface $giftMessageOrderRepository,
\Magento\GiftMessage\Api\OrderItemRepositoryInterface $giftMessageOrderItemRepository
) {
$this->giftMessageOrderRepository = $giftMessageOrderRepository;
$this->giftMessageOrderItemRepository = $giftMessageOrderItemRepository;
}
/**
* Save gift message
*
* @param \Magento\Sales\Api\OrderRepositoryInterface $subject
* @param callable $proceed
* @param \Magento\Sales\Api\Data\OrderInterface $order
* @return \Magento\Sales\Api\Data\OrderInterface
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function aroundSave(
\Magento\Sales\Api\OrderRepositoryInterface $subject,
\Closure $proceed,
\Magento\Sales\Api\Data\OrderInterface $order
) {
/** @var \Magento\Sales\Api\Data\OrderInterface $resultOrder */
$resultOrder = $proceed($order);
$resultOrder = $this->saveOrderGiftMessage($resultOrder);
$resultOrder = $this->saveOrderItemGiftMessage($resultOrder);
return $resultOrder;
}
/**
* Save gift message for order
*
* @param \Magento\Sales\Api\Data\OrderInterface $order
* @return \Magento\Sales\Api\Data\OrderInterface
*/
protected function saveOrderGiftMessage(\Magento\Sales\Api\Data\OrderInterface $order)
{
if (!is_null($order->getExtensionAttributes())) {
/* @var \Magento\GiftMessage\Api\Data\MessageInterface $giftMessage */
$giftMessage = $order->getExtensionAttributes()->getGiftMessage();
try {
$this->giftMessageOrderRepository->save($order->getEntityId(), $giftMessage);
} catch (\Exception $e) {
throw new CouldNotSaveException(__('Could not add gift message to order: "%1"', $e->getMessage()), $e);
}
}
return $order;
}
/**
* Save gift message for items of order
*
* @param \Magento\Sales\Api\Data\OrderInterface $order
* @return \Magento\Sales\Api\Data\OrderInterface
*/
protected function saveOrderItemGiftMessage(\Magento\Sales\Api\Data\OrderInterface $order)
{
if (null !== $order->getItems()) {
/** @var \Magento\Sales\Api\Data\OrderItemInterface $orderItem */
foreach ($order->getItems() as $orderItem) {
if (!is_null($orderItem->getExtensionAttributes())) {
/* @var \Magento\GiftMessage\Api\Data\MessageInterface $giftMessage */
$giftMessage = $orderItem->getExtensionAttributes()->getGiftMessage();
try {
$this->giftMessageOrderItemRepository->save(
$order->getEntityId(),
$orderItem->getItemId(),
$giftMessage
);
} catch (\Exception $e) {
throw new CouldNotSaveException(
__('Could not add gift message to order\'s item: "%1"', $e->getMessage()), $e
);
}
}
}
}
return $order;
}
}
......@@ -39,7 +39,7 @@ class QuoteItem
) {
/** @var $orderItem Item */
$orderItem = $proceed($item, $additional);
$isAvailable = $this->_helper->isMessagesAvailable('item', $item, $item->getStoreId());
$isAvailable = $this->_helper->isMessagesAllowed('item', $item, $item->getStoreId());
$orderItem->setGiftMessageId($item->getGiftMessageId());
$orderItem->setGiftMessageAvailable($isAvailable);
......
......@@ -265,7 +265,7 @@ class Save extends \Magento\Framework\Object
*/
public function isGiftMessagesAvailable($item)
{
return $this->_giftMessageMessage->getIsMessagesAvailable('item', $item, $item->getStore());
return $this->_giftMessageMessage->isMessagesAllowed('item', $item, $item->getStore());
}
/**
......
......@@ -29,7 +29,7 @@ class MessageTest extends \PHPUnit_Framework_TestCase
}
/**
* Make sure that isMessagesAvailable is not called
* Make sure that isMessagesAllowed is not called
*/
public function testGetInlineForCheckout()
{
......
......@@ -163,7 +163,6 @@ class CartRepositoryTest extends \PHPUnit_Framework_TestCase
{
$this->quoteMock->expects($this->once())->method('getItemsCount')->will($this->returnValue(1));
$this->quoteMock->expects($this->once())->method('isVirtual')->will($this->returnValue(true));
$this->cartRepository->save($this->cartId, $this->messageMock);
}
......@@ -173,7 +172,7 @@ class CartRepositoryTest extends \PHPUnit_Framework_TestCase
$this->quoteMock->expects($this->once())->method('getItemsCount')->will($this->returnValue(1));
$this->storeManagerMock->expects($this->once())->method('getStore')->will($this->returnValue($this->storeMock));
$this->helperMock->expects($this->once())
->method('getIsMessagesAvailable')
->method('isMessagesAllowed')
->with('quote', $this->quoteMock, $this->storeMock)
->will($this->returnValue(true));
$this->giftMessageManagerMock->expects($this->once())
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment