Skip to content

Commit

Permalink
PR 28194 Add ability to update Gift Message for cart item
Browse files Browse the repository at this point in the history
  • Loading branch information
Usik2203 committed May 21, 2020
1 parent b059e51 commit 120a105
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 5 deletions.
18 changes: 18 additions & 0 deletions app/code/Magento/GiftMessageGraphQl/etc/graphql/di.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\StoreGraphQl\Model\Resolver\Store\StoreConfigDataProvider">
<arguments>
<argument name="extendedConfigData" xsi:type="array">
<item name="allow_order" xsi:type="string">sales/gift_options/allow_order</item>
<item name="allow_items" xsi:type="string">sales/gift_options/allow_items</item>
</argument>
</arguments>
</type>
</config>
15 changes: 15 additions & 0 deletions app/code/Magento/GiftMessageGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Copyright © Magento, Inc. All rights reserved.
# See COPYING.txt for license details.

type StoreConfig {
allow_order : String @doc(description: "Allow Gift Messages on order level")
allow_items : String @doc(description: "Allow Gift Messages for order items")
}

type Cart {
gift_message: GiftMessage @resolver (class: "\\Magento\\GiftMessageGraphQl\\Model\\Resolver\\Cart\\GiftMessage") @doc(description: "The entered gift message for the cart")
}
Expand All @@ -10,3 +15,13 @@ type GiftMessage {
from: String! @doc(description: "Sender name")
message: String! @doc(description: "Gift message text")
}

input CartItemUpdateInput {
gift_message: GiftMessageInput @doc(description: "Gift message details for the cart item")
}

input GiftMessageInput {
to: String! @doc(description: "Recepient name")
from: String! @doc(description: "Sender name")
message: String! @doc(description: "Gift message text")
}
43 changes: 39 additions & 4 deletions app/code/Magento/QuoteGraphQl/Model/Resolver/UpdateCartItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\GiftMessage\Api\ItemRepositoryInterface;
use Magento\Quote\Api\CartItemRepositoryInterface;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Model\Quote;
Expand Down Expand Up @@ -46,21 +47,29 @@ class UpdateCartItems implements ResolverInterface
private $cartRepository;

/**
* @param GetCartForUser $getCartForUser
* @var ItemRepositoryInterface
*/
private $itemRepository;

/**
* @param GetCartForUser $getCartForUser
* @param CartItemRepositoryInterface $cartItemRepository
* @param UpdateCartItem $updateCartItem
* @param CartRepositoryInterface $cartRepository
* @param UpdateCartItem $updateCartItem
* @param CartRepositoryInterface $cartRepository
* @param ItemRepositoryInterface $itemRepository
*/
public function __construct(
GetCartForUser $getCartForUser,
CartItemRepositoryInterface $cartItemRepository,
UpdateCartItem $updateCartItem,
CartRepositoryInterface $cartRepository
CartRepositoryInterface $cartRepository,
ItemRepositoryInterface $itemRepository
) {
$this->getCartForUser = $getCartForUser;
$this->cartItemRepository = $cartItemRepository;
$this->updateCartItem = $updateCartItem;
$this->cartRepository = $cartRepository;
$this->itemRepository = $itemRepository;
}

/**
Expand Down Expand Up @@ -131,6 +140,32 @@ private function processCartItems(Quote $cart, array $items): void
} else {
$this->updateCartItem->execute($cart, $itemId, $quantity, $customizableOptions);
}

if (!empty($item['gift_message'])) {
$this->updateGiftMessageForItem($cart, $item, $itemId);
}
}
}

/**
* Update Gift Message for Quote item
*
* @param Quote $cart
* @param array $item
* @param int $itemId
*
* @throws GraphQlInputException
*/
private function updateGiftMessageForItem(Quote $cart, array $item, int $itemId)
{
try {
$giftItemMessage = $this->itemRepository->get($cart->getEntityId(), $itemId);
$giftItemMessage->setRecipient($item['gift_message']['to']);
$giftItemMessage->setSender($item['gift_message']['from']);
$giftItemMessage->setMessage($item['gift_message']['message']);
$this->itemRepository->save($cart->getEntityId(), $giftItemMessage, $itemId);
} catch (LocalizedException $exception) {
throw new GraphQlInputException(__('Gift Message can not be updated.'));
}
}
}
3 changes: 2 additions & 1 deletion app/code/Magento/QuoteGraphQl/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"magento/module-customer-graph-ql": "*",
"magento/module-sales": "*",
"magento/module-directory": "*",
"magento/module-graph-ql": "*"
"magento/module-graph-ql": "*",
"magento/module-gift-message": "*"
},
"suggest": {
"magento/module-graph-ql-cache": "*"
Expand Down

0 comments on commit 120a105

Please sign in to comment.