Skip to content

Commit 120a105

Browse files
committed
PR 28194 Add ability to update Gift Message for cart item
1 parent b059e51 commit 120a105

File tree

4 files changed

+74
-5
lines changed

4 files changed

+74
-5
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9+
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
10+
<type name="Magento\StoreGraphQl\Model\Resolver\Store\StoreConfigDataProvider">
11+
<arguments>
12+
<argument name="extendedConfigData" xsi:type="array">
13+
<item name="allow_order" xsi:type="string">sales/gift_options/allow_order</item>
14+
<item name="allow_items" xsi:type="string">sales/gift_options/allow_items</item>
15+
</argument>
16+
</arguments>
17+
</type>
18+
</config>

app/code/Magento/GiftMessageGraphQl/etc/schema.graphqls

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
# Copyright © Magento, Inc. All rights reserved.
22
# See COPYING.txt for license details.
33

4+
type StoreConfig {
5+
allow_order : String @doc(description: "Allow Gift Messages on order level")
6+
allow_items : String @doc(description: "Allow Gift Messages for order items")
7+
}
8+
49
type Cart {
510
gift_message: GiftMessage @resolver (class: "\\Magento\\GiftMessageGraphQl\\Model\\Resolver\\Cart\\GiftMessage") @doc(description: "The entered gift message for the cart")
611
}
@@ -10,3 +15,13 @@ type GiftMessage {
1015
from: String! @doc(description: "Sender name")
1116
message: String! @doc(description: "Gift message text")
1217
}
18+
19+
input CartItemUpdateInput {
20+
gift_message: GiftMessageInput @doc(description: "Gift message details for the cart item")
21+
}
22+
23+
input GiftMessageInput {
24+
to: String! @doc(description: "Recepient name")
25+
from: String! @doc(description: "Sender name")
26+
message: String! @doc(description: "Gift message text")
27+
}

app/code/Magento/QuoteGraphQl/Model/Resolver/UpdateCartItems.php

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
1515
use Magento\Framework\GraphQl\Query\ResolverInterface;
1616
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
17+
use Magento\GiftMessage\Api\ItemRepositoryInterface;
1718
use Magento\Quote\Api\CartItemRepositoryInterface;
1819
use Magento\Quote\Api\CartRepositoryInterface;
1920
use Magento\Quote\Model\Quote;
@@ -46,21 +47,29 @@ class UpdateCartItems implements ResolverInterface
4647
private $cartRepository;
4748

4849
/**
49-
* @param GetCartForUser $getCartForUser
50+
* @var ItemRepositoryInterface
51+
*/
52+
private $itemRepository;
53+
54+
/**
55+
* @param GetCartForUser $getCartForUser
5056
* @param CartItemRepositoryInterface $cartItemRepository
51-
* @param UpdateCartItem $updateCartItem
52-
* @param CartRepositoryInterface $cartRepository
57+
* @param UpdateCartItem $updateCartItem
58+
* @param CartRepositoryInterface $cartRepository
59+
* @param ItemRepositoryInterface $itemRepository
5360
*/
5461
public function __construct(
5562
GetCartForUser $getCartForUser,
5663
CartItemRepositoryInterface $cartItemRepository,
5764
UpdateCartItem $updateCartItem,
58-
CartRepositoryInterface $cartRepository
65+
CartRepositoryInterface $cartRepository,
66+
ItemRepositoryInterface $itemRepository
5967
) {
6068
$this->getCartForUser = $getCartForUser;
6169
$this->cartItemRepository = $cartItemRepository;
6270
$this->updateCartItem = $updateCartItem;
6371
$this->cartRepository = $cartRepository;
72+
$this->itemRepository = $itemRepository;
6473
}
6574

6675
/**
@@ -131,6 +140,32 @@ private function processCartItems(Quote $cart, array $items): void
131140
} else {
132141
$this->updateCartItem->execute($cart, $itemId, $quantity, $customizableOptions);
133142
}
143+
144+
if (!empty($item['gift_message'])) {
145+
$this->updateGiftMessageForItem($cart, $item, $itemId);
146+
}
147+
}
148+
}
149+
150+
/**
151+
* Update Gift Message for Quote item
152+
*
153+
* @param Quote $cart
154+
* @param array $item
155+
* @param int $itemId
156+
*
157+
* @throws GraphQlInputException
158+
*/
159+
private function updateGiftMessageForItem(Quote $cart, array $item, int $itemId)
160+
{
161+
try {
162+
$giftItemMessage = $this->itemRepository->get($cart->getEntityId(), $itemId);
163+
$giftItemMessage->setRecipient($item['gift_message']['to']);
164+
$giftItemMessage->setSender($item['gift_message']['from']);
165+
$giftItemMessage->setMessage($item['gift_message']['message']);
166+
$this->itemRepository->save($cart->getEntityId(), $giftItemMessage, $itemId);
167+
} catch (LocalizedException $exception) {
168+
throw new GraphQlInputException(__('Gift Message can not be updated.'));
134169
}
135170
}
136171
}

app/code/Magento/QuoteGraphQl/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"magento/module-customer-graph-ql": "*",
1414
"magento/module-sales": "*",
1515
"magento/module-directory": "*",
16-
"magento/module-graph-ql": "*"
16+
"magento/module-graph-ql": "*",
17+
"magento/module-gift-message": "*"
1718
},
1819
"suggest": {
1920
"magento/module-graph-ql-cache": "*"

0 commit comments

Comments
 (0)