php - Increment Shopping cart item quantity using session -


i know question similar anothers in stackoverflow. have found similar questions couldn't right solution problem;

i writing shopping cart using sessions , ajax-json. products structure bit complicated. has name, size, type, colour , specific price each type , size. main point can't increment item quantity if name, size, type, color , price same, if i'm adding same product. here code. think writing right, can't understand problem. increments item quantity, 1 time, , when checking 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 $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 $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 increase quantity in main product array below.

foreach ($products_s $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;     } } 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -