Files
pricebuddy/resources/views/components/product-badges.blade.php
Jeremy 2841ec0c4d fix: no deal verdict until the price has actually moved
A product said "Great time to buy" the moment it was added. With a flat or
single-point history the beat fraction is 0, but the current price is trivially
the lowest ever seen, so isAllTimeLow fired and floored the score at 9.5. The
claim was about a price nothing had been compared to.

DealScoreCalculator now takes hasPriceVariation and beatsOtherListings, and
short-circuits before the percentile and all-time-low logic when the price has
never moved:

- no variation, not the cheapest listing -> new `unknown` verdict, "Not enough
  data yet", score 0
- no variation but cheaper than the product's other URLs -> "Great time to buy",
  score 8. Not history, but real evidence: it is the best place to buy this today
- variation -> unchanged

isAllTimeLow is false in both no-variation cases. A low only means something next
to a high, and the flag also drives the hero's "cheapest it has ever been" line
and the dashboard's ordering.

Side effect worth having: the dashboard's "Buy now" section filters on
dealScore.score >= 6, so every newly added product used to land in it at 9.5.
An `unknown` product scores 0 and stays out.

Recomputed against real data: 3 of 11 products change, all of them two readings
at an identical price on a single URL. The rest are untouched. Existing products
keep their cached verdict until the next price change or
`artisan buddy:regenerate-price-cache`.
2026-08-08 19:11:43 +10:00

76 lines
3.4 KiB
PHP
Executable File

@php
$product = $product ?? $getRecord();
$latestPrice = $product->getPriceCache()->first();
$verdict = data_get($product->insights_cache, 'dealScore.verdict');
$verdictKey = data_get($product->insights_cache, 'dealScore.verdictKey');
$lowConfidence = (bool) data_get($product->insights_cache, 'dealScore.lowConfidence', false);
$verdictColor = match ($verdictKey) {
'great', 'good' => 'success',
'average', 'unknown' => 'gray',
'pricey' => 'warning',
'wait' => 'danger',
default => 'gray',
};
// "Not enough data yet" already says it has no history; the generic low-confidence
// hover would just repeat itself.
$verdictHover = $verdictKey === 'unknown'
? __('The price has not moved yet, so there is nothing to compare it against')
: ($lowConfidence ? __('Not enough price history for a confident verdict') : $verdict);
@endphp
@if (! $product->is_last_scrape_successful || $product->is_notified_price || $latestPrice?->isUnavailable() || $product->paused || $verdict)
<div {{ $attributes->merge(['class' => 'inline-flex gap-2 mt-1 flex-wrap']) }}>
@if ($verdict && ! $product->is_notified_price)
<div class="mt-1 whitespace-nowrap" data-verdict-color="{{ $lowConfidence ? 'gray' : $verdictColor }}">
@include('components.icon-badge', [
'hoverText' => $verdictHover,
'label' => $verdict,
'color' => $lowConfidence ? 'gray' : $verdictColor,
'icon' => $lowConfidence ? 'heroicon-m-question-mark-circle' : 'heroicon-m-sparkles',
])
</div>
@endif
@if ($product->paused)
<div class="mt-1 whitespace-nowrap">
@include('components.icon-badge', [
'hoverText' => __('Price checking is paused for this product'),
'label' => __('Paused'),
'color' => 'gray',
'icon' => 'heroicon-m-pause',
])
</div>
@endif
@if ($latestPrice?->isUnavailable())
<div class="mt-1 whitespace-nowrap">
@include('components.icon-badge', [
'hoverText' => __('This item is currently :status', ['status' => strtolower($latestPrice->getStockStatusLabel())]),
'label' => __($latestPrice->getStockStatusLabel()),
'color' => $latestPrice->getStockStatusColor(),
'icon' => $latestPrice->getStockStatusIcon(),
])
</div>
@endif
@if (! $product->is_last_scrape_successful)
<div class="mt-1 whitespace-nowrap">
@include('components.icon-badge', [
'hoverText' => __('One or more urls failed last scrape'),
'label' => __('Scrape error'),
'color' => 'warning',
])
</div>
@endif
@if ($product->is_notified_price)
<div class="mt-1 whitespace-nowrap">
@include('components.icon-badge', [
'hoverText' => __('Price matches your target'),
'label' => __('Notify match'),
'color' => 'success',
'icon' => 'heroicon-m-shopping-bag'
])
</div>
@endif
</div>
@endif