Files
SpiceManagementSystem/app/Services/ShopifyService.php
T
Malek TellissiandClaude Sonnet 4.6 a7f5c4a83e
Deploy / deploy (push) Successful in 15s
Recipe versioning, inline recipe create, recipe summary, Shopify description
- Add version + product_id to recipes table (migration)
- Recipe select shows version label (e.g. "Kurkuma v2")
- Creating recipe from product page pre-fills product name, auto-increments version
- Recipe ingredient summary shown read-only below the select
- Remove package_content field (redundant with recipe ingredients)
- Pull product description from Shopify body_html on import
- Update existing products with empty description on re-import

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-09-12 05:18:11 +02:00

91 lines
3.1 KiB
PHP

<?php
namespace App\Services;
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'],
'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;
}
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;
}
}