Browse code

Add truncate properties for product method

Benjamin Roth authored on07/11/2024 19:16:50
Showing1 changed files
... ...
@@ -240,6 +240,69 @@ class Shopware
240 240
         return in_array($response->getStatusCode(),[200,201,202,203,204]);
241 241
     }
242 242
 
243
+    public function truncatePropertiesForProductBySku(string $strSku, string|array|null $groupIds = null): bool
244
+    {
245
+        if ($groupIds !== null && !is_array($groupIds))
246
+        {
247
+            $groupIds = (array) $groupIds;
248
+        }
249
+
250
+        // Look for product
251
+        if (($searchResponse = $this->getProductsForSku($strSku))->getStatusCode() == 200)
252
+        {
253
+            $Product = json_decode($searchResponse->getContent());
254
+
255
+            if (!$Product->total)
256
+            {
257
+                return false;
258
+            }
259
+
260
+            // Get property ids
261
+            $arrPropertiesPayload = [];
262
+            foreach (System::getContainer()->getParameter('vonrotenberg_shopware_api.mappings.properties') as $groupId => $properties)
263
+            {
264
+                if ($groupIds !== null && !in_array($groupId,$groupIds))
265
+                {
266
+                    continue;
267
+                }
268
+
269
+                foreach (array_keys($properties) as $propertyId)
270
+                {
271
+                    $arrPropertiesPayload[] = [
272
+                        'productId' => $Product->data[0]->id,
273
+                        'optionId' => $propertyId
274
+                    ];
275
+                }
276
+            }
277
+
278
+            // Build request data
279
+            $arrData = [
280
+                [
281
+                    'entity' => 'product_property',
282
+                    'action' => 'delete',
283
+                    'payload' => $arrPropertiesPayload
284
+                ]
285
+            ];
286
+
287
+            $options = [
288
+                'headers' => [
289
+                    $this->getAuthentication(),
290
+                    'Content-Type: application/json',
291
+                    'Accept: application/json',
292
+                    'fail-on-error: false'
293
+                ],
294
+                'body' => json_encode($arrData)
295
+            ];
296
+
297
+            // Send request
298
+            $response = $this->sendRequest('_action/sync',$options,'POST');
299
+
300
+            return in_array($response->getStatusCode(),[200]);
301
+        }
302
+
303
+        return false;
304
+    }
305
+
243 306
     protected function createUuid($data = null) {
244 307
         // Generate 16 bytes (128 bits) of random data or use the data passed into the function.
245 308
         $data = $data ?? random_bytes(16);