Download and store Shopify product images locally on import
Deploy / deploy (push) Successful in 13s
Deploy / deploy (push) Successful in 13s
Images are fetched from Shopify CDN, stored in storage/app/public/product-images/ as UUIDs, and saved in the Product.images array — matching Filament's FileUpload format. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
edc4c5a28c
commit
9ee67dfb69
2 files changed
+31
-1
No files matched your search
@@ -9,6 +9,9 @@ use App\Models\ProductSize;
|
||||
use App\Services\ShopifyService;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class ShopifyImport extends Command
|
||||
{
|
||||
@@ -57,6 +60,7 @@ class ShopifyImport extends Command
|
||||
);
|
||||
if ($product->wasRecentlyCreated) {
|
||||
$stats['products']++;
|
||||
$this->downloadImages($product, $variant['image_urls']);
|
||||
}
|
||||
|
||||
$size = ProductSize::firstOrCreate(
|
||||
@@ -102,6 +106,30 @@ class ShopifyImport extends Command
|
||||
return self::SUCCESS;
|
||||
}
|
||||
|
||||
private function downloadImages(Product $product, array $urls): void
|
||||
{
|
||||
if (empty($urls) || ! empty($product->images)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$paths = [];
|
||||
foreach ($urls as $url) {
|
||||
$response = Http::get($url);
|
||||
if ($response->failed()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$ext = pathinfo(parse_url($url, PHP_URL_PATH), PATHINFO_EXTENSION) ?: 'jpg';
|
||||
$filename = 'product-images/' . Str::uuid() . '.' . $ext;
|
||||
Storage::disk('public')->put($filename, $response->body());
|
||||
$paths[] = $filename;
|
||||
}
|
||||
|
||||
if (! empty($paths)) {
|
||||
$product->update(['images' => $paths]);
|
||||
}
|
||||
}
|
||||
|
||||
private function parsePacking(string $option, string $sku): ?array
|
||||
{
|
||||
// "Standbodenbeutel 100 g" / "Metalldose 40 g"
|
||||
|
||||
@@ -25,7 +25,7 @@ class ShopifyService
|
||||
public function getAllVariants(): array
|
||||
{
|
||||
$variants = [];
|
||||
$url = "{$this->baseUrl}/products.json?limit=250&fields=id,title,variants";
|
||||
$url = "{$this->baseUrl}/products.json?limit=250&fields=id,title,variants,images";
|
||||
|
||||
while ($url) {
|
||||
$response = Http::withHeader('X-Shopify-Access-Token', $this->token)->get($url);
|
||||
@@ -48,6 +48,7 @@ class ShopifyService
|
||||
}
|
||||
|
||||
foreach ($response->json('products', []) as $product) {
|
||||
$imageUrls = collect($product['images'] ?? [])->pluck('src')->all();
|
||||
foreach ($product['variants'] as $variant) {
|
||||
$variants[] = [
|
||||
'id' => (string) $variant['id'],
|
||||
@@ -55,6 +56,7 @@ class ShopifyService
|
||||
'inventory_quantity' => (int) ($variant['inventory_quantity'] ?? 0),
|
||||
'product_title' => $product['title'],
|
||||
'option1' => $variant['option1'] ?? '',
|
||||
'image_urls' => $imageUrls,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user