简体   繁体   English

使用会话增加购物车项目数量

[英]Increment Shopping cart item quantity using session

I know that my question could be very similar to anothers in Stackoverflow. 我知道我的问题可能与Stackoverflow中的另一个问题非常相似。 I have found some similar questions but actually I couldn't get the right solution for my problem; 我发现了一些类似的问题,但实际上我无法为我的问题找到正确的解决方案。

I am writing shopping cart using Sessions and ajax-json. 我正在使用Sessions和ajax-json编写购物车。 My products structure is a bit complicated. 我的产品结构有点复杂。 It has name, size, type, colour and a specific price for each type and size. 它具有名称,大小,类型,颜色以及每种类型和大小的特定价格。 The main point is that I can't increment the item quantity if the name, size, type, color and price are the same, if I'm adding the same product. 要点是,如果我添加相同的产品,则名称,尺寸,类型,颜色和价格相同时,我将无法增加商品数量。 Here is my code. 这是我的代码。 I think I am writing right, but I can't understand what is the problem. 我想我写的没错,但是我不明白问题出在什么地方。 Actually it increments the item quantity, but just one time, and when I am checking the array, it's item quantity has not been incremented. 实际上,它增加了项目数量,但是只有一次,当我检查数组时,它的项目数量尚未增加。

$data = json_decode($_POST['jsonData'], true);
    $pr_type = $data['pr_type'];
    $pr_size = $data['pr_size'];
    $pr_link = $data['pr_link'];
    $pr_color = $data['pr_color'];
    $pr_price = $data['pr_price'];

    $products_s = $this->getSession()->get('prof_cart');
    $product = array();

    if (empty($products_s)) {
        $products_s = array();
    } else {
        $products_s = $products_s;
    }

    $products = Model::factory('Client_Product')->getProductById($pr_link);
    $type = Model::factory("Client_Product")->getProductTypeByLink($pr_type);
    $size = Model::factory("Client_Product")->getProductSizeById($pr_size);
    if ($pr_type != 'undefined') {
        $product['type'] = $type[0]['title'];
    } else {
        $product['type'] = "";
    }
    $isCreate = true;

    foreach ($products_s as $id) {
        if ($id['price'] == $pr_price &&
                $id['title'] == $products[0]['title'] &&
                $id['size'] == $size[0]['size'] &&
                $id['type'] == $type[0]['title']) {
            $id['quant']++;
            $isCreate = false;
        }
    }

    if ($isCreate) {
        $product['quant'] = 1;
        $product['size'] = $size[0]['size'];
        $product['title'] = $products[0]['title'];
        $product['price'] = $pr_price;            
        $product['color'] = $pr_color;
        array_push($products_s, $product);
    }

    $sum = 0;
    foreach ($products_s as $id) {
        $sum += $id['price'] * $id['quant'];
    }

    //echo $sum;

    echo "<pre>";
   var_dump($products_s);
   echo "</pre>";

    $this->getSession()->set('prof_cart', $products_s);

    $this->getSession()->set('prof_sum', $sum);

You need to increase quantity in your main product array like below. 您需要增加主要产品阵列中的数量,如下所示。

foreach ($products_s as $key => $id) {
    if ($id['price'] == $pr_price &&
            $id['title'] == $products[0]['title'] &&
            $id['size'] == $size[0]['size'] &&
            $id['type'] == $type[0]['title']) {
        $products_s[$key]['quant']++;
        $isCreate = false;
    }
}

try this : 尝试这个 :

    foreach ($products_s as $id) {
    if ($id['price'] == $pr_price &&
            $id['title'] == $products[0]['title'] &&
            $id['size'] == $size[0]['size'] &&
            $id['type'] == $type[0]['title']) {
        $id['quant'] = $id['quant']++;
        $isCreate = false;
    }
}

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

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