带有最大现金返还的php折扣券

时间:2019-01-15 08:04:50

标签: php discount

我想用优惠券打折的php公式,但必须有最大的现金返还。

示例:20%的折扣,最多返现40000。

案例:如果输入的价格为100000且有20%的凭单,则可赚取的现金返还为20000,但是如果输入的价格为500000且有20%的凭单,则返还的现金也仅为40000。

帮我解决问题,我已经搜索过Google,但没有找到解决方法。

我有这个脚本:

我的脚本:

<?php
    require_once 'conn.php';
    $coupon_code = $_POST['coupon'];
    $price = $_POST['price'];
    $totalorder = $_POST ['totalorder'];

    $query = mysqli_query($conn, "SELECT * FROM `coupon` WHERE `coupon_code` = '$coupon_code' && `status` = 'Valid'") or die(mysqli_error());
    $count = mysqli_num_rows($query);
    $fetch = mysqli_fetch_array($query);
    $array = array();
    if($count > 0){
        $discount = $fetch['discount'] / 100;
        $total = $discount * $price;
        $array['discount'] = $fetch['discount'];
        $array['price'] = ($price - $total) * $totalorder;
        echo json_encode($array);
    }else{
        echo "error";
    }
?>

1 个答案:

答案 0 :(得分:0)

我希望这是您所期望的:

<?php
function voucher_calc($price, $max, $voucher){
    $discount = $price * $voucher;
    if($discount >= $max){
        return $max;
    }else{
        return $discount;
    }
}

$price = 500000;
$voucher = 0.2;
$max = 40000;

echo voucher_calc($price, $max, $voucher);