如何修改此功能代码才能正常工作?

时间:2019-06-08 01:53:29

标签: php

我在Buy One Get One Half Price in PHP shopping cart total处找到了此代码,我正在尝试为活动参加者对其进行修改,以使他们注册的每个其他参加者都享有40%的折扣。如果有四口之家的参加者A签约,参加者A将支付全价,其余3名参加者将各自获得40%的门票折扣。

在没有找到任何帮助之前,我一直在搜索各种在线论坛,但它与我要寻找的非常接近,但有所不同。到目前为止,我的代码无法正常工作,我将不胜感激。

$total = 0;
$ticket['Price'] = 10;
$ticket['Item'] = 1;

$ticket_no = 3; 

if ($ticket['Item'] == "tickets" and $ticket_no % 2 == 0 ) {
    //calculate here buy one get each additional at 40% discount
    $eventPrice = ($ticket_no)*$ticket['Price'];
    $discountPrice = ($ticket_no)*($ticket['Price’]*.4);

    $total = $eventPrice+$discountPrice;

} else {
    $ticket_no = $ticket_no-1;

    $eventPrice = ($ticket_no)*$ticket['Price'];
    $discountPrice = ($ticket_no)*($ticket['Price’]*.4);

    $total = $eventPrice+$discountPrice+$ticket['Price'];

}

echo $total;
?>

2 个答案:

答案 0 :(得分:0)

predict(modFOI, newdata = testdat)

答案 1 :(得分:0)

  

如果有四口之家的出席者A签约,出席者A将全额支付,其余3名参与者中的每一个将获得40%的门票折扣。

您可以使用此代码并根据用例进行修改。

$total = 0;
$ticket['Price'] = 10;

// discount
$discount = 0.4; 

// I assume this is the number of tickets of a family of 4 signup
$ticket_no = 4; 

// set current price as the total for "Attendee A"
$total = $ticket['Price'];

// then decrement the ticket_no so it is now 3.
$ticket_no--;

// make sure ticket no is greater than 0; so we can calculate for the remaining
if ($ticket_no > 0){
    $discountedPrice = $ticket['Price'] - ($ticket['Price'] * $discount);

    $total += ($ticket_no) * $discountedPrice;
}

echo $total;

因此,这里的预期总金额为28美元。