diff --git a/src/Command/ScrapeWebsite.php b/src/Command/ScrapeWebsite.php index 3ee86fe..4701deb 100644 --- a/src/Command/ScrapeWebsite.php +++ b/src/Command/ScrapeWebsite.php @@ -120,7 +120,7 @@ class ScrapeWebsite extends Command $productModel->lastSeen = date("Y-m-d"); $productModel->touch('updated_at'); $productModel->country()->associate($country); - $productModel->promotions = json_encode($product->promotions); + $productModel->promotions = $product->promotions; $productModel->save(); $priceExists = $productModel->price()->whereRaw("strftime('%Y-%m-%d', created_at) = ?", [date('Y-m-d')])->exists(); diff --git a/src/Models/Product.php b/src/Models/Product.php index f285a99..ecaf52c 100644 --- a/src/Models/Product.php +++ b/src/Models/Product.php @@ -88,6 +88,13 @@ class Product extends Model set: fn(array $value) => json_encode($value), ); } + public function promotions(): Attribute + { + return Attribute::make( + get: fn(?string $value) => json_decode($value ?? '{"hasPromotion": false}', 1), + set: fn(array $value) => json_encode($value), + ); + } public function isDiscontinued(): bool { diff --git a/templates/product.html.twig b/templates/product.html.twig index 5f2d52d..0240d95 100644 --- a/templates/product.html.twig +++ b/templates/product.html.twig @@ -11,6 +11,7 @@ {{ product.name }} {{ product.subTitle }} + {% if product.promotions is not null and product.promotions.hasPromotion %}PROMO: {{ product.promotions.tag }}{% endif %}