Extend shopify:import to handle Stück, Stangen and Default Title variants
Deploy / deploy (push) Successful in 13s
Deploy / deploy (push) Successful in 13s
Parses additional packaging formats from option1 and falls back to SKU parsing for single-variant products with 'Default Title'. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
94d94effa9
commit
32e196719a
1 file changed
+32
-5
@@ -33,21 +33,22 @@ class ShopifyImport extends Command
|
|||||||
foreach ($variants as $variant) {
|
foreach ($variants as $variant) {
|
||||||
$packingRaw = $variant['option1'];
|
$packingRaw = $variant['option1'];
|
||||||
|
|
||||||
// Format: "Standbodenbeutel 100 g" oder "Metalldose 40 g"
|
$parsed = $this->parsePacking($packingRaw, $variant['sku']);
|
||||||
if (! preg_match('/^(.+?)\s+(\d+(?:[.,]\d+)?)\s*g$/i', $packingRaw, $m)) {
|
|
||||||
|
if ($parsed === null) {
|
||||||
$this->warn(" ⚠ Verpackungsformat nicht erkannt: '{$packingRaw}' ({$variant['sku']}) – übersprungen");
|
$this->warn(" ⚠ Verpackungsformat nicht erkannt: '{$packingRaw}' ({$variant['sku']}) – übersprungen");
|
||||||
$stats['skipped']++;
|
$stats['skipped']++;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($dryRun) {
|
if ($dryRun) {
|
||||||
$this->line(" {$variant['product_title']} | {$packingRaw} | SKU {$variant['sku']} | Bestand {$variant['inventory_quantity']}");
|
$this->line(" {$variant['product_title']} | {$parsed['name']} {$parsed['size']} | SKU {$variant['sku']} | Bestand {$variant['inventory_quantity']}");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
$packing = Packing::firstOrCreate([
|
$packing = Packing::firstOrCreate([
|
||||||
'name' => trim($m[1]),
|
'name' => $parsed['name'],
|
||||||
'size' => (float) str_replace(',', '.', $m[2]),
|
'size' => $parsed['size'],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$product = Product::firstOrCreate(
|
$product = Product::firstOrCreate(
|
||||||
@@ -100,4 +101,30 @@ class ShopifyImport extends Command
|
|||||||
|
|
||||||
return self::SUCCESS;
|
return self::SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function parsePacking(string $option, string $sku): ?array
|
||||||
|
{
|
||||||
|
// "Standbodenbeutel 100 g" / "Metalldose 40 g"
|
||||||
|
if (preg_match('/^(.+?)\s+(\d+(?:[.,]\d+)?)\s*g$/i', $option, $m)) {
|
||||||
|
return ['name' => trim($m[1]), 'size' => (float) str_replace(',', '.', $m[2])];
|
||||||
|
}
|
||||||
|
|
||||||
|
// "Metalldose 5 Stück" / "Standbodenbeutel 3 Stück"
|
||||||
|
if (preg_match('/^(.+?)\s+(\d+)\s*Stück$/i', $option, $m)) {
|
||||||
|
return ['name' => trim($m[1]) . ' (Stück)', 'size' => (float) $m[2]];
|
||||||
|
}
|
||||||
|
|
||||||
|
// "1 Stange" / "3 Stangen" / "5 Stangen"
|
||||||
|
if (preg_match('/^(\d+)\s*Stangen?$/i', $option, $m)) {
|
||||||
|
return ['name' => 'Stangen', 'size' => (float) $m[1]];
|
||||||
|
}
|
||||||
|
|
||||||
|
// "Default Title" — Verpackung aus SKU ableiten: z.B. GWM-AAS-100SB → 100g Standbodenbeutel
|
||||||
|
if ($option === 'Default Title' && preg_match('/(\d+)(SB|MD|GT|BB)$/i', $sku, $m)) {
|
||||||
|
$names = ['SB' => 'Standbodenbeutel', 'MD' => 'Metalldose', 'GT' => 'Glas', 'BB' => 'Beutel'];
|
||||||
|
return ['name' => $names[strtoupper($m[2])], 'size' => (float) $m[1]];
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user