Browse code

Optimize get products by sku console command

Benjamin Roth authored on21/10/2024 12:21:03
Showing2 changed files
... ...
@@ -117,8 +117,10 @@ class Shopware
117 117
         return false;
118 118
     }
119 119
 
120
-    public function getProductsForSku(string $strSku)
120
+    public function getProductsForSku(string $strSku, bool $blnWildcardSearch=false, bool $blnShort=false)
121 121
     {
122
+
123
+
122 124
         $options = [
123 125
             'headers' => [
124 126
                 $this->getAuthentication(),
... ...
@@ -128,14 +130,35 @@ class Shopware
128 130
             'body' => json_encode([
129 131
                 'filter' => [
130 132
                     [
131
-                        'type' => 'equals',
133
+                        'type' => !$blnWildcardSearch ? 'equals' : 'contains',
132 134
                         'field' => 'productNumber',
133 135
                         'value' => $strSku
134 136
                     ]
137
+                ],
138
+                'sort' => [
139
+                    [
140
+                        'field' => 'productNumber',
141
+                        'order' => 'ASC',
142
+                        'naturalSorting' => true
143
+                    ]
135 144
                 ]
136 145
             ])
137 146
         ];
138 147
 
148
+        if ($blnShort)
149
+        {
150
+            $options['body'] = json_encode(
151
+                array_merge(
152
+                    json_decode($options['body'],true),
153
+                    [
154
+                        'fields' => [
155
+                            'name'
156
+                        ]
157
+                    ]
158
+                )
159
+            );
160
+        }
161
+
139 162
         $response = $this->sendRequest('search/product',$options,'POST');
140 163
 
141 164
         if ($response->getStatusCode() == 200)
142 165
new file mode 100644
... ...
@@ -0,0 +1,76 @@
1
+<?php
2
+
3
+declare(strict_types=1);
4
+
5
+/*
6
+ * This file is part of vonRotenberg Shopware API Bundle.
7
+ *
8
+ * (c) vonRotenberg
9
+ *
10
+ * @license proprietary
11
+ */
12
+
13
+namespace vonRotenberg\ShopwareApiBundle\Command;
14
+
15
+use Contao\CoreBundle\Framework\ContaoFramework;
16
+use Contao\System;
17
+use Symfony\Component\Console\Command\Command;
18
+use Symfony\Component\Console\Input\InputArgument;
19
+use Symfony\Component\Console\Input\InputInterface;
20
+use Symfony\Component\Console\Input\InputOption;
21
+use Symfony\Component\Console\Output\OutputInterface;
22
+use Symfony\Component\Console\Style\SymfonyStyle;
23
+use Symfony\Component\VarDumper\Cloner\VarCloner;
24
+use Symfony\Component\VarDumper\Dumper\CliDumper;
25
+use vonRotenberg\ShopwareApiBundle\API\Shopware;
26
+
27
+class ShopwareGetProductsBySku extends Command
28
+{
29
+    protected static $defaultName = 'shopware:get-products-by-sku';
30
+
31
+    protected $framework;
32
+
33
+    public function __construct(ContaoFramework $framework)
34
+    {
35
+        $this->framework = $framework;
36
+
37
+        parent::__construct();
38
+    }
39
+
40
+    protected function configure(): void
41
+    {
42
+        $this
43
+            ->setName(self::$defaultName)
44
+            ->setDescription('Search Shopware products by SKU')
45
+            ->addOption('short','s',InputOption::VALUE_NONE, 'Short result: Only show name and basic product infos')
46
+        ;
47
+    }
48
+    protected function execute(InputInterface $input, OutputInterface $output): int
49
+    {
50
+        $this->framework->initialize();
51
+
52
+        $io = new SymfonyStyle($input, $output);
53
+        $io->title('Shopware products search by SKU');
54
+        $strSku = $io->ask('Enter SKU to search for');
55
+
56
+        /** @var Shopware $Shopware */
57
+        $Shopware = System::getContainer()->get(Shopware::class);
58
+
59
+        if (($Products = $Shopware->getProductsForSku($strSku,true, $input->getOption('short') ? true : false)) !== false)
60
+        {
61
+            $cloner = new VarCloner();
62
+            $dumper = new CliDumper();
63
+
64
+            if ($Products->total)
65
+            {
66
+                $dumper->dump(($cloner->cloneVar($Products->data)));
67
+            }
68
+            $io->success(sprintf('Found %s product(s)',$Products->total));
69
+
70
+            return self::SUCCESS;
71
+        }
72
+
73
+        $io->error('Could not retrieve products by SKU');
74
+        return self::FAILURE;
75
+    }
76
+}