2017-09-21 24 views
0

私はショッピングカートを持っており、オファー機能を構築する必要があります。すでにパーセンテージを完了しており、数量プロモーションに何らかの助けが必要です。「3を買って2を支払う」という計算方法を教えてください。

たとえば、顧客が3つのアイテムを購入した場合、2つしか支払うことはできませんが、5つのアイテムを購入する場合は、3つのアイテムのみをプロモーションに適用する必要があり、2つは満額です。

ここで、それは私のコード

$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

最初に最も多い数量チェックから始めてください。 'if(quantity> = 5){} else if(quantity> = 3)... ' – aynber

+0

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

答えて

2

だが3を購入し、完全な価格で休憩Xと2を支払います。 mathamaticsで

それは意味:あなたのコード内ので

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

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

EDIT:もう少し説明

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 

この2つの値を追加し、あなたが持っています価格に掛けなければならない項目の量1つのアイテムに対して。それでおしまい。

関連する問題