Exemplo n.º 1
0
 /**
  * update cart更新购物车物品数量+-
  */
 public function ucAction()
 {
     if (ComTool::isAjax()) {
         $type = $this->post('type', 'inc');
         $productId = intval($this->post('proid', 0));
         if (!$productId) {
             ComTool::ajax(100001, '服务器忙,请刷新重试');
         }
         $product = GoodsData::getById($productId);
         if (!$product) {
             ComTool::ajax(100001, '服务器忙,请刷新重试');
         }
         $curCategory = $product['category_id'];
         $productInCart = $_SESSION['cart'][$curCategory][$productId];
         $productQuantity = intval($productInCart['quantity']);
         switch ($type) {
             case 'inc':
                 //increment
                 $productInCart['quantity'] = $productQuantity + 1;
                 $_SESSION['cart'][$curCategory][$productId] = $productInCart;
                 break;
             case 'dec':
                 //decrement
                 $productInCart['quantity'] = $productQuantity - 1;
                 if ($productInCart['quantity'] <= 0) {
                     $_SESSION['cart'][$curCategory][$productId] = array();
                     unset($_SESSION['cart'][$curCategory][$productId]);
                 } else {
                     $_SESSION['cart'][$curCategory][$productId] = $productInCart;
                 }
                 break;
             case 'rm':
                 //delete
                 $_SESSION['cart'][$curCategory][$productId] = array();
                 unset($_SESSION['cart'][$curCategory][$productId]);
                 break;
         }
         ComTool::ajax(100000, 'ok');
     }
 }