promotional price routine

Discussion in 'PHP' started by afonseca, Nov 4, 2009.

  1. #1
    Hi there,

    I have a ecommerce site where I need to place an promotional price routine. What I need it to do is this: I have a columm with the normal price and a new one with the promotional price. Do this in the normal display page is simple and implemented. Now I have the problem in the shopping chart. I want to implement the code in order to get (if columm with promotion exist) the new price and not the normal one. This is what I have but not working:


    <?php
    function checkPrice($realprice)
    {
    if ($row['preco_desc']<>"" and $row['promocao']==1){
    checkprice(echo $row['preco_desc'];) }
    else{
    checkprice(echo $row['euro'];)
    }
    }

    ?>


    And then I pass the variable.

    $preco=$realprice;
    $preco = str_replace(",",".",$preco);
    $Subtotal=floatval($preco)*$qtd;
    $totalCost += floatval($preco)*$qtd;
    $Subtotal=str_replace(".",",",$Subtotal);


    Can anyone figure this out or give samples or examples?

    Thanks in advance.
    António
     
    afonseca, Nov 4, 2009 IP
  2. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Firstly, you've not defined $row anywhere in your function, so they will be null always. Secondly, you need to return the values not echo them out. It would make much more sense if you have a function like
    function checkprice($product_id) {
      //Run query here on product table to get the product info
      //check if promo price exists, if so use that, if not use the normal price
      //return the value
    }
    PHP:
     
    JAY6390, Nov 4, 2009 IP
  3. afonseca

    afonseca Peon

    Messages:
    48
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for your input Jay,

    I've defined $row now (it was defined before in the code, out of the functiont ho, dunno if that matters), and returned the values instead of echoeing them.

    So I came up with this:

    <?php
                                                   
    function checkPrice($preco)
    {                              
                    $result = mysql_query("select * from cart inner join sub_produto on cart.itemId = sub_produto.ID where cart.cookieId = '" . GetCartId() . "' order by sub_produto.codigo asc");
                    $row = mysql_fetch_array($result);
                                                                                                  
                    if ($row['promo_price']<>"" and $row['promotion']==1){
                    checkPrice(return $row['promo_price];) }
                    else{
                    checkPrice(return $row['normal_price'];) }
                                                                                                                                                                 
                                                                   }
                                                                   
                                                                   ?>
    PHP:
    I need $price to be the price of the product (with promotion if there's 1) so I can handle it after on my cart total.
    Unfourtunly this gives me a programming error.

    Any ideas why?

    Thanks!
     
    afonseca, Nov 4, 2009 IP
  4. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #4
    JAY6390, Nov 4, 2009 IP