<?php
function coupon_find($code)
{
return db_one('SELECT * FROM coupons WHERE code = ? AND status = 1', [strtoupper(trim($code))]);
}
// Kupon kullanılamıyorsa sebebini, kullanılabiliyorsa null döner.
function coupon_error($coupon, $subtotal)
{
$today = date('Y-m-d');
if (!$coupon) {
return 'Kupon kodu geçersiz.';
}
if ($coupon['starts_at'] && $coupon['starts_at'] > $today) {
return 'Bu kupon henüz kullanıma açılmadı.';
}
if ($coupon['ends_at'] && $coupon['ends_at'] < $today) {
return 'Bu kuponun süresi dolmuş.';
}
if ($coupon['usage_limit'] > 0 && $coupon['used'] >= $coupon['usage_limit']) {
return 'Bu kuponun kullanım limiti dolmuş.';
}
if ($subtotal < $coupon['min_total']) {
return 'Bu kupon ' . money($coupon['min_total']) . ' ve üzeri alışverişlerde geçerli.';
}
return null;
}
function coupon_discount($coupon, $subtotal)
{
$discount = $coupon['type'] === 'percent'
? $subtotal * $coupon['value'] / 100
: $coupon['value'];
return round(min($discount, $subtotal), 2);
}
function coupon_label($coupon)
{
return $coupon['type'] === 'percent' ? '%' . (float) $coupon['value'] : money($coupon['value']);
}
// Sepete uygulanmış kuponu doğrular. Geçersizse sepetten kaldırır.
function cart_coupon($subtotal)
{
$code = sess('coupon');
if (!$code) {
return null;
}
$coupon = coupon_find($code);
if (coupon_error($coupon, $subtotal)) {
sess_forget('coupon');
return null;
}
return ['code' => $coupon['code'], 'label' => coupon_label($coupon), 'discount' => coupon_discount($coupon, $subtotal)];
}
Lisans bilgisi projenin LICENSE dosyasında.