Add Shopify pull-from feature and fix product creation payload
Deploy / deploy (push) Successful in 14s
Deploy / deploy (push) Successful in 14s
- Add "Aus Shopify laden" button on product edit page that pulls description and variant prices/SKUs/GTINs from Shopify back into SMS - Add ShopifyService::getProduct() for fetching a single product - Add 'options' to createProduct() payload so Shopify properly creates variants (without it Shopify ignores variant option values) - Add request/response logging to createProduct() for diagnosing transfer issues - Remove dead code from ProductResource (mutateFormData*, getRecord, getActions) - Clean up imports in ProductResource Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
9ef24fc45f
commit
811f5a015e
3 files changed
+99
-58
No files matched your search
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\Product;
|
||||
use App\Models\ProductSize;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
@@ -76,14 +77,38 @@ class ShopifyService
|
||||
return $variants;
|
||||
}
|
||||
|
||||
public function createProduct(\App\Models\Product $product, \Illuminate\Support\Collection $sizes): array
|
||||
public function getProduct(string $shopifyProductId): array
|
||||
{
|
||||
$response = Http::withHeader('X-Shopify-Access-Token', $this->token)
|
||||
->get("{$this->baseUrl}/products/{$shopifyProductId}.json");
|
||||
|
||||
if ($response->failed()) {
|
||||
throw new \RuntimeException(
|
||||
'Shopify-Fehler ' . $response->status() . ': ' . $response->body()
|
||||
);
|
||||
}
|
||||
|
||||
$p = $response->json('product');
|
||||
|
||||
return [
|
||||
'description' => strip_tags($p['body_html'] ?? ''),
|
||||
'variants' => collect($p['variants'] ?? [])->map(fn ($v) => [
|
||||
'shopify_variant_id' => (string) $v['id'],
|
||||
'price' => (float) ($v['price'] ?? 0),
|
||||
'sku' => $v['sku'] ?? null,
|
||||
'gtin' => $v['barcode'] ?? null,
|
||||
])->all(),
|
||||
];
|
||||
}
|
||||
|
||||
public function createProduct(Product $product, \Illuminate\Support\Collection $sizes): array
|
||||
{
|
||||
$variants = $sizes->map(fn (ProductSize $s) => array_filter([
|
||||
'option1' => $s->packing->name . ' ' . $s->packing->size . 'g',
|
||||
'price' => number_format((float) $s->price, 2, '.', ''),
|
||||
'sku' => $s->sku ?: null,
|
||||
'barcode' => $s->gtin ?: null,
|
||||
]))->values()->toArray();
|
||||
], fn ($v) => $v !== null))->values()->toArray();
|
||||
|
||||
$body = [
|
||||
'product' => [
|
||||
@@ -91,6 +116,7 @@ class ShopifyService
|
||||
'body_html' => $product->description
|
||||
? '<p>' . nl2br(e($product->description)) . '</p>'
|
||||
: '',
|
||||
'options' => [['name' => 'Größe']],
|
||||
],
|
||||
];
|
||||
|
||||
@@ -98,9 +124,20 @@ class ShopifyService
|
||||
$body['product']['variants'] = $variants;
|
||||
}
|
||||
|
||||
Log::info('Shopify createProduct request', [
|
||||
'product_id' => $product->id,
|
||||
'sizes' => $sizes->count(),
|
||||
'body' => $body,
|
||||
]);
|
||||
|
||||
$response = Http::withHeader('X-Shopify-Access-Token', $this->token)
|
||||
->post("{$this->baseUrl}/products.json", $body);
|
||||
|
||||
Log::info('Shopify createProduct response', [
|
||||
'status' => $response->status(),
|
||||
'body' => $response->json(),
|
||||
]);
|
||||
|
||||
if ($response->failed()) {
|
||||
throw new \RuntimeException(
|
||||
'Shopify-Fehler ' . $response->status() . ': ' . $response->body()
|
||||
|
||||
Reference in new issue
Block a user