My application rewards loyalty by printing a coupon code on the receipt / thank you page every 15th order. The coupon value is equal to the total price of one of the previous 15 orders, pulled at random.
This was working for a while, but now for some reason, every new user is getting a coupon code on their very first order:
$count = dbFuncs::countRewardsProgress($_SESSION['user_id'])['count'];
$displayRewardsCode = dbFuncs::displayRewardsCode()['rewardsCode'];
if ( $count > 0 ) {
$remainder = $count % 15;
}
if($remainder == 0) {
echo "Congratulations, You've earned a $$previousOrderValueRoulette coupon code! We thank you for your continued business.";
echo "<br />";
echo "<br />";
echo 'Coupon code: ';
echo $displayRewardsCode;
dbFuncs::assignRewardsCode($previousOrderValueRoulette, $_SESSION['user_id'], $displayRewardsCode);
} else {
echo 'rewards progress: ';
echo $count;
echo '/15 orders completed';
}
How can I ensure that customers only receive a coupon every 15th order?
Because if $count
is 0, $reminder
is not initialized, so it is equal to 0 in your next condition.
To solve, change the second condition to also check that $count
is greater than 0
if ( $count > 0 ) {
$remainder = $count % 15;
}
if($count > 0 && $remainder == 0) {
echo "Congratulations, You've earned a $$previousOrderValueRoulette coupon code! We thank you for your continued business.";
echo "<br />";
echo "<br />";
echo 'Coupon code: ';
echo $displayRewardsCode;
dbFuncs::assignRewardsCode($previousOrderValueRoulette, $_SESSION['user_id'], $displayRewardsCode);
} else {
echo 'rewards progress: ';
echo $count;
echo '/15 orders completed';
}