1313use Magento \Framework \Exception \NoSuchEntityException ;
1414use Magento \Framework \GraphQl \Exception \GraphQlInputException ;
1515use Magento \Framework \GraphQl \Exception \GraphQlNoSuchEntityException ;
16- use Magento \Framework \Stdlib \ArrayManager ;
1716use Magento \Quote \Model \Quote ;
1817
1918/**
2019 * Add simple product to cart
21- *
22- * TODO: should be replaced for different types resolver
2320 */
2421class AddSimpleProductToCart
2522{
26- /**
27- * @var ArrayManager
28- */
29- private $ arrayManager ;
30-
3123 /**
3224 * @var DataObjectFactory
3325 */
@@ -39,16 +31,13 @@ class AddSimpleProductToCart
3931 private $ productRepository ;
4032
4133 /**
42- * @param ArrayManager $arrayManager
4334 * @param DataObjectFactory $dataObjectFactory
4435 * @param ProductRepositoryInterface $productRepository
4536 */
4637 public function __construct (
47- ArrayManager $ arrayManager ,
4838 DataObjectFactory $ dataObjectFactory ,
4939 ProductRepositoryInterface $ productRepository
5040 ) {
51- $ this ->arrayManager = $ arrayManager ;
5241 $ this ->dataObjectFactory = $ dataObjectFactory ;
5342 $ this ->productRepository = $ productRepository ;
5443 }
@@ -67,11 +56,6 @@ public function execute(Quote $cart, array $cartItemData): void
6756 {
6857 $ sku = $ this ->extractSku ($ cartItemData );
6958 $ quantity = $ this ->extractQuantity ($ cartItemData );
70- if ($ quantity <= 0 ) {
71- throw new GraphQlInputException (
72- __ ('Please enter a number greater than 0 in this field. ' )
73- );
74- }
7559 $ customizableOptions = $ this ->extractCustomizableOptions ($ cartItemData );
7660
7761 try {
@@ -105,11 +89,10 @@ public function execute(Quote $cart, array $cartItemData): void
10589 */
10690 private function extractSku (array $ cartItemData ): string
10791 {
108- $ sku = $ this ->arrayManager ->get ('data/sku ' , $ cartItemData );
109- if (!isset ($ sku )) {
110- throw new GraphQlInputException (__ ('Missing key "sku" in cart item data ' ));
92+ if (!isset ($ cartItemData ['data ' ]['sku ' ]) || empty ($ cartItemData ['data ' ]['sku ' ])) {
93+ throw new GraphQlInputException (__ ('Missed "sku" in cart item data ' ));
11194 }
112- return (string )$ sku ;
95+ return (string )$ cartItemData [ ' data ' ][ ' sku ' ] ;
11396 }
11497
11598 /**
@@ -121,11 +104,17 @@ private function extractSku(array $cartItemData): string
121104 */
122105 private function extractQuantity (array $ cartItemData ): float
123106 {
124- $ quantity = $ this ->arrayManager ->get ('data/quantity ' , $ cartItemData );
125- if (!isset ($ quantity )) {
126- throw new GraphQlInputException (__ ('Missing key "quantity" in cart item data ' ));
107+ if (!isset ($ cartItemData ['data ' ]['quantity ' ])) {
108+ throw new GraphQlInputException (__ ('Missed "qty" in cart item data ' ));
109+ }
110+ $ quantity = (float )$ cartItemData ['data ' ]['quantity ' ];
111+
112+ if ($ quantity <= 0 ) {
113+ throw new GraphQlInputException (
114+ __ ('Please enter a number greater than 0 in this field. ' )
115+ );
127116 }
128- return ( float ) $ quantity ;
117+ return $ quantity ;
129118 }
130119
131120 /**
@@ -136,11 +125,17 @@ private function extractQuantity(array $cartItemData): float
136125 */
137126 private function extractCustomizableOptions (array $ cartItemData ): array
138127 {
139- $ customizableOptions = $ this ->arrayManager ->get ('customizable_options ' , $ cartItemData , []);
128+ if (!isset ($ cartItemData ['customizable_options ' ]) || empty ($ cartItemData ['customizable_options ' ])) {
129+ return [];
130+ }
140131
141132 $ customizableOptionsData = [];
142- foreach ($ customizableOptions as $ customizableOption ) {
143- $ customizableOptionsData [$ customizableOption ['id ' ]] = $ customizableOption ['value ' ];
133+ foreach ($ cartItemData ['customizable_options ' ] as $ customizableOption ) {
134+ if (isset ($ customizableOption ['value_string ' ])) {
135+ $ customizableOptionsData [$ customizableOption ['id ' ]] = $ this ->convertCustomOptionValue (
136+ $ customizableOption ['value_string ' ]
137+ );
138+ }
144139 }
145140 return $ customizableOptionsData ;
146141 }
@@ -161,4 +156,20 @@ private function createBuyRequest(float $quantity, array $customOptions): DataOb
161156 ],
162157 ]);
163158 }
159+
160+ /**
161+ * Convert custom options vakue
162+ *
163+ * @param string $value
164+ * @return string|array
165+ */
166+ private function convertCustomOptionValue (string $ value )
167+ {
168+ $ value = trim ($ value );
169+ if (substr ($ value , 0 , 1 ) === "[ " &&
170+ substr ($ value , strlen ($ value ) - 1 , 1 ) === "] " ) {
171+ return explode (', ' , substr ($ value , 1 , -1 ));
172+ }
173+ return $ value ;
174+ }
164175}
0 commit comments