Guest User

cart_update.php

a guest
May 28th, 2014
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. <?php
  2. session_start();
  3. include_once("config.php");
  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. $product_unit = filter_var($_POST["product_unit"], FILTER_SANITIZE_STRING); // product code
  19. $qty1st = filter_var($_POST["qty1st"], FILTER_SANITIZE_NUMBER_INT); // product code
  20. $qty2nd = filter_var($_POST["qty2nd"], FILTER_SANITIZE_NUMBER_INT); // product code
  21. $qty3rd = filter_var($_POST["qty3rd"], FILTER_SANITIZE_NUMBER_INT); // product code
  22. $qty4th = filter_var($_POST["qty4th"], FILTER_SANITIZE_NUMBER_INT); // product code
  23. $return_url = base64_decode($_POST["return_url"]); //return url
  24.  
  25. //limit quantity for single product
  26. //if($product_qty > 10){
  27. // 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>');
  28. //}
  29.  
  30. //MySqli query - get details of item from db using product code
  31. $results = $mysqli->query("SELECT * FROM app WHERE counter='$product_code' LIMIT 1");
  32. $obj = $results->fetch_object();
  33.  
  34. if ($results) { //we have the product info
  35.  
  36. //prepare array for the session variable
  37. $new_product = array(array('name'=>$obj->item_name, 'code'=>$product_code, 'qty'=>$product_qty, 'price'=>$obj->price, 'unit'=>$product_unit, 'qty1st'=>$qty1st
  38. , 'qty2nd'=>$qty2nd, 'qty3rd'=>$qty3rd, 'qty4th'=>$qty4th));
  39.  
  40. if(isset($_SESSION["wala"])) //if we have the session
  41. {
  42. $found = false; //set found item to false
  43.  
  44. foreach ($_SESSION["wala"] as $cart_itm) //loop through session array
  45. {
  46. if($cart_itm["code"] == $product_code){ //the item exist in array
  47. $product[] = array('name'=>$cart_itm["name"], 'code'=>$cart_itm["code"], 'qty'=>$product_qty, 'price'=>$cart_itm["price"], 'unit'=>$cart_itm["unit"], 'qty1st'=>$qty1st, 'qty2nd'=>$qty2nd, 'qty3rd'=>$qty3rd, 'qty4th'=>$qty4th);
  48. $found = true;
  49. }else{
  50. //item doesn't exist in the list, just retrive old info and prepare array for session var
  51. $product[] = array('name'=>$cart_itm["name"], 'code'=>$cart_itm["code"], 'qty'=>$cart_itm["qty"], 'price'=>$cart_itm["price"], 'unit'=>$cart_itm["unit"], 'qty1st'=>$cart_itm["qty1st"]
  52. , 'qty2nd'=>$cart_itm["qty2nd"], 'qty3rd'=>$cart_itm["qty3rd"], 'qty4th'=>$cart_itm["qty4th"]);
  53. }
  54. }
  55.  
  56. if($found == false) //we didn't find item in array
  57. {
  58. //add new user item in array
  59. $_SESSION["wala"] = array_merge($product, $new_product);
  60. }else{
  61. //found user item in array list, and increased the quantity
  62. $_SESSION["wala"] = $product;
  63. }
  64.  
  65. }else{
  66. //create a new session var if does not exist
  67. $_SESSION["wala"] = $new_product;
  68. }
  69. }
  70. //redirect back to original page
  71. header('Location:'.$return_url);
  72. }
  73. //remove item from shopping cart
  74. if(isset($_GET["removep"]) && isset($_GET["return_url"]) && isset($_SESSION["wala"]))
  75. {
  76. $product_code = $_GET["removep"]; //get the product code to remove
  77. $return_url = base64_decode($_GET["return_url"]); //get return url
  78.  
  79. foreach ($_SESSION["wala"] as $cart_itm) //loop through session array var
  80. {
  81. if($cart_itm["code"]!=$product_code){ //item does,t exist in the list
  82. $product[] = array('name'=>$cart_itm["name"], 'code'=>$cart_itm["code"], 'qty'=>$cart_itm["qty"], 'price'=>$cart_itm["price"], 'unit'=>$cart_itm["unit"], 'qty1st'=>$cart_itm["qty1st"]
  83. , 'qty2nd'=>$cart_itm["qty2nd"], 'qty3rd'=>$cart_itm["qty3rd"], 'qty4th'=>$cart_itm["qty4th"]);
  84. }
  85. //create a new product list for cart
  86. $_SESSION["wala"] = $product;
  87. }
  88. //redirect back to original page
  89. header('Location:'.$return_url);
  90. }
  91. ?>
Advertisement
Add Comment
Please, Sign In to add comment