PHP Session Array - store same item with different values -
i'm working on php cart system , have got problem.
the problem:
when user adds item , again, adds same item different values (different size or quantity) cart updates entry new values that's chosen user. previous details gets deleted.
solution have been looking for
if users adds item, , wants add same item different requirements, should added separate entry int cart session.(only if specific variable gets changed, example: single product different sizes).
how can in current code?
the cart
//add item in shopping cart if(isset($_post["type"]) && $_post["type"]=='add') { $product_code = filter_var($_post["product_code"], filter_sanitize_string); //product code $product_size = filter_var($_post["product_size"], filter_sanitize_number_int); //product size $product_qty = filter_var($_post["product_qty"], filter_sanitize_number_int); //product quantity $return_url = base64_decode($_post["return_url"]); //return url //mysqli query - details of item db using product code $results = $connection->query("select * products prod_code='$product_code' limit 1"); $obj = $results->fetch_object(); if ($results) { //we have product info //prepare array session variable $new_product = array(array('name'=>$obj->product_name, 'code'=>$product_code, 'size'=>$product_size, 'qty'=>$product_qty, 'price'=>$obj->price)); if(isset($_session["products"])) //if have session { $found = false; //set found item false foreach ($_session["products"] $cart_itm) //loop through session array { if($cart_itm["code"] == $product_code){ //the item exist in array $product[] = array('name'=>$cart_itm["name"], 'code'=>$cart_itm["code"], 'size'=>$product_size, 'qty'=>$product_qty, 'price'=>$cart_itm["price"]); $found = true; }else{ //item doesn't exist in list, retrieve old info , prepare array session var $product[] = array('name'=>$cart_itm["name"], 'code'=>$cart_itm["code"], 'size'=>$cart_itm["size"], 'qty'=>$cart_itm["qty"], 'price'=>$cart_itm["price"]); } } if($found == false) //we didn't find item in array { //add new user item in array $_session["products"] = array_merge($product, $new_product); }else{ //found user item in array list, , increased quantity $_session["products"] = $product; } }else{ //create new session var if not exist $_session["products"] = $new_product; } } //redirect original page header('location:'.$return_url); }
looking forward helpful answers.
thanks
***** update ******
code remove product cart:
//remove item shopping cart if(isset($_get["removep"]) && isset($_get["return_url"]) && isset($_session["products"])) { $product_code = $_get["removep"]; //get product code remove $return_url = base64_decode($_get["return_url"]); //get return url foreach ($_session["products"] $cart_itm) //loop through session array var { //if($cart_itm["code"]!=$product_code){ //item does,t exist in list if($cart_itm["code"] != $product_code && $cart_itm["size"] != $product_size){ $product[] = array( 'name'=>$cart_itm["name"], 'code'=>$cart_itm["code"], 'size'=>$cart_itm["size"], 'qty'=>$cart_itm["qty"], 'price'=>$cart_itm["price"] ); } //create new product list cart $_session["products"] = $product; } //redirect original page header('location:'.$return_url); }
the if
condition using mistake did. try one
if($cart_itm["code"] == $product_code && $cart_itm["size"] == $product_size)
instead of
if($cart_itm["code"] == $product_code)
the method of checking quantity not practice since can edit quantity alone in existing entry.
Comments
Post a Comment