【问题标题】:Increment Shopping cart item quantity using session使用会话增加购物车项目数量
【发布时间】:2013-05-09 00:32:30
【问题描述】:

我知道我的问题可能与 Stackoverflow 中的其他问题非常相似。我发现了一些类似的问题,但实际上我无法为我的问题找到正确的解决方案;

我正在使用 Sessions 和 ajax-json 编写购物车。 我的产品 结构有点复杂。它有名称、大小、类型、颜色和 每种类型和尺寸的具体价格。重点是我做不到 如果名称、尺寸、类型、颜色和价格不符合要求,则增加项目数量 同样,如果我添加相同的产品。这是我的代码。我想我 我写得对,但我不明白是什么问题。 实际上它增加了项目数量,但只是一次,当 我正在检查数组,它的项目数量没有增加。

$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);

【问题讨论】:

  • 谢谢!!!!!!非常感谢,效果很好

标签: php ajax json


【解决方案1】:

您需要增加主要产品数组中的数量,如下所示。

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;
    }
}

【讨论】:

    【解决方案2】:

    试试这个:

        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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-09-17
      • 1970-01-01
      • 1970-01-01
      • 2015-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-14
      • 1970-01-01
      相关资源
      最近更新 更多