diff --git a/src/filamentTracker/getFilamentPrices.php b/src/filamentTracker/getFilamentPrices.php index d81ce55..424f236 100644 --- a/src/filamentTracker/getFilamentPrices.php +++ b/src/filamentTracker/getFilamentPrices.php @@ -48,7 +48,10 @@ try { 'color' => $filament['color'], 'amazonUrl' => $filament['amazonUrl'], 'prices' => [], - 'currentDiscount' => $filament['currentDiscount'] ? json_decode($filament['currentDiscount'], true) : null // Decode JSON format + 'currentDiscount' => $filament['currentDiscount'] ? json_decode($filament['currentDiscount'], true) : [ + 'discount' => ['value' => 0, 'type' => 'none'], + 'voucher' => ['value' => 0, 'type' => 'none'] + ] // Decode JSON format and provide fallback ]; } diff --git a/src/filamentTracker/updateFilamentPrices.php b/src/filamentTracker/updateFilamentPrices.php index 438ffaf..d334a49 100644 --- a/src/filamentTracker/updateFilamentPrices.php +++ b/src/filamentTracker/updateFilamentPrices.php @@ -18,7 +18,17 @@ try { $scrapedData = scrapeAmazonData($amazonUrl); if ($scrapedData && $scrapedData['price'] > 0) { - $currentDiscount = json_encode($scrapedData['voucher']); // Encode discount as JSON + // Prepare currentDiscount JSON structure with the correct format + $currentDiscount = json_encode([ + 'discount' => [ + 'value' => $scrapedData['currentDiscount']['discount']['value'] ?? 0, + 'type' => $scrapedData['currentDiscount']['discount']['type'] ?? 'percentage', + ], + 'voucher' => [ + 'value' => $scrapedData['currentDiscount']['voucher']['value'] ?? 0, + 'type' => $scrapedData['currentDiscount']['voucher']['type'] ?? null, + ], + ]); $stmt = $pdo->prepare(" INSERT INTO filamentPriceHistory (filamentId, price, currentDiscount) @@ -30,7 +40,7 @@ try { ':currentDiscount' => $currentDiscount ]); - echo "Updated price for {$filament['filamentName']}: £{$scrapedData['price']}, Discount: " . json_encode($scrapedData['voucher']) . "\n"; + echo "Updated price for {$filament['filamentName']}: £{$scrapedData['price']}, Current Discount: {$currentDiscount}\n"; } else { echo "Failed to update {$filament['filamentName']} (no price found or £0).\n"; } @@ -41,4 +51,3 @@ try { } catch (PDOException $e) { echo "Database error: " . $e->getMessage(); } -?>