public function testSumCart() { empty_cart(); $add_to_cart = array('content_id' => 1, 'qty' => 3, 'price' => 300); $cart_add = update_cart($add_to_cart); $cart_items = get_cart(); $sum = cart_sum(); $this->assertEquals($sum, 900); $this->assertEquals(isset($cart_add['success']), true); $this->assertEquals(!empty($cart_items), true); }
public function login() { //echo $_SESSION["abc"]; if (IS_POST) { $result["status"] = 0; $result["msg"] = "登录成功。"; $password = md5($_POST['password']); $db = M('member'); // $data['mobile'] = $_POST['username']; // $user = $db->where($data)->find(); $m = D('P/Member'); $user = $m->getByMobile($_POST['username']); if (!$user || $user['password'] != $password) { $result["msg"] = '用户名或密码不正确'; } else { $data = array('uid' => $user['uid'], 'login' => array('exp', '`login` + 1'), 'login_time' => date('y-m-d-H-i-s'), 'login_ip' => get_client_ip()); $db->save($data); // 将临时购物车的记录替换成真的 $cdb = M('cart'); $_uid = get_temp_uid(); // 清空之前的商品 $cmap['uid'] = $_uid; $cdb->where($cmap)->delete(); empty_cart(); $cartCount = $cdb->where(array('uid' => $user['uid']))->count(); count_cart($cartCount); // $sql = 'update `yyg_cart` SET `flag` = 1 ,`uid` = ' . $data['uid'] .' WHERE `uid` = ' . $_uid; // $row = M()->execute($sql); session("_uid", $user['uid']); session('wxUserinfo', $user); $url = decode(I('post.redirect')); $result["status"] = 1; session('loginstatus', 1); //$this->success('登录成功',U('Person/me', '','')); } $this->ajaxReturn($result); } else { layout(false); $this->assign('title', '壹圆购物'); $this->display(); } }
/** * Add items, delete items, or delete whole cart */ function manage_cart_items() { // make sure we have our session if (!isset($_SESSION['cartItems']) || !is_array($_SESSION['cartItems'])) { $_SESSION['cartItems'] = array(); } // DELETE CART ITEM if (isset($_GET['delete-item']) && is_numeric($_GET['delete-item'])) { $cartItems = $_SESSION['cartItems']; if (($key = array_search($_GET['delete-item'], $cartItems)) !== false) { unset($cartItems[$key]); } $_SESSION['cartItems'] = $cartItems; refresh_and_clear_querystring(); // DELETE CART } elseif (isset($_GET['delete-cart'])) { empty_cart(); refresh_and_clear_querystring(); // ADD TO CART } elseif (isset($_GET['item']) && is_numeric($_GET['item'])) { $newItem = $_GET['item']; if (!in_array($newItem, $_SESSION['cartItems'])) { array_push($_SESSION['cartItems'], $newItem); } else { refresh_and_clear_querystring(); } } }
} catch (Exception $e) { $error = $e->getMessage(); } } ?> <?php if (isset($error)) { ?> <h2>Error</h2> <p class="error">Oops, sorry. (The developers might understand this message: <code><?php echo $error; ?> </code>)</p> <?php } else { ?> <?php include 'inc_invoice.php'; empty_cart(); ?> <h2 class="thanks">Thank you for your purchase!</h2> <?php } ?> <?php require 'inc_footer.php';
echo '<div class ="alert alert-success alert-dismissable">'; echo '<center>Purchased ' . $product['productname'] . ' for $' . $cost . ' ($' . $product['price'] . ' x ' . $quantity . ')</center>'; echo '</div>'; echo '<div class="clear"> </div>'; //No quantity left, so do not allow for a purchase } else { echo "\n<div class ='alert alert-danger alert-dismissable'>"; echo "\n\t<center>Could not purchase" . $quantity . " " . $product['productname'] . ", only " . $product['quantity'] . " available</center>"; echo "\n</div>"; exit; } unset($_SESSION['cart'][$product_id]); } } echo '<hr>'; empty_cart($num_purchased, $tournament_purchased); } else { echo "\n<div class ='alert alert-danger alert-dismissable'>"; echo "\n\t<center>Error: Can't complete purchase, there are no items in shopping cart!</center>"; echo "\n</div>"; } echo '<center><a class="center" href="../Store/">Continue Shopping</a></center>'; function empty_cart($num_purchased, $tournament_purchased) { // This empties the shopping cart upon completing the transaction. unset($_SESSION['cart']); //if cart was purchased then display success message if ($num_purchased > 0) { echo "\n<div class ='alert alert-success alert-dismissable'>"; echo "\n\t<center>Success! Please Print this page for your records.</center>"; echo "\n</div>";
/** * 创建预支付订单 */ public function createPrePay() { if (is_login()) { $adb = M('account'); $cdb = D('cart'); $agdb = M('AccountGoods'); $result['status'] = -1; $adb->startTrans(); $uid = get_temp_uid(); $payid = \Org\Util\String::keyGen(); $accountData = array('payid' => $payid, 'uid' => $uid, 'type' => -1, 'status' => 0); if ($adb->add($accountData) !== FALSE) { $map['uid'] = $uid; $list = $cdb->where($map)->select(); $result['status'] = 0; // 复制购物车 foreach ($list as $cart) { $cart['payid'] = $payid; if ($agdb->add($cart) === FALSE) { $result['status'] = -1; break; } } // 清空购物车 if ($result['status'] == 0 && $cdb->where($map)->delete() === FALSE) { $result['status'] = -1; // 失败 $result['message'] = $cdb->getLastSql(); } } if ($result['status'] == 0) { $adb->commit(); $result['rst'] = $payid; empty_cart(); } else { $adb->rollback(); } } else { // 未登录 $result['status'] = -2; } $this->ajaxReturn($result, 'JSON'); }