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>
136 lines
4.9 KiB
PHP
136 lines
4.9 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\ProductSize;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class ShopifyService
|
|
{
|
|
private string $baseUrl;
|
|
private string $token;
|
|
|
|
public function __construct()
|
|
{
|
|
$domain = config('services.shopify.domain');
|
|
$version = config('services.shopify.api_version');
|
|
$this->baseUrl = "https://{$domain}/admin/api/{$version}";
|
|
$this->token = config('services.shopify.access_token');
|
|
}
|
|
|
|
/**
|
|
* Returns all product variants from Shopify.
|
|
* Each entry: ['id', 'sku', 'inventory_quantity', 'product_title']
|
|
*/
|
|
public function getAllVariants(): array
|
|
{
|
|
$variants = [];
|
|
$url = "{$this->baseUrl}/products.json?limit=250&fields=id,title,body_html,variants,images";
|
|
|
|
while ($url) {
|
|
$response = Http::withHeader('X-Shopify-Access-Token', $this->token)->get($url);
|
|
|
|
if ($response->failed()) {
|
|
$status = $response->status();
|
|
$hint = match (true) {
|
|
$status === 401 => 'Access Token ungültig oder fehlt',
|
|
$status === 403 => 'IP-Whitelist: VPS-IP ist in Shopify nicht freigeschalten',
|
|
$status === 429 => 'Rate Limit überschritten',
|
|
$status >= 500 => 'Shopify-seitiger Fehler',
|
|
default => 'Unbekannter Fehler',
|
|
};
|
|
Log::error("Shopify Sync fehlgeschlagen: {$hint}", [
|
|
'status' => $status,
|
|
'url' => $url,
|
|
'body' => $response->body(),
|
|
]);
|
|
break;
|
|
}
|
|
|
|
foreach ($response->json('products', []) as $product) {
|
|
$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),
|
|
'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,
|
|
];
|
|
}
|
|
}
|
|
|
|
// Follow Shopify's Link header pagination
|
|
$url = $this->nextPageUrl($response->header('Link'));
|
|
|
|
if ($url) {
|
|
usleep(500_000); // 0.5s — stay well within 2 req/s limit
|
|
}
|
|
}
|
|
|
|
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) {
|
|
return null;
|
|
}
|
|
|
|
// Link: <https://...?page_info=abc>; rel="next"
|
|
if (preg_match('/<([^>]+)>;\s*rel="next"/', $linkHeader, $m)) {
|
|
return $m[1];
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|