Guest User

Untitled

a guest
May 27th, 2015
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.62 KB | None | 0 0
  1. <?php
  2. session_start(); //start session
  3. include_once("db.php"); //include config file
  4.  
  5. //empty cart by distroying current session
  6. if(isset($_GET["emptycart"]) && $_GET["emptycart"]==1)
  7. {
  8. $return_url = base64_decode($_GET["return_url"]); //return url
  9. session_destroy();
  10. header('Location:'.$return_url);
  11. }
  12.  
  13. //add item in shopping cart
  14. if(isset($_POST["type"]) && $_POST["type"]=='add')
  15. {
  16. $product_code = filter_var($_POST["product_code"], FILTER_SANITIZE_STRING); //product code
  17. $product_qty = filter_var($_POST["product_qty"], FILTER_SANITIZE_NUMBER_INT); //product code
  18. $return_url = base64_decode($_POST["return_url"]); //return url
  19.  
  20. //limit quantity for single product
  21. if($product_qty > 10){
  22. die('<div align="center">This demo does not allowed more than 10 quantity!<br /><a href="http://sanwebe.com/assets/paypal-shopping-cart-integration/">Back To Products</a>.</div>');
  23. }
  24.  
  25. //MySqli query - get details of item from db using product code
  26. $results = $mysqli->query("SELECT product_name,price FROM products WHERE product_code='$product_code' LIMIT 1");
  27. $obj = $results->fetch_object();
  28.  
  29. if ($results) { //we have the product info
  30.  
  31. //prepare array for the session variable
  32. $new_product = array(array('name'=>$obj->product_name, 'code'=>$product_code, 'qty'=>$product_qty, 'price'=>$obj->price));
  33.  
  34. if(isset($_SESSION["products"])) //if we have the session
  35. {
  36. $found = false; //set found item to false
  37.  
  38. foreach ($_SESSION["products"] as $cart_itm) //loop through session array
  39. {
  40. if($cart_itm["code"] == $product_code){ //the item exist in array
  41.  
  42. $product[] = array('name'=>$cart_itm["name"], 'code'=>$cart_itm["code"], 'qty'=>$product_qty, 'price'=>$cart_itm["price"]);
  43. $found = true;
  44. }else{
  45. //item doesn't exist in the list, just retrive old info and prepare array for session var
  46. $product[] = array('name'=>$cart_itm["name"], 'code'=>$cart_itm["code"], 'qty'=>$cart_itm["qty"], 'price'=>$cart_itm["price"]);
  47. }
  48. }
  49.  
  50. if($found == false) //we didn't find item in array
  51. {
  52. //add new user item in array
  53. $_SESSION["products"] = array_merge($product, $new_product);
  54. }else{
  55. //found user item in array list, and increased the quantity
  56. $_SESSION["products"] = $product;
  57. }
  58.  
  59. }else{
  60. //create a new session var if does not exist
  61. $_SESSION["products"] = $new_product;
  62. }
  63.  
  64. }
  65.  
  66. //redirect back to original page
  67. header('Location:'.$return_url);
  68. }
  69.  
  70. //remove item from shopping cart
  71. if(isset($_GET["removep"]) && isset($_GET["return_url"]) && isset($_SESSION["products"]))
  72. {
  73. $product_code = $_GET["removep"]; //get the product code to remove
  74. $return_url = base64_decode($_GET["return_url"]); //get return url
  75.  
  76.  
  77. foreach ($_SESSION["products"] as $cart_itm) //loop through session array var
  78. {
  79. if($cart_itm["code"]!=$product_code){ //item does,t exist in the list
  80. $product[] = array('name'=>$cart_itm["name"], 'code'=>$cart_itm["code"], 'qty'=>$cart_itm["qty"], 'price'=>$cart_itm["price"]);
  81. }
  82.  
  83. //create a new product list for cart
  84. $_SESSION["products"] = $product;
  85. }
  86.  
  87. //redirect back to original page
  88. header('Location:'.$return_url);
  89. }
Add Comment
Please, Sign In to add comment