简体   繁体   English

购物车会话按尺寸保存产品

[英]cart session to save products by size

I have this code to save product in cart.我有这个代码可以将产品保存在购物车中。 I need to save product and size - quantity x price.我需要保存产品和尺寸 - 数量 x 价格。 At this momment is storing product name, size, quantity and price, but it display the same row.此时正在存储产品名称、尺寸、数量和价格,但它显示同一行。 If I add 'product1' size M quanity 1, and if I add 'product1' size S quanitity 1, in the cart I get 'product1' size S (the last size added) quantity 1 (but in the tquan I get 2 products).如果我添加“product1”尺寸 M 数量 1,如果我添加“product1”尺寸 S 数量 1,在购物车中我会得到“product1”尺寸 S(添加的最后一个尺寸)数量 1(但在 tquan 中我得到 2 个产品)。 So how can I make to have for each size of a product, another row.那么我怎样才能让每个尺寸的产品都有另一排。

$_SESSION['CART']['order_token']    = $form_token;
        $arrCart['id']              = $pID;
        $arrCart['quantity']    = $_SESSION['CART']['PRODUCT'][$pID]['size']['quantity'] + $_POST['quantity'];
        $chkQuan = mysqli_fetch_assoc(mysqli_query($link, "SELECT quantity FROM table WHERE id = '".$pID."'"));

            $total =$_SESSION['CART']['tquan']+$_POST['quantity'];
            $arrCart['pret']                = $_POST['product_price'];
            $arrCart['size']                = $_POST['size'];
            $_SESSION['CART']['tquan']      = $total;
            $_SESSION['CART']['PRODUCT'][$pID] = $arrCart;

And this part is for displaying cart这部分用于展示购物车

        if(isset($_SESSION['CART']) && $_SESSION['CART']!='') 
        { 

            $endsumm = '';
            $product_total_price = '';
            foreach($_SESSION['CART']['PRODUCT'] as $pID=>$pArr)
            {
                $prod = mysqli_fetch_assoc(mysqli_query($link, "SELECT * FROM table WHERE id='".$pID."'"));
                $product_total_price = $pArr['quantity']*$pArr['pret'];
                $endsumm += $pArr['quantity']*$pArr['pret'];

Before inserting product in the cart, you should check that product is already in the cart or not.在将产品放入购物车之前,您应该检查产品是否已经在购物车中。

  • Here in your case you should check for the productid as well as size is available in the cart or not.在您的情况下,您应该检查产品ID 以及购物车中是否有尺寸
    If any one or both condition is not true then insert that product into the cart.如果任何一个或两个条件不成立,则将该产品插入购物车。
    Otherwise update your cart.否则更新您的购物车。

                $total =$_SESSION['CART']['tquan']+$_POST['quantity'];
       $arrCart['pret']= $_POST['product_price'];
       $arrCart['size']= $_POST['size'];
    $_SESSION['CART']['tquan'] = $total;
    $_SESSION['CART']['PRODUCT'][$pID] = $arrCart

if(!in_array($pID, array_map(function($id){return $id[$pID];}, $_SESSION['CART']))){

          if(!in_array($_POST['size'], array_map(function($size){return $size['size'];}, $_SESSION['CART']))){

//Here insert the product into the cart as you were inserted in the question

     }


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM