Skip to content

Commit

Permalink
Merge pull request #583 from magento-folks/bugs
Browse files Browse the repository at this point in the history
[Folks]Bugfix
  • Loading branch information
Korshenko, Oleksii(okorshenko) committed May 4, 2016
2 parents 981d1f4 + 7edc294 commit 870bdfb
Show file tree
Hide file tree
Showing 8 changed files with 496 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,12 @@ private function prepareShippingAssignment(CartInterface $quote, AddressInterfac
if ($cartExtension === null) {
$cartExtension = $this->getCartExtensionFactory()->create();
}
$shippingAssignment = $cartExtension->getShippingAssignments()[0];
if ($cartExtension->getShippingAssignments() === null) {

$shippingAssignments = $cartExtension->getShippingAssignments();
if (empty($shippingAssignments)) {
$shippingAssignment = $this->getShippingAssignmentFactory()->create();
} else {
$shippingAssignment = $shippingAssignments[0];
}

$shipping = $shippingAssignment->getShipping();
Expand Down
28 changes: 14 additions & 14 deletions app/code/Magento/GiftMessage/Model/Plugin/OrderGet.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,14 @@ public function __construct(
* Get gift message
*
* @param \Magento\Sales\Api\OrderRepositoryInterface $subject
* @param callable $proceed
* @param int $orderId
* @param \Magento\Sales\Api\Data\OrderInterface $resultOrder
* @return \Magento\Sales\Api\Data\OrderInterface
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function aroundGet(
public function afterGet(
\Magento\Sales\Api\OrderRepositoryInterface $subject,
\Closure $proceed,
$orderId
\Magento\Sales\Api\Data\OrderInterface $resultOrder
) {
/** @var \Magento\Sales\Api\Data\OrderInterface $resultOrder */
$resultOrder = $proceed($orderId);

$resultOrder = $this->getOrderGiftMessage($resultOrder);
$resultOrder = $this->getOrderItemGiftMessage($resultOrder);

Expand All @@ -74,7 +69,8 @@ public function aroundGet(
*/
protected function getOrderGiftMessage(\Magento\Sales\Api\Data\OrderInterface $order)
{
if ($order->getExtensionAttributes() && $order->getExtensionAttributes()->getGiftMessage()) {
$extensionAttributes = $order->getExtensionAttributes();
if ($extensionAttributes && $extensionAttributes->getGiftMessage()) {
return $order;
}

Expand All @@ -86,7 +82,7 @@ protected function getOrderGiftMessage(\Magento\Sales\Api\Data\OrderInterface $o
}

/** @var \Magento\Sales\Api\Data\OrderExtension $orderExtension */
$orderExtension = $this->orderExtensionFactory->create();
$orderExtension = $extensionAttributes ? $extensionAttributes : $this->orderExtensionFactory->create();
$orderExtension->setGiftMessage($giftMessage);
$order->setExtensionAttributes($orderExtension);

Expand All @@ -101,10 +97,12 @@ protected function getOrderGiftMessage(\Magento\Sales\Api\Data\OrderInterface $o
*/
protected function getOrderItemGiftMessage(\Magento\Sales\Api\Data\OrderInterface $order)
{
if (null !== $order->getItems()) {
$orderItems = $order->getItems();
if (null !== $orderItems) {
/** @var \Magento\Sales\Api\Data\OrderItemInterface $orderItem */
foreach ($order->getItems() as $orderItem) {
if ($orderItem->getExtensionAttributes() && $orderItem->getExtensionAttributes()->getGiftMessage()) {
foreach ($orderItems as $orderItem) {
$extensionAttributes = $orderItem->getExtensionAttributes();
if ($extensionAttributes && $extensionAttributes->getGiftMessage()) {
continue;
}

Expand All @@ -119,7 +117,9 @@ protected function getOrderItemGiftMessage(\Magento\Sales\Api\Data\OrderInterfac
}

/** @var \Magento\Sales\Api\Data\OrderItemExtension $orderItemExtension */
$orderItemExtension = $this->orderItemExtensionFactory->create();
$orderItemExtension = $extensionAttributes
? $extensionAttributes
: $this->orderItemExtensionFactory->create();
$orderItemExtension->setGiftMessage($giftMessage);
$orderItem->setExtensionAttributes($orderItemExtension);
}
Expand Down
47 changes: 24 additions & 23 deletions app/code/Magento/GiftMessage/Model/Plugin/OrderSave.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,16 @@ public function __construct(
* Save gift message
*
* @param \Magento\Sales\Api\OrderRepositoryInterface $subject
* @param callable $proceed
* @param \Magento\Sales\Api\Data\OrderInterface $order
* @param \Magento\Sales\Api\Data\OrderInterface $resultOrder
* @return \Magento\Sales\Api\Data\OrderInterface
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
* @throws CouldNotSaveException
*/
public function aroundSave(
public function afterSave(
\Magento\Sales\Api\OrderRepositoryInterface $subject,
\Closure $proceed,
\Magento\Sales\Api\Data\OrderInterface $order
\Magento\Sales\Api\Data\OrderInterface $resultOrder
) {
/** @var \Magento\Sales\Api\Data\OrderInterface $resultOrder */
$resultOrder = $proceed($order);
$resultOrder = $this->saveOrderGiftMessage($resultOrder);
$resultOrder = $this->saveOrderItemGiftMessage($resultOrder);

Expand All @@ -58,24 +56,24 @@ public function aroundSave(
*
* @param \Magento\Sales\Api\Data\OrderInterface $order
* @return \Magento\Sales\Api\Data\OrderInterface
* @throws CouldNotSaveException
*/
protected function saveOrderGiftMessage(\Magento\Sales\Api\Data\OrderInterface $order)
{
$extensionAttributes = $order->getExtensionAttributes();
if (
null !== $order->getExtensionAttributes() &&
null !== $order->getExtensionAttributes()->getGiftMessage()
null !== $extensionAttributes &&
null !== $extensionAttributes->getGiftMessage()
) {
/* @var \Magento\GiftMessage\Api\Data\MessageInterface $giftMessage */
$giftMessage = $order->getExtensionAttributes()->getGiftMessage();
if (null !== $giftMessage) {
try {
$this->giftMessageOrderRepository->save($order->getEntityId(), $giftMessage);
} catch (\Exception $e) {
throw new CouldNotSaveException(
__('Could not add gift message to order: "%1"', $e->getMessage()),
$e
);
}
$giftMessage = $extensionAttributes->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;
Expand All @@ -86,18 +84,21 @@ protected function saveOrderGiftMessage(\Magento\Sales\Api\Data\OrderInterface $
*
* @param \Magento\Sales\Api\Data\OrderInterface $order
* @return \Magento\Sales\Api\Data\OrderInterface
* @throws CouldNotSaveException
*/
protected function saveOrderItemGiftMessage(\Magento\Sales\Api\Data\OrderInterface $order)
{
if (null !== $order->getItems()) {
$items = $order->getItems();
if (null !== $items) {
/** @var \Magento\Sales\Api\Data\OrderItemInterface $orderItem */
foreach ($order->getItems() as $orderItem) {
foreach ($items as $orderItem) {
$extensionAttribute = $orderItem->getExtensionAttributes();
if (
null !== $orderItem->getExtensionAttributes() &&
null !== $orderItem->getExtensionAttributes()->getGiftMessage()
null !== $extensionAttribute &&
null !== $extensionAttribute->getGiftMessage()
) {
/* @var \Magento\GiftMessage\Api\Data\MessageInterface $giftMessage */
$giftMessage = $orderItem->getExtensionAttributes()->getGiftMessage();
$giftMessage = $extensionAttribute->getGiftMessage();
try {
$this->giftMessageOrderItemRepository->save(
$order->getEntityId(),
Expand Down
Loading

0 comments on commit 870bdfb

Please sign in to comment.