Skip to content

Commit a8ba229

Browse files
author
Prabhu Ram
committed
MC-22213: Implementation - Merge cart
- Added resolver for merge cart
1 parent 496a855 commit a8ba229

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type Mutation {
1919
setPaymentMethodOnCart(input: SetPaymentMethodOnCartInput): SetPaymentMethodOnCartOutput @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\SetPaymentMethodOnCart")
2020
setGuestEmailOnCart(input: SetGuestEmailOnCartInput): SetGuestEmailOnCartOutput @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\SetGuestEmailOnCart")
2121
setPaymentMethodAndPlaceOrder(input: SetPaymentMethodAndPlaceOrderInput): PlaceOrderOutput @deprecated(reason: "Should use setPaymentMethodOnCart and placeOrder mutations in single request.") @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\SetPaymentAndPlaceOrder")
22+
mergeCarts(source_cart_id: String, destination_cart_id: String): Cart! @doc(description:"Merges source cart into the destination cart") @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\MergeCarts")
2223
placeOrder(input: PlaceOrderInput): PlaceOrderOutput @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\PlaceOrder")
2324
}
2425

0 commit comments

Comments
 (0)