From cfb70a365dc82f9d4c2f72a9ac9fe75f5b44e3fd Mon Sep 17 00:00:00 2001 From: Krzysiej Date: Sun, 1 Feb 2026 22:19:53 +0100 Subject: [PATCH] Display a promotion as a badge next to a product name --- src/Command/ScrapeWebsite.php | 2 +- src/Models/Product.php | 7 +++++++ templates/product.html.twig | 1 + templates/productList.html.twig | 1 + 4 files changed, 10 insertions(+), 1 deletion(-) 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 %}