php - shopping cart issues while adding color option -
please check demo. here 1 product different color not added cart updated same product different color not right way.
here code add product.
if(isset($_post["product_code"]) && isset($_post["product_color"])) { foreach($_post $key => $value){ $new_product[$key] = filter_var($value, filter_sanitize_string); //create new product array } //we need product name , price database. $statement = $mysqli_conn->prepare("select product_name, product_price products_list product_code=? limit 1"); $statement->bind_param('s', $new_product['product_code']); $statement->execute(); $statement->bind_result($product_name, $product_price); while($statement->fetch()){ $new_product["product_name"] = $product_name; //fetch product name database $new_product["product_price"] = $product_price; //fetch product price database $new_product["product_color"] = $_post["product_color"]; if(isset($_session["products"])){ //if session var exist $check = $_session["products"][$new_product['product_code']][$new_product['product_color']]; if(isset($check)) //check item exist in products array { unset($check); //unset old item } } $check = $new_product; //update products new item array } $total_items = count($_session["products"]); //count total items die(json_encode(array('items'=>$total_items))); //output json }
in above code how should add color of product 1 product different color can added?
list items in cart code
if(isset($_post["load_cart"]) && $_post["load_cart"]==1) { if(isset($_session["products"]) && count($_session["products"])>0){ //if have session variable $cart_box = '<ul class="cart-products-loaded">'; $total = 0; foreach($_session["products"] $product){ //loop though items , prepare html content //set variables use them in html content below $product_name = $product["product_name"]; $product_price = $product["product_price"]; $product_code = $product["product_code"]; $product_qty = $product["product_qty"]; $product_color = $product["product_color"]; //$product_size = $product["product_size"]; //$cart_box .= "<li> $product_name (qty : $product_qty | $product_color | $product_size ) — $currency ".sprintf("%01.2f", ($product_price * $product_qty)). " <a href=\"#\" class=\"remove-item\" data-code=\"$product_code\">×</a></li>"; $cart_box .= "<li> $product_name (qty : $product_qty | $product_color ) — $currency ".sprintf("%01.2f", ($product_price * $product_qty)). " <a href=\"#\" class=\"remove-item\" data-code=\"$product_code\">×</a></li>"; $subtotal = ($product_price * $product_qty); $total = ($total + $subtotal); } $cart_box .= "</ul>"; $cart_box .= '<div class="cart-products-total">total : '.$currency.sprintf("%01.2f",$total).' <u><a href="view_cart.php" title="review cart , check-out">check-out</a></u></div>'; die($cart_box); //exit , output content }else{ die("your cart empty"); //we have empty cart } }
remove item cart
if(isset($_get["remove_code"]) && isset($_session["products"])) { $product_code = filter_var($_get["remove_code"], filter_sanitize_string); //get product code remove if(isset($_session["products"][$product_code])) { unset($_session["products"][$product_code]); } $total_items = count($_session["products"]); die(json_encode(array('items'=>$total_items))); }
in above code how color of product should compared? e.g. cool t-shirt | blue , cool t-shirt | red , if cool t-shirt | red removed both should not removed.
can please me in this?
Comments
Post a Comment