|
14 | 14 | use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
|
15 | 15 | use Magento\Framework\GraphQl\Query\ResolverInterface;
|
16 | 16 | use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
|
| 17 | +use Magento\GiftMessage\Api\ItemRepositoryInterface; |
17 | 18 | use Magento\Quote\Api\CartItemRepositoryInterface;
|
18 | 19 | use Magento\Quote\Api\CartRepositoryInterface;
|
19 | 20 | use Magento\Quote\Model\Quote;
|
@@ -46,21 +47,29 @@ class UpdateCartItems implements ResolverInterface
|
46 | 47 | private $cartRepository;
|
47 | 48 |
|
48 | 49 | /**
|
49 |
| - * @param GetCartForUser $getCartForUser |
| 50 | + * @var ItemRepositoryInterface |
| 51 | + */ |
| 52 | + private $itemRepository; |
| 53 | + |
| 54 | + /** |
| 55 | + * @param GetCartForUser $getCartForUser |
50 | 56 | * @param CartItemRepositoryInterface $cartItemRepository
|
51 |
| - * @param UpdateCartItem $updateCartItem |
52 |
| - * @param CartRepositoryInterface $cartRepository |
| 57 | + * @param UpdateCartItem $updateCartItem |
| 58 | + * @param CartRepositoryInterface $cartRepository |
| 59 | + * @param ItemRepositoryInterface $itemRepository |
53 | 60 | */
|
54 | 61 | public function __construct(
|
55 | 62 | GetCartForUser $getCartForUser,
|
56 | 63 | CartItemRepositoryInterface $cartItemRepository,
|
57 | 64 | UpdateCartItem $updateCartItem,
|
58 |
| - CartRepositoryInterface $cartRepository |
| 65 | + CartRepositoryInterface $cartRepository, |
| 66 | + ItemRepositoryInterface $itemRepository |
59 | 67 | ) {
|
60 | 68 | $this->getCartForUser = $getCartForUser;
|
61 | 69 | $this->cartItemRepository = $cartItemRepository;
|
62 | 70 | $this->updateCartItem = $updateCartItem;
|
63 | 71 | $this->cartRepository = $cartRepository;
|
| 72 | + $this->itemRepository = $itemRepository; |
64 | 73 | }
|
65 | 74 |
|
66 | 75 | /**
|
@@ -131,6 +140,32 @@ private function processCartItems(Quote $cart, array $items): void
|
131 | 140 | } else {
|
132 | 141 | $this->updateCartItem->execute($cart, $itemId, $quantity, $customizableOptions);
|
133 | 142 | }
|
| 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.')); |
134 | 169 | }
|
135 | 170 | }
|
136 | 171 | }
|
0 commit comments