2017-09-21 3 views
0

Ich habe einen Einkaufswagen und ich muss die Angebotsfunktion erstellen, ich habe bereits den Prozentsatz aus und ich brauche etwas Hilfe mit Mengenpromotionen.Wie mache ich "Buy 3 und Pay 2" Mathe?

Zum Beispiel, wenn der Kunde 3 Artikel kauft, zahlt er nur für 2, aber wenn er 5 Artikel kaufen möchte, muss ich für die Förderung nur für 3 Artikel bewerben und 2 sind zum vollen Preis.

Hier ist es mein Code

$quand_in_cart = '5'; //quantity for product X in the cart 
    $prod_price = '1.5'; //product price 

    $percentage_off = NULL; 
    $buy_any = 3; //promotion on 3 items 
    $for_the_price = 2; //pay only 2 

    if($percentage_off and $quand_in_cart >= $buy_any){ 

     $price_discount = ($percentage_off/100) * $quand_in_cart; //works percentage done. 

    } elseif ($buy_any && $for_the_price) { 
     if($quand_in_cart >= $buy_any){ 
      //here i need some help 
     } 
    } 
+0

Beginnen Sie mit der höchsten Mengenprüfung zuerst: 'if (Menge> = 5) {} sonst if (Menge> = 3) ...' – aynber

+0

'$ fullPrice = $ quand_in_cart% $ buy_any; $ diskontiert = $ quand_in_cart - $ fullPrice; ' –

Antwort

2

3 kaufen und zahlen 2 mit dem Rest X mit dem vollen Preis.

In mathamatics heißt es:

items_to_pay = floor(amount/3) * 2 + X, with X = amount mod 3 

So in Ihrem Code:

$price = $prod_price * ((floor($quand_in_cart/$buy_any)*$for_the_price + ($quand_in_cart % $buy_any)) 

EDIT: einige weitere Erklärungen

floor($quand_in_cart/$buy_any)*$for_the_price // for every 3, only add 2 to the price 
($quand_in_cart % $buy_any) // how many items are the rest if you devide by $buy_any 

diese zwei Werte addieren und Sie haben die Anzahl der Artikel, die mit dem Preis multipliziert werden müssen für einen Gegenstand. Das ist es.

Verwandte Themen