|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\QuoteGraphQl\Model\Resolver; |
| 9 | + |
| 10 | +use Magento\Framework\GraphQl\Config\Element\Field; |
| 11 | +use Magento\Framework\GraphQl\Exception\GraphQlInputException; |
| 12 | +use Magento\Framework\GraphQl\Query\ResolverInterface; |
| 13 | +use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; |
| 14 | +use Magento\QuoteGraphQl\Model\Cart\GetCartForUser; |
| 15 | + |
| 16 | +/** |
| 17 | + * Merge Carts Resolver |
| 18 | + */ |
| 19 | +class MergeCarts implements ResolverInterface |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @var GetCartForUser |
| 23 | + */ |
| 24 | + private $getCartForUser; |
| 25 | + |
| 26 | + /** |
| 27 | + * @param GetCartForUser $getCartForUser |
| 28 | + */ |
| 29 | + public function __construct( |
| 30 | + GetCartForUser $getCartForUser |
| 31 | + ) { |
| 32 | + $this->getCartForUser = $getCartForUser; |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * @inheritdoc |
| 37 | + */ |
| 38 | + public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) |
| 39 | + { |
| 40 | + if (empty($args['source_cart_id'])) { |
| 41 | + throw new GraphQlInputException(__('Required parameter "cart_id" is missing')); |
| 42 | + } |
| 43 | + |
| 44 | + if (empty($args['destination_cart_id'])) { |
| 45 | + throw new GraphQlInputException(__('Required parameter "destination_cart_id" is missing')); |
| 46 | + } |
| 47 | + |
| 48 | + $guestMaskedCartId = $args['source_cart_id']; |
| 49 | + $customerMaskedCartId = $args['destination_cart_id']; |
| 50 | + |
| 51 | + $currentUserId = $context->getUserId(); |
| 52 | + $storeId = $storeId = (int)$context->getExtensionAttributes()->getStore()->getId(); |
| 53 | + // passing customerId as null enforces source cart should always be a guestcart |
| 54 | + $guestCart = $this->getCartForUser->execute($guestMaskedCartId, null, $storeId); |
| 55 | + $customerCart = $this->getCartForUser->execute($customerMaskedCartId, $currentUserId, $storeId); |
| 56 | + $customerCart->merge($guestCart); |
| 57 | + |
| 58 | + return [ |
| 59 | + 'model' => $customerCart, |
| 60 | + ]; |
| 61 | + } |
| 62 | +} |
0 commit comments