添加颜色选项时购物车问题

时间:2015-07-30 07:54:44

标签: php mysql ajax shopping-cart cart

请检查此demo。这里有一种不同颜色的产品没有添加到购物车中,但它更新了相同的产品,颜色不同,这是不正确的。

此处是添加产品的代码。

    if(isset($_POST["product_code"]) && isset($_POST["product_color"]))
{
    foreach($_POST as $key => $value){
        $new_product[$key] = filter_var($value, FILTER_SANITIZE_STRING); //create a new product array 
    }

    //we need to get product name and price from database.
    $statement = $mysqli_conn->prepare("SELECT product_name, product_price FROM products_list WHERE 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 from database
        $new_product["product_price"] = $product_price;  //fetch product price from database
        $new_product["product_color"] = $_POST["product_color"];

        if(isset($_SESSION["products"])){  //if session var already 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 with new item array   
    }

    $total_items = count($_SESSION["products"]); //count total items
    die(json_encode(array('items'=>$total_items))); //output json 

}

在上面的代码中,我应该如何添加产品的颜色,以便添加不同颜色的产品?

列出购物车代码中的商品

 if(isset($_POST["load_cart"]) && $_POST["load_cart"]==1)
    {

        if(isset($_SESSION["products"]) && count($_SESSION["products"])>0){ //if we have session variable
            $cart_box = '<ul class="cart-products-loaded">';
            $total = 0;
            foreach($_SESSION["products"] as $product){ //loop though items and prepare html content

                //set variables to 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 ) &mdash; $currency ".sprintf("%01.2f", ($product_price * $product_qty)). " <a href=\"#\" class=\"remove-item\" data-code=\"$product_code\">&times;</a></li>";

                $cart_box .=  "<li> $product_name (Qty : $product_qty | $product_color ) &mdash; $currency ".sprintf("%01.2f", ($product_price * $product_qty)). " <a href=\"#\" class=\"remove-item\" data-code=\"$product_code\">&times;</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 and Check-Out">Check-out</a></u></div>';
            die($cart_box); //exit and output content
        }else{
            die("Your Cart is empty"); //we have empty cart
        }
    }

从购物车中删除商品

if(isset($_GET["remove_code"]) && isset($_SESSION["products"]))
{
    $product_code   = filter_var($_GET["remove_code"], FILTER_SANITIZE_STRING); //get the product code to remove

    if(isset($_SESSION["products"][$product_code]))
    {
        unset($_SESSION["products"][$product_code]);
    }

    $total_items = count($_SESSION["products"]);
    die(json_encode(array('items'=>$total_items)));
}

在上面的代码中还应该如何比较产品的颜色?例如酷T恤|蓝色和酷T恤|红色和如果酷T恤|红色被删除,然后两个都不应被删除。

有人可以帮助我吗?

0 个答案:

没有答案