[英]How to check if the voucher has already been used
我遇到了一個混亂的循環,實際上,我正在嘗試檢查,
第一個條件:如果憑證類型為AMOUNT = AMOUNT
且value > 0 and $totalRules <= 1
表示這是第一次,如果用戶正在使用優惠券,則允許用戶使用它。
第二個條件:我正在檢查AMOUNT = AMOUNT
且value > 0 and $totalRules >=1
表示用戶已經使用了優惠券。
閾值條件:如果AMOUNT !=AMOUNT
且$totalRules >=1
表示這次用戶選擇了其他優惠券,可能是PERCENT類型,則我們不允許用戶使用此優惠券。
現在,代碼第一次可以正常工作,但是即使刷新頁面,其他條件也可以實現,即使我使用break;
也不行break;
誰能幫我解決這個問題? 我知道我需要改進該代碼,需要老年人的幫助。
$vDetail = array('AMOUNT', 'ONLY'); // For now check if the coupon type is AMOUNT (set from admin)
$voucherAllowed = 1; // Total number of voucher allowed, Global variable.
$totalRules = 1; // First tiem it'll be 0, then always adds 1 in that variable to check if user has already used any coupon.
$voucherTypeArray ['AMOUNT'] = 8.00;
$voucherTypeArray ['PERCENT'] = 0.00;
foreach($voucherTypeArray as $thisVoucher => $vValue )
{
echo "$vDetail[0] == $thisVoucher && $vValue>0 && $totalRules <= $voucherAllowed <br>";
if($vDetail[0]==$thisVoucher && $vValue>0 && (int)$totalRules <= $voucherAllowed)
{ $throwError = '0'; break; } // Continue to the next rule and add this one.
elseif($vDetail[0]==$thisVoucher && $vValue > 0 && (int)$totalRules >= $voucherAllowed)
{
$throwError = "Only $voucherAllowed voucher can be redeem at this time.";
break;
}
elseif($vDetail[0]!=$thisVoucher && (int)$totalRules >= $voucherAllowed)
{
$throwError = 'aYou cannot redeem this voucher at this time.';
break;
}
}
我在您的代碼中看到兩個問題:
if($vDetail[0]==$thisVoucher && $vValue>0 && (int)$totalRules <= $voucherAllowed)
$vDetail
不是一個數組,但是您正在這樣訪問它。
這意味着它將$vDetail
視為一個字符數組(在本例中為首字母A
)。 確保這就是您要執行的操作(您的代碼有效地進行了比較: $voucher['AMOUNT'] == 'A'
)。
並且您的條件不考慮$totalRules == 1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.