a litle help with pricing in cart

Discussion in 'PHP' started by Kyriakos, Dec 20, 2013.

  1. #1
    Hi,
    i have a e-shop and i have some quantitative offers for some products.
    Example bellow:
    [​IMG]

    i want when the customers adds for example the quantity
    1 to 4 to the cart, the price of 15 euros,
    for 5 to 7 the price of 12 euros,
    for 8 to 11 the price of 10 euros
    and 12+ the price of 9 euros.

    how is this possible? do you have any idea?

    this is my SQL query
    INSERT INTO cart (sessionid,prodID,qty,price VALUES ( '$sesid','$prodID','$qty','$price')
    Code (markup):
    this quantities and prices are in another table named "quant_prices".

    any idea?
    thanks in advance.
     
    Kyriakos, Dec 20, 2013 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    Just do a valuation before adding $price to the query - something like:
    
    <?php
    
    $qty = 10;
    
    switch(true) {
       case in_array($qty, range(1,4)):
       $price = 15;
       break;
       case in_array($qty, range(5,8)):
       $price = 12;
       break;
       case in_array($qty, range(9,11)):
       $price = 10;
       break;
       case in_array($qty, range(12,200)):
       $price = 9;
       break;
       default:
       $price = 15;
    }
    
    echo $price;
    
    ?>
    
    PHP:
    You can of course adjust the ranges and price-setting as needed - I used 200 as max for the last range, you can of course set this to some insanely high number (200000) which will never be ordered, or you can add more discounts with larger amounts...

    EDIT: Of course, the $qty and echo before and after the switch-statement is just for illustrative purposes - you will remove these for your page, as the values will be dynamically calculated, and you don't need the echo :)
     
    PoPSiCLe, Dec 20, 2013 IP