価格:1,824円
価格:1,824円
割引価格:1,824円
(24%off🔥)
Prime
// 3. クーポンがない場合のセール価格計算
$is_timesale = isset($n->start_time) && \Carbon\Carbon::parse($n->start_time)->isPast() && \Carbon\Carbon::parse($n->end_time)->isFuture();
if($is_timesale && $sortedCoupons->isEmpty() && !$n->prime_discount){
if(isset($n->list_price) && $n->list_price > 0 && $n->list_price > $n->price){
$res = floor(($n->list_price * (100 - $n->percent_off)/100));
}else{
$res = floor(($n->price * (100 - $n->percent_off)/100));
}
$res2 = $n->percent_off.'%off';
$kakaku = 'セール価格';
} else { // 4. クーポンがある場合、またはセールではない場合の割引価格計算
// 基準価格を設定
$price = $n->price;
// プライム割引を適用
if($n->prime_discount){
$price -= $n->prime_discount;
}
// 定期便割引率を取得
$teiki = (isset($n->teiki_type) && $n->teiki_type >= 5) ? $n->teiki_type : 0;
// 5. 全ての有効なクーポンから割引額を合算
$totalPercentDiscount = 0;
$totalFixedDiscount = 0;
foreach ($sortedCoupons as $coupon) {
if (in_array($coupon->coupon_type, [2, 4])) { // % 割引
$totalPercentDiscount += $coupon->coupon_value;
} elseif (in_array($coupon->coupon_type, [1, 3])) { // 固定額割引
$totalFixedDiscount += $coupon->coupon_value;
}
}
// 6. 最終価格を計算 (パーセント割引 → 固定額割引の順)
$finalPrice = $price * (100 - ($totalPercentDiscount + $teiki)) / 100;
$finalPrice -= $totalFixedDiscount;
$res = floor(max(0, $finalPrice)); // 価格がマイナスにならないように制御
// 7. 割引情報の文字列を生成
$res2_parts = [];
$totalDisplayPercent = $totalPercentDiscount + ($n->percent_off ?? 0) + $teiki;
if ($totalDisplayPercent > 0) {
// 小数点以下の不要な0を削除
$res2_parts[] = rtrim(rtrim(number_format($totalDisplayPercent, 2), '0'), '.') . '%off';
}
if ($totalFixedDiscount > 0) {
$res2_parts[] = number_format($totalFixedDiscount) . $currencySymbol . 'off';
}
$res2 = implode('+', $res2_parts);
// 割引文字列が空の場合のフォールバック (定価からの割引率を計算)
if(empty($res2) && $price > 0){
if(isset($n->list_price) && $n->list_price > 0 && $n->list_price > $price){
$discountRate = 100 - floor($price / $n->list_price * 100);
if ($discountRate + $teiki > 0) {
$res2 = ($discountRate + $teiki) . '%off';
}
}
}
$kakaku = '割引価格';
}
@endphp
割引価格:1,824円
(24%off🔥)
Prime