Skip to content

Commit 4c325de

Browse files
author
Anna Bukatar
committed
ACP2E-1612: Gift Cart product update in a Cart by using GraphQL
1 parent c9d1397 commit 4c325de

File tree

5 files changed

+126
-12
lines changed

5 files changed

+126
-12
lines changed

app/code/Magento/QuoteGraphQl/Model/Cart/CreateBuyRequest.php

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,21 @@ class CreateBuyRequest
2020
*/
2121
private $dataObjectFactory;
2222

23+
/**
24+
* @var CreateBuyRequestDataProviderInterface[]
25+
*/
26+
private $providers;
27+
2328
/**
2429
* @param DataObjectFactory $dataObjectFactory
30+
* @param array $providers
2531
*/
2632
public function __construct(
27-
DataObjectFactory $dataObjectFactory
33+
DataObjectFactory $dataObjectFactory,
34+
array $providers = []
2835
) {
2936
$this->dataObjectFactory = $dataObjectFactory;
37+
$this->providers = $providers;
3038
}
3139

3240
/**
@@ -39,21 +47,30 @@ public function __construct(
3947
public function execute(float $qty, array $customizableOptionsData): DataObject
4048
{
4149
$customizableOptions = [];
50+
$enteredOptions = [];
4251
foreach ($customizableOptionsData as $customizableOption) {
4352
if (isset($customizableOption['value_string'])) {
44-
$customizableOptions[$customizableOption['id']] = $this->convertCustomOptionValue(
45-
$customizableOption['value_string']
46-
);
53+
if (!is_numeric($customizableOption['id'])) {
54+
$enteredOptions[$customizableOption['id']] = $customizableOption['value_string'];
55+
} else {
56+
$customizableOptions[$customizableOption['id']] = $this->convertCustomOptionValue(
57+
$customizableOption['value_string']
58+
);
59+
}
4760
}
4861
}
4962

50-
$dataArray = [
51-
'data' => [
63+
$requestData = [
64+
[
5265
'qty' => $qty,
53-
'options' => $customizableOptions,
54-
],
66+
'options' => $customizableOptions
67+
]
5568
];
56-
return $this->dataObjectFactory->create($dataArray);
69+
foreach ($this->providers as $provider) {
70+
$requestData[] = $provider->execute($enteredOptions);
71+
}
72+
73+
return $this->dataObjectFactory->create(['data' => array_merge([], ...$requestData)]);
5774
}
5875

5976
/**
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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\Cart;
9+
10+
interface CreateBuyRequestDataProviderInterface
11+
{
12+
/**
13+
* Create buy request data that can be used for working with cart items
14+
*
15+
* @param array $cartItemData
16+
* @return array
17+
*/
18+
public function execute(array $cartItemData): array;
19+
}

app/code/Magento/QuoteGraphQl/Model/CartItem/CartItemsUidArgsProcessor.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1111
use Magento\Framework\GraphQl\Query\Resolver\ArgumentsProcessorInterface;
1212
use Magento\Framework\GraphQl\Query\Uid;
13+
use Magento\Framework\App\ObjectManager;
1314

1415
/**
1516
* Category UID processor class for category uid and category id arguments
@@ -23,18 +24,26 @@ class CartItemsUidArgsProcessor implements ArgumentsProcessorInterface
2324
/** @var Uid */
2425
private $uidEncoder;
2526

27+
/**
28+
* @var CustomizableOptionUidArgsProcessor
29+
*/
30+
private $optionUidArgsProcessor;
31+
2632
/**
2733
* @param Uid $uidEncoder
34+
* @param CustomizableOptionUidArgsProcessor|null $optionUidArgsProcessor
2835
*/
29-
public function __construct(Uid $uidEncoder)
36+
public function __construct(Uid $uidEncoder, ?CustomizableOptionUidArgsProcessor $optionUidArgsProcessor = null)
3037
{
3138
$this->uidEncoder = $uidEncoder;
39+
$this->optionUidArgsProcessor =
40+
$optionUidArgsProcessor ?: ObjectManager::getInstance()->get(CustomizableOptionUidArgsProcessor::class);
3241
}
3342

3443
/**
3544
* Process the updateCartItems arguments for cart uids
3645
*
37-
* @param string $fieldName,
46+
* @param string $fieldName
3847
* @param array $args
3948
* @return array
4049
* @throws GraphQlInputException
@@ -58,6 +67,10 @@ public function process(
5867
$args[$filterKey]['cart_items'][$key][self::ID] = $this->uidEncoder->decode((string)$uidFilter);
5968
unset($args[$filterKey]['cart_items'][$key][self::UID]);
6069
}
70+
if (!empty($cartItem['customizable_options'])) {
71+
$args[$filterKey]['cart_items'][$key]['customizable_options'] =
72+
$this->optionUidArgsProcessor->process($fieldName, $cartItem['customizable_options']);
73+
}
6174
}
6275
}
6376
return $args;
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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\CartItem;
9+
10+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
11+
use Magento\Framework\GraphQl\Query\Resolver\ArgumentsProcessorInterface;
12+
use Magento\Framework\GraphQl\Query\Uid;
13+
14+
/**
15+
* Category UID processor class for category uid and category id arguments
16+
*/
17+
class CustomizableOptionUidArgsProcessor implements ArgumentsProcessorInterface
18+
{
19+
private const ID = 'id';
20+
21+
private const UID = 'uid';
22+
23+
/** @var Uid */
24+
private $uidEncoder;
25+
26+
/**
27+
* @param Uid $uidEncoder
28+
*/
29+
public function __construct(Uid $uidEncoder)
30+
{
31+
$this->uidEncoder = $uidEncoder;
32+
}
33+
34+
/**
35+
* Process the customizable options for updateCartItems arguments for uids
36+
*
37+
* @param string $fieldName
38+
* @param array $customizableOptions
39+
* @return array
40+
* @throws GraphQlInputException
41+
*/
42+
public function process(string $fieldName, array $customizableOptions): array
43+
{
44+
foreach ($customizableOptions as $key => $option) {
45+
$idFilter = $option[self::ID] ?? [];
46+
$uidFilter = $option[self::UID] ?? [];
47+
48+
if (!empty($idFilter)
49+
&& !empty($uidFilter)
50+
&& $fieldName === 'updateCartItems') {
51+
throw new GraphQlInputException(
52+
__(
53+
'`%1` and `%2` can\'t be used for CustomizableOptionInput object at the same time.',
54+
[self::ID, self::UID]
55+
)
56+
);
57+
} elseif (!empty($uidFilter)) {
58+
$customizableOptions[$key][self::ID] = $this->uidEncoder->decode((string)$uidFilter);
59+
unset($customizableOptions[$key][self::UID]);
60+
}
61+
}
62+
return $customizableOptions;
63+
}
64+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ input CartItemInput @doc(description: "Defines an item to be added to the cart."
6262
}
6363

6464
input CustomizableOptionInput @doc(description: "Defines a customizable option.") {
65-
id: Int @doc(description: "The customizable option ID of the product.")
65+
uid: ID @doc(description: "The unique ID for a `CartItemInterface` object.")
66+
id: Int @deprecated(reason: "Use `uid` instead.") @doc(description: "The customizable option ID of the product.")
6667
value_string: String! @doc(description: "The string value of the option.")
6768
}
6869

0 commit comments

Comments
 (0)