Skip to content

Commit 226cbf7

Browse files
committed
Update add to cart with extendible buy request builder
1 parent 989038c commit 226cbf7

File tree

3 files changed

+35
-38
lines changed

3 files changed

+35
-38
lines changed

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

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,33 @@
1212
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1313
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
1414
use Magento\Quote\Model\Quote;
15+
use Magento\QuoteGraphQl\Model\Cart\BuyRequest\BuyRequestBuilder;
1516

1617
/**
1718
* Add simple product to cart
1819
*/
1920
class AddSimpleProductToCart
2021
{
2122
/**
22-
* @var CreateBuyRequest
23+
* @var ProductRepositoryInterface
2324
*/
24-
private $createBuyRequest;
25+
private $productRepository;
2526

2627
/**
27-
* @var ProductRepositoryInterface
28+
* @var BuyRequestBuilder
2829
*/
29-
private $productRepository;
30+
private $buyRequestBuilder;
3031

3132
/**
3233
* @param ProductRepositoryInterface $productRepository
33-
* @param CreateBuyRequest $createBuyRequest
34+
* @param BuyRequestBuilder $buyRequestBuilder
3435
*/
3536
public function __construct(
3637
ProductRepositoryInterface $productRepository,
37-
CreateBuyRequest $createBuyRequest
38+
BuyRequestBuilder $buyRequestBuilder
3839
) {
3940
$this->productRepository = $productRepository;
40-
$this->createBuyRequest = $createBuyRequest;
41+
$this->buyRequestBuilder = $buyRequestBuilder;
4142
}
4243

4344
/**
@@ -53,8 +54,6 @@ public function __construct(
5354
public function execute(Quote $cart, array $cartItemData): void
5455
{
5556
$sku = $this->extractSku($cartItemData);
56-
$quantity = $this->extractQuantity($cartItemData);
57-
$customizableOptions = $cartItemData['customizable_options'] ?? [];
5857

5958
try {
6059
$product = $this->productRepository->get($sku);
@@ -63,7 +62,7 @@ public function execute(Quote $cart, array $cartItemData): void
6362
}
6463

6564
try {
66-
$result = $cart->addProduct($product, $this->createBuyRequest->execute($quantity, $customizableOptions));
65+
$result = $cart->addProduct($product, $this->buyRequestBuilder->build($cartItemData));
6766
} catch (\Exception $e) {
6867
throw new GraphQlInputException(
6968
__(
@@ -92,26 +91,4 @@ private function extractSku(array $cartItemData): string
9291
}
9392
return (string)$cartItemData['data']['sku'];
9493
}
95-
96-
/**
97-
* Extract quantity from cart item data
98-
*
99-
* @param array $cartItemData
100-
* @return float
101-
* @throws GraphQlInputException
102-
*/
103-
private function extractQuantity(array $cartItemData): float
104-
{
105-
if (!isset($cartItemData['data']['quantity'])) {
106-
throw new GraphQlInputException(__('Missed "qty" in cart item data'));
107-
}
108-
$quantity = (float)$cartItemData['data']['quantity'];
109-
110-
if ($quantity <= 0) {
111-
throw new GraphQlInputException(
112-
__('Please enter a number greater than 0 in this field.')
113-
);
114-
}
115-
return $quantity;
116-
}
11794
}

app/code/Magento/QuoteGraphQl/Model/Cart/BuyRequest/CustomizableOptionsDataProvider.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,33 @@ public function __construct(
3333
*/
3434
public function execute(array $cartItemData): array
3535
{
36-
$customizableOptions = $this->arrayManager->get('customizable_options', $cartItemData, []);
36+
$customizableOptionsData = $this->arrayManager->get('customizable_options', $cartItemData, []);
3737

38-
$customizableOptionsData = [];
39-
foreach ($customizableOptions as $customizableOption) {
40-
$customizableOptionsData[$customizableOption['id']] = $customizableOption['value'];
38+
$customizableOptions = [];
39+
foreach ($customizableOptionsData as $customizableOption) {
40+
if (isset($customizableOption['value_string'])) {
41+
$customizableOptions[$customizableOption['id']] = $this->convertCustomOptionValue(
42+
$customizableOption['value_string']
43+
);
44+
}
4145
}
4246

4347
return ['options' => $customizableOptionsData];
4448
}
49+
50+
/**
51+
* Convert custom options value
52+
*
53+
* @param string $value
54+
* @return string|array
55+
*/
56+
private function convertCustomOptionValue(string $value)
57+
{
58+
$value = trim($value);
59+
if (substr($value, 0, 1) === "[" &&
60+
substr($value, strlen($value) - 1, 1) === "]") {
61+
return explode(',', substr($value, 1, -1));
62+
}
63+
return $value;
64+
}
4565
}

app/code/Magento/QuoteGraphQl/Model/Cart/BuyRequest/QuantityDataProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ public function __construct(
3434
*/
3535
public function execute(array $cartItemData): array
3636
{
37-
$qty = $this->arrayManager->get('data/qty', $cartItemData);
37+
$qty = $this->arrayManager->get('data/quantity', $cartItemData);
3838
if (!isset($qty)) {
39-
throw new GraphQlInputException(__('Missing key "qty" in cart item data'));
39+
throw new GraphQlInputException(__('Missing key "quantity" in cart item data'));
4040
}
4141

4242
return ['qty' => (float)$qty];

0 commit comments

Comments
 (0)