0

Hello everyone, I have 2 issues that I've been trying to solve for days now

1.  I'm trying to display an error message if a user tries to purchase more quantities of an item than what are actually available

2.  I can't figure out how to decrease the actual in stock quantity of an item when a user clicks on the 'Place order' button on my site

If anyone has knowledge of this please reply or send me a message thanks

here is the code I have for the shopping cart page

<?php

    // shopping cart



    //calculate shopping cart value



    if (isset($_GET["shopping_cart"]) || isset($_POST["shopping_cart"]))

    {



        if (isset($_GET["add2cart"]) && $_GET["add2cart"]>0) //add product to cart with productID=$add

        {



            $q = db_query("select in_stock from ".PRODUCTS_TABLE." where productID='".$_GET["add2cart"]."'") or die (db_error());

            $is = db_fetch_row($q); $is = $is[0];



            //$_SESSION[gids] contains product IDs

            //$_SESSION[counts] contains product quantities ($_SESSION[counts][$i] corresponds to $_SESSION[gids][$i])

            //$_SESSION[gids][$i] == 0 means $i-element is 'empty'

            if (!isset($_SESSION["gids"]))

            {

                $_SESSION["gids"] = array();

                $_SESSION["counts"] = array();

            }

            //check for current item in the current shopping cart content

            $i=0;

            while ($i<count($_SESSION["gids"]) && $_SESSION["gids"][$i] != $_GET["add2cart"]) $i++;

            if ($i < count($_SESSION["gids"])) //increase current product's quantity

            {

                $_SESSION["counts"][$i]++;

            }

            else //no item - add it to $gids array

            {

                $_SESSION["gids"][] = $_GET["add2cart"];

                $_SESSION["counts"][] = 1;

            }



            header("Location: index.php?shopping_cart=yes");



        }





        if (isset($_GET["remove"]) && $_GET["remove"] > 0) //remove from cart product with productID == $remove

        {

            $i=0;

            while ($i<count($_SESSION["gids"]) && $_SESSION["gids"][$i] != $_GET["remove"]) $i++;

            if ($i<count($_SESSION["gids"])) $_SESSION["gids"][$i] = 0;



            header("Location: index.php?shopping_cart=yes");

        }





        if (isset($_POST["update"])) //update shopping cart content

        {



            foreach ($_POST as $key => $val)

                if (strstr($key, "count_"))

                {

                  //select product's in stock level

                  $q = db_query("select in_stock from ".PRODUCTS_TABLE." where productID='".str_replace("count_","",$key)."'") or die (db_error());

                  $is = db_fetch_row($q); $is = $is[0];



                    if ($val > 0)

                    {

                        for ($i=0; $i<count($_SESSION["gids"]); $i++)

                        {

                            if ($_SESSION["gids"][$i] == str_replace("count_","",$key))

                            {

                                $_SESSION["counts"][$i] = floor($val);

                            }

                        }

                    }

                    else //remove

                    {

                        $i=0;

                        while ($_SESSION["gids"][$i] != str_replace("count_","",$key) && $i<count($_SESSION["gids"])) $i++;

                        $_SESSION["gids"][$i] = 0;

                    }

                  }



            header("Location: index.php?shopping_cart=yes");



        }



        if (isset($_GET["clear_cart"])) //completely clear shopping cart

        {

            //clear cart

            unset($_SESSION["gids"]);

            unset($_SESSION["counts"]);



            header("Location: index.php?shopping_cart=yes");

        }







        //shopping cart items count

        $c = 0;

        if (isset($_SESSION["gids"]))

            for ($j=0; $j<count($_SESSION["gids"]); $j++)

                if ($_SESSION["gids"][$j]) $c += $_SESSION["counts"][$j];



        //not empty?

        if (isset($_SESSION["gids"]) && $c)

        {

            $k = 0; //total cart value

            $products = array();

            for ($i=0; $i<count($_SESSION["gids"]); $i++)

              if ($_SESSION["gids"][$i])

              {

                $q = db_query("SELECT name, Price, product_code FROM ".PRODUCTS_TABLE." WHERE productID='".$_SESSION["gids"][$i]."'") or die (db_error());

                if ($r = db_fetch_row($q))

                {

                    $tmp = array("id"=>$_SESSION["gids"][$i], "name"=>$r[0], "quantity"=>$_SESSION["counts"][$i], "cost"=>show_price($_SESSION["counts"][$i]*$r[1]), "product_code"=>$r[2]);



                    $products[] = $tmp;



                    $k += $_SESSION["counts"][$i]*$r[1];

                }

              }



            //total...

            $smarty->assign("cart_content", $products);

            $smarty->assign("cart_total", show_price($k));



        }

        else

        {

            $smarty->assign("cart_total", "");

        }



        $smarty->assign("main_content_template", "shopping_cart.tpl.html");





    }

