Browse code

POC update product

Benjamin Roth authored on21/10/2024 23:23:53
Showing1 changed files
... ...
@@ -204,24 +204,36 @@ class Shopware
204 204
 
205 205
     public function addOrUpdateProductBySku(string $strSku, array $arrData)/*: bool*/
206 206
     {
207
+        $blnInsert = true;
207 208
         // Update or insert check
208
-        $blnInsert = !$this->checkBySkuIfExists($strSku);
209
+        if (($searchResponse = $this->getProductsForSku($strSku))->getStatusCode() == 200)
210
+        {
211
+            $Product = json_decode($searchResponse->getContent());
212
+
213
+            if ($Product->total)
214
+            {
215
+                $blnInsert = false;
216
+            }
217
+        }
209 218
 
210 219
         // Prepare product data
211 220
         $arrData = [
212 221
             'taxId' => '018e65c0485071508949c072f8dc18bd',
213
-            'id' => $this->createUuid(),
222
+            'id' => $blnInsert ? $this->createUuid() : $Product->data[0]->id,
214 223
             'price' => [
215 224
                 [
216 225
                     'currencyId' => 'b7d2554b0ce847cd82f3ac9bd1c0dfca',
217 226
                     'gross' => 9.9,
218
-                    'net' => 0,
227
+                    'net' => 9.9/119*100,
219 228
                     'linked' => true
220 229
                 ]
221 230
             ],
222 231
             'productNumber' => $strSku,
223 232
             'stock' => 9999999,
224
-            'name' => 'Superheldenumhang'
233
+            'name' => 'Superheldenumhang',
234
+            'customFields' => [
235
+                'custom_wine_attributes_jahrgang' => "2020"
236
+            ]
225 237
         ];
226 238
 
227 239
         $options = [
... ...
@@ -236,9 +248,11 @@ class Shopware
236 248
         if ($blnInsert)
237 249
         {
238 250
             $response = $this->sendRequest('product',$options,'POST');
251
+        } else {
252
+            $response = $this->sendRequest('product/'.$Product->data[0]->id,$options,'PATCH');
239 253
         }
240 254
 
241
-        return $response->getStatusCode() == 200;
255
+        return in_array($response->getStatusCode(),[200,201,202,203,204]);
242 256
     }
243 257
 
244 258
     protected function createUuid($data = null) {