Add Shopify publish/sync per product variant
Deploy / deploy (push) Successful in 14s

Each size row in the product form now shows a Shopify status badge
(green ✓ Shopify / gray Lokal). For saved variants, an action button
appears: 'Zu Shopify' for local-only variants (creates via Shopify
API and stores the returned variant ID), 'Shopify sync' for linked
variants (pushes price/SKU/GTIN to Shopify).

Also fixes:
- gtin missing from ProductSize::$fillable (was silently not saved)
- shopify_product_id added to products table and stored on import
- sizes afterStateHydrated now loads ProductSize directly instead of
  via the belongsToMany pivot (gets product_size_id, shopify_variant_id)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Malek TellissiandClaude Sonnet 4.6 committed 2026-09-12 17:20:26 +02:00
1 parent e187465553
commit 3ebd9845b0
7 files changed
+189 -20

No files matched your search

+48 -3
View File
@@ -2,6 +2,7 @@
namespace App\Services;
use App\Models\ProductSize;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
@@ -51,11 +52,12 @@ class ShopifyService
$imageUrls = collect($product['images'] ?? [])->pluck('src')->all();
foreach ($product['variants'] as $variant) {
$variants[] = [
'id' => (string) $variant['id'],
'sku' => $variant['sku'] ?? '',
'price' => (float) ($variant['price'] ?? 0),
'id' => (string) $variant['id'],
'sku' => $variant['sku'] ?? '',
'price' => (float) ($variant['price'] ?? 0),
'inventory_quantity' => (int) ($variant['inventory_quantity'] ?? 0),
'product_title' => $product['title'],
'shopify_product_id' => (string) $product['id'],
'product_description' => strip_tags($product['body_html'] ?? ''),
'option1' => $variant['option1'] ?? '',
'image_urls' => $imageUrls,
@@ -74,6 +76,49 @@ class ShopifyService
return $variants;
}
public function createVariant(string $shopifyProductId, ProductSize $size): string
{
$packing = $size->packing;
$option1 = $packing->name . ' ' . $packing->size . 'g';
$response = Http::withHeader('X-Shopify-Access-Token', $this->token)
->post("{$this->baseUrl}/products/{$shopifyProductId}/variants.json", [
'variant' => array_filter([
'option1' => $option1,
'price' => number_format((float) $size->price, 2, '.', ''),
'sku' => $size->sku ?: null,
'barcode' => $size->gtin ?: null,
]),
]);
if ($response->failed()) {
throw new \RuntimeException(
'Shopify-Fehler ' . $response->status() . ': ' . $response->body()
);
}
return (string) $response->json('variant.id');
}
public function updateVariant(ProductSize $size): void
{
$response = Http::withHeader('X-Shopify-Access-Token', $this->token)
->put("{$this->baseUrl}/variants/{$size->shopify_variant_id}.json", [
'variant' => array_filter([
'id' => $size->shopify_variant_id,
'price' => number_format((float) $size->price, 2, '.', ''),
'sku' => $size->sku ?: null,
'barcode' => $size->gtin ?: null,
], fn ($v) => $v !== null),
]);
if ($response->failed()) {
throw new \RuntimeException(
'Shopify-Fehler ' . $response->status() . ': ' . $response->body()
);
}
}
private function nextPageUrl(?string $linkHeader): ?string
{
if (!$linkHeader) {