?>

this is the code for my order page

<?php

    //place order: save to the database, send notifications, gateway processing



    if (isset($_GET["order_placement_result"])) //show 'order successful' page

    {       

        $smarty->assign("order_id", $_SESSION["order_id"]);

        $smarty->assign("order_amount", $_SESSION["order_amount"]);



        $smarty->assign("main_content_template", "order_place.tpl.html");

        $smarty->assign("order_is_placed", $_GET["order_placement_result"]);

    }

    else if (isset($_POST["complete_order"])) //place order

    {



        //shopping cart items count

        $c = 0;

        if (isset($_SESSION["gids"]))

            for ($j=0; $j<count($_SESSION["gids"]); $j++)

                if ($_SESSION["gids"][$j]) $c += $_SESSION["counts"][$j];



        //not empty?

        if (isset($_SESSION["gids"]) && $c)

        {

            //insert order into database



            db_query("insert into ".ORDERS_TABLE." (order_time, cust_firstname, cust_lastname, cust_email, cust_country, cust_zip, cust_state, cust_city, cust_address, cust_phone) values ('".get_current_time()."','".$_POST["first_name"]."','".$_POST["last_name"]."','".$_POST["email"]."','".$_POST["country"]."','".$_POST["zip"]."','".$_POST["state"]."','".$_POST["city"]."','".$_POST["address"]."','".$_POST["phone"]."');") or die (db_error());

            $oid = db_insert_id(); //order ID



            //now move shopping cart content to the database



            $k = 0; //total cart value

            $products = array();

            $adm = ""; //order notification for administrator



            for ($i=0; $i<count($_SESSION["gids"]); $i++)

              if ($_SESSION["gids"][$i])

              {

                $q = db_query("SELECT name, Price, product_code, in_stock, items_sold FROM ".PRODUCTS_TABLE." WHERE productID='".$_SESSION["gids"][$i]."'") or die (db_error());

                if ($r = db_fetch_row($q))

                {

                    //product info

                    $tmp = array(

                        $_SESSION["gids"][$i],

                        $r[0],

                        $_SESSION["counts"][$i],

                        ($_SESSION["counts"][$i]*$r[1])." ".$currency_iso_3,

                        $r[2],

                        $r[3] = $r[3]--,

                        $r[4] = $r[4]++

                    );



                    //store ordered products info into database

                    $articul = trim($tmp[4]) ? "[".$tmp[4]."] " : "";

                    db_query("insert into ".ORDERED_CARTS_TABLE." (orderID, productID, name, Price, Quantity) values ('$oid', '".$tmp[0]."', '".$articul.$tmp[1]."', '".$r[1]."', '".$tmp[2]."');");



                    $products[] = $tmp;



                    //update order amount

                    $k += $_SESSION["counts"][$i]*$r[1];



                    //order notification for administrator - update

                    $adm .= $articul.$tmp[1]." (x".$tmp[2]."): ".$tmp[3]."\n";



                }

              }



            //assign order content to smarty

            $smarty_mail->assign("order_content", $products);

            $smarty_mail->assign("order_total", $k." ".$currency_iso_3);

            $smarty_mail->assign("order_id", $oid);

            $smarty_mail->assign("order_custname", $_POST["first_name"]." ".$_POST["last_name"]);

            $smarty_mail->assign("order_shipping_address", $_POST["address"]."\n".$_POST["city"]." ".$_POST["state"]."  ".$_POST["zip"]."\n".$_POST["country"]);



            $_SESSION["order_id"] = $oid;

            $_SESSION["order_amount"] = $k;



            //send message to customer

            mail($_POST["email"], EMAIL_CUSTOMER_ORDER_NOTIFICATION_SUBJECT, $smarty_mail->fetch("order_notification.tpl.html"), "From: \"".CONF_SHOP_NAME."\"<".CONF_GENERAL_EMAIL.">\n".stripslashes(EMAIL_MESSAGE_PARAMETERS)."\nReturn-path: <".CONF_GENERAL_EMAIL.">");



            //notification for administrator

            $od = STRING_ORDER_ID.": $oid\n\n";

            $adm .= "\n".CUSTOMER_FIRST_NAME." ".$_POST["first_name"]."\n".CUSTOMER_LAST_NAME." ".$_POST["last_name"]."\n".CUSTOMER_ADDRESS.": ".$_POST["country"].", ".$_POST["zip"].", ".$_POST["state"].",  ".$_POST["city"].", ".$_POST["address"]."\n".CUSTOMER_PHONE_NUMBER.": ".$_POST["phone"]."\n".CUSTOMER_EMAIL.": ".$_POST["email"];

            mail(CONF_ORDERS_EMAIL, EMAIL_ADMIN_ORDER_NOTIFICATION_SUBJECT, $od.$adm, "From: \"".CONF_SHOP_NAME."\"<".CONF_GENERAL_EMAIL.">\n".stripslashes(EMAIL_MESSAGE_PARAMETERS)."\nReturn-path: <".CONF_GENERAL_EMAIL.">");



            unset($_SESSION["gids"]);

            unset($_SESSION["counts"]);



            //show order placement result

            header("Location: index.php?order_placement_result=1");



        }

        else //empty shopping cart

        {

            header("Location: index.php?shopping_cart=yes");

        }

    }



?>

Edited by TheGame1083

3
Contributors
5
Replies
6
Views
5 Years
Discussion Span
Last Post by TheGame1083
0

this is the piece of code I need help with for shopping_cart.php

if (isset($_POST["update"])) //update shopping cart content
        {
            foreach ($_POST as $key => $val)

                if (strstr($key, "count_"))
                {
                  //select product's in stock level
                  $q = db_query("select in_stock from ".PRODUCTS_TABLE." where productID='".str_replace("count_","",$key)."'") or die (db_error());
                  $is = db_fetch_row($q); $is = $is[0];

                    if ($val > 0)
                    {
                        for ($i=0; $i<count($_SESSION["gids"]); $i++)
                        {
                            if ($_SESSION["gids"][$i] == str_replace("count_","",$key))
                            {
                                $_SESSION["counts"][$i] = floor($val);
                            }
                        }
                    }

                    else //remove
                    {
                        $i=0;
                        while ($_SESSION["gids"][$i] != str_replace("count_","",$key) && $i<count($_SESSION["gids"])) $i++;
                        $_SESSION["gids"][$i] = 0;
                    }
                  }
            header("Location: index.php?shopping_cart=yes");
        }
0

the query can be something like this.

SET product_InInventory = CountBeforeThePurchase - 'ItemPurchasedByYourClient'

Then you send another query to get the updated count.. Say, you only have 10 items remaining, and your client type in 11.. On your controller file, you should alert the client that they cannot select more than 10.

Another option is to pre-populate your select menu for quantity.. for example

 $available = 11; 
 ## the integer 11 should be from your database.
 $option = array();
 for($qA = 1; $qA <= $available; $qA++){
 $option[] = $qA;

 $smartyOption[] = $option;

 }

 $smarty->assign('smartyOption',$smartyOption);


 ## on your template side it should be something like this

 <select name="quantity">
 {foreach $smartyOption option}
 <option>{$option}</option>
 {/foreach}
 </select>

sorry I can't use your codes as example.. I can't just decipher the logic flows of your script.

Edited by veedeoo: more info added

0

I figured out the solution a bit differently..here is what I did

//update shopping cart content
if (isset($_POST["update"]))
        {
            foreach ($_POST as $key => $val)

                if (strstr($key, "count_"))
                {
                  //select product's in stock level
                  $q = db_query("select in_stock from ".PRODUCTS_TABLE." where productID='".str_replace("count_","",$key)."'") or die (db_error());
                  $is = db_fetch_row($q); $is = $is[0]; //in_stock value

                    if ($val > 0 && $val <= $is[0])  // value in quantity textbox
                    {
                        for ($i=0; $i<count($_SESSION["gids"]); $i++)
                        {
                            if ($_SESSION["gids"][$i] == str_replace("count_","",$key))
                            {
                                $_SESSION["counts"][$i] = floor($val);
                            }
                        }
                    }
                    else //remove
                    {
                        echo "<script>alert('Please enter a lower quantity')</script>";
                    }
                  }
        }
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.