12
12
use Magento \Framework \GraphQl \Exception \GraphQlInputException ;
13
13
use Magento \Framework \GraphQl \Exception \GraphQlNoSuchEntityException ;
14
14
use Magento \Quote \Model \Quote ;
15
+ use Magento \QuoteGraphQl \Model \Cart \BuyRequest \BuyRequestBuilder ;
15
16
16
17
/**
17
18
* Add simple product to cart
18
19
*/
19
20
class AddSimpleProductToCart
20
21
{
21
22
/**
22
- * @var CreateBuyRequest
23
+ * @var ProductRepositoryInterface
23
24
*/
24
- private $ createBuyRequest ;
25
+ private $ productRepository ;
25
26
26
27
/**
27
- * @var ProductRepositoryInterface
28
+ * @var BuyRequestBuilder
28
29
*/
29
- private $ productRepository ;
30
+ private $ buyRequestBuilder ;
30
31
31
32
/**
32
33
* @param ProductRepositoryInterface $productRepository
33
- * @param CreateBuyRequest $createBuyRequest
34
+ * @param BuyRequestBuilder $buyRequestBuilder
34
35
*/
35
36
public function __construct (
36
37
ProductRepositoryInterface $ productRepository ,
37
- CreateBuyRequest $ createBuyRequest
38
+ BuyRequestBuilder $ buyRequestBuilder
38
39
) {
39
40
$ this ->productRepository = $ productRepository ;
40
- $ this ->createBuyRequest = $ createBuyRequest ;
41
+ $ this ->buyRequestBuilder = $ buyRequestBuilder ;
41
42
}
42
43
43
44
/**
@@ -53,8 +54,6 @@ public function __construct(
53
54
public function execute (Quote $ cart , array $ cartItemData ): void
54
55
{
55
56
$ sku = $ this ->extractSku ($ cartItemData );
56
- $ quantity = $ this ->extractQuantity ($ cartItemData );
57
- $ customizableOptions = $ cartItemData ['customizable_options ' ] ?? [];
58
57
59
58
try {
60
59
$ product = $ this ->productRepository ->get ($ sku );
@@ -63,7 +62,7 @@ public function execute(Quote $cart, array $cartItemData): void
63
62
}
64
63
65
64
try {
66
- $ result = $ cart ->addProduct ($ product , $ this ->createBuyRequest -> execute ( $ quantity , $ customizableOptions ));
65
+ $ result = $ cart ->addProduct ($ product , $ this ->buyRequestBuilder -> build ( $ cartItemData ));
67
66
} catch (\Exception $ e ) {
68
67
throw new GraphQlInputException (
69
68
__ (
@@ -92,26 +91,4 @@ private function extractSku(array $cartItemData): string
92
91
}
93
92
return (string )$ cartItemData ['data ' ]['sku ' ];
94
93
}
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
- }
117
94
}
0 commit comments