dongluo3331 2013-05-14 16:16
浏览 11
已采纳

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

I know that my question could be very similar to anothers in 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. 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);
  • 写回答

2条回答 默认 最新

  • douzhi4056 2013-05-14 16:17
    关注

    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;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?