如果尺寸不同,请将购物车细节中的产品分开

时间:2018-05-01 13:06:09

标签: javascript php jquery

我正在开发一个鞋类和服装在线商店的项目。如果尺寸不同,我该如何分离产品。正如你在屏幕截图(链接下面)中看到的那样,我添加了Nike LeBron 15' New Heights'大小6.现在,假设我将为该大小添加另一个数量,2个数量然后它会自动将数量更新为3.再次,假设我将添加Nike LeBron' New Heights'大小7.现在的问题是,我怎样才能展示耐克勒布朗的新高度'如果我添加它,购物车内容中的大小为7(见下面的屏幕截图)?我已经尝试了下面的代码,但它只能看到Nike LeBron 15' New Heights'大小6。

截图:

https://ibb.co/e5k9KS

我尝试了什么:

//Add to Cart button
<button id="add_to_cart<?=$row['product_id']?>" onclick="add_to_cart('<?=$row['product_id']?>', 'Add')" class="add_to_cart">
    <i class="fa fa-shopping-cart"></i> Add to Cart
</button>

//JQuery/Ajax
function add_to_cart(product_id, product_size, status) {
var quantity = $('#quantity'+product_id).val();
var button = (status === 'Delete') ? $('#add_to_cart'+product_id).html('<i class="fa fa-shopping-cart"></i> Add to cart') : $('#add_to_cart'+product_id).html('<i class="fa fa-check-square-o"></i> Added');
button,setTimeout(function(){
    var data = { action : 'Add To Cart', product_id : product_id, status : status, quantity : quantity, product_size : product_size };
    $.ajax({
        type: 'POST',
        url: '../pages/class.php',
        data: data,
        cache: false,
        dataType: 'json',
        success: function(data) {
            data.success == true ? successful(data.success,data.bgcolor,data.color,data.message) : successful(data.success,data.bgcolor,data.color,data.message);
        }
    });
    $('#add_to_cart'+product_id).html('<i class="fa fa-shopping-cart"></i> Add to Cart');
    show_cart_count();
    show_cart_contents();
    show_cart_content_in_cart_page();
    show_cart_content_in_checkout_page();
    show_cart_total();
},300);

}

//Here's the class.php
if(isset($_POST['action']) && !empty($_POST['action'])) {
    $action = $_POST['action'];
    switch($action) {
       case 'Add To Cart':
          $product_id = $_POST['product_id'];
          $product_size = $_POST['product_size'];
          $quantity = $_POST['quantity'];
          $status = $_POST['status'];
          add_to_cart($product_id, $product_size, $quantity, $status);
       break;
    }
}

//add_to_cart function
function add_to_cart($product_id, $product_size, $quantity, $status) {
        GLOBAL $db_conn;
        $search_query = "SELECT p.*, pe.*, pct.* FROM tblproduct p JOIN (SELECT p.product_id, MIN(pe.product_extension_id) AS product_extension_id FROM tblproduct p LEFT JOIN tblproduct_extension pe ON pe.product_id = p.product_id WHERE p.product_id='$product_id' GROUP BY product_id) product_unique LEFT JOIN tblproduct_extension pe ON pe.product_extension_id = product_unique.product_extension_id LEFT JOIN tblproduct_carousel pct ON pct.product_id = p.product_id WHERE p.product_id = product_unique.product_id";
        $query = mysqli_query($db_conn, $search_query);
        $row = mysqli_fetch_array($query);

        if(isset($product_id)) {
              switch ($status) {
                 case 'Add':
                          if(isset($_SESSION['item_cart'][$product_id]['product_id']) == $product_id &&  isset($quantity)) {
                                $_SESSION['item_cart'][$product_id]['product_qty'] += $quantity;
                                $qty = ($quantity == 1)  ?  'quantity' : 'quantities';
                                notify([true,'#222','#fff',$row['product_name']. ' ' .'has been readded with '.$quantity.' '.$qty. '.']);
                          } else {
                                $count = (!isset($_SESSION['item_cart'])) ? 0 : count($_SESSION['item_cart']);
                                $add_quantity = (isset($quantity)) ? $quantity : 1;
                                $_SESSION['item_cart'][$product_id][$product_size] = array(
                                      "product_id"=>$product_id,
                                      "product_qty"=>$add_quantity, 
                                      "product_size"=>$product_size,
                                      "product_price"=>$row['product_price'],
                                      "product_name"=>$row['product_name'],
                                      "product_segment"=>$row['segment_id'],  
                                      "product_image"=>$row['product_images'],
                                      "product_brand"=>$row['product_brand'],
                                      "product_stocks"=>$row['product_stocks'],
                                      "product_category"=>$row['category_name']
                                );
                                notify([true,'#222','#fff',$row['product_name']. ' ' .'has been added.']);
                          }
                    break;
              }
        }
  }

0 个答案:

没有答案