|
|
|
ระบบตะกร้าสินค้า แบบ session ลบทีละ 1 รายการแล้วมีปัญหาครับ |
|
|
|
|
|
|
|
ใช้ รหัสสินค้าใส่ไปแทน array 0 1 พวกนั้นครับ เวลา unset ออกก็ อิง รหัสไป
|
|
|
|
|
Date :
2016-01-19 23:38:09 |
By :
progamer2000 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//ajax delete cart item
// @index = index of array
// @index + 1 = total product
function delete() {
$id = $_POST['id']; //product id
$index = array_search($id, $_SESSION['id']); //search id in session
if (strval($index) !== "") { //found
$_SESSION['index'] = intval($_SESSION['index']) - 1; //total product - 1
unset($_SESSION['id'][$index]); //delete id
unset($_SESSION['name'][$index]); //delete name
unset($_SESSION['price'][$index]); //delete price
unset($_SESSION['qty'][$index]); //delete qty
}
show(); //call display function for refresh cart
}
เผื่องงเอา function add ไปด้วย
//ajax add cart item
// @index = index of array
// @index + 1 = total product
function add() {
$id = $_POST['id'];
$name = $_POST['name'];
$price = $_POST['price'];
if (!isset($_SESSION['index'])) { // if first item
$_SESSION['index'] = 0;
$_SESSION['id'][0] = $id;
$_SESSION['name'][0] = $name;
$_SESSION['price'][0] = $price;
$_SESSION['qty'][0] = 1;
} else {
//search id
$index = array_search($id, $_SESSION['id']); //search id in session
//if found (increase item qty)
if (strval($index) !== "") {
$_SESSION['qty'][$index] = intval($_SESSION['qty'][$index]) + 1; //qty +1
} else {
//not found (new item)
$_SESSION['index'] = intval($_SESSION['index']) + 1;
$new_index = $_SESSION['index'];
$_SESSION['id'][$new_index] = $id;
$_SESSION['name'][$new_index] = $name;
$_SESSION['price'][$new_index] = $price;
$_SESSION['qty'][$new_index] = 1;
}
}
show();//call display function for refresh cart
}
|
|
|
|
|
Date :
2016-01-20 00:25:31 |
By :
noMerzy |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
อะให้ เอาไปปรับเองนะครับ
Code (PHP)
function AddToCart(){
$lang = $_SESSION['LANG'];
$code = $_GET['code'];
$qty = $_GET['qty'];
if(isset($_SESSION[CART]['ORDER'][$code])){
$_SESSION[CART]['ORDER'][$code]['quantity'] = $_SESSION[CART]['ORDER'][$code]['quantity'] + $qty;
$_SESSION[CART]['ORDER'][$code]['amount'] = $_SESSION[CART]['ORDER'][$code]['quantity'] * $_SESSION[CART]['ORDER'][$code]['net_total'];
}else{
$obj = new clsShopping();
$product = $obj->LoadProductDetail($code);
$product['quantity'] = $qty;
$product['pic'] = Thumbnail($product['filepic'], 47);
$product['name'] = $product["name_$lang"];
$product['unitprice'] = $product["price"];//check package
$product['discount_total'] = ($product['discount'] * $product["price"] / 100);//check package
$product['net_total'] = $product["price"] - $product['discount_total'];//check package
$_SESSION[CART]['ORDER'][$code] = $product;
}
LoadOrder();
}
function LoadOrder(){
$obj = new clsShopping();
$config = $obj->LoadConfig();
$result['sum']['total'] = 0;
$result['sum']['discount_total'] = 0;
$result['sum']['transfer_total'] = 0;
$result['sum']['net_total'] = 0;
$result['cart'] = array();
if(!empty($_SESSION[CART]['ORDER'])){
foreach($_SESSION[CART]['ORDER'] as $i => $order){
$order['amount'] = $order['unitprice'] * $order['quantity'];
$result['cart'][] = $order;
$result['sum']['discount_total'] += $order['discount_total'] * $order['quantity'];
$result['sum']['total'] += $order['unitprice'] * $order['quantity'];
}
}
$result['sum']['net_total'] = $result['sum']['total'] - $result['sum']['discount_total'];
if($config['shipping_min'] > $result['sum']['net_total']){
$result['sum']['transfer_total'] = $config['shipping_fee'];
$result['sum']['net_total'] += $config['shipping_fee'];
}
$result['sum']['cnt'] = count($_SESSION[CART]['ORDER']);
$result['sum']['net_total'] = ceil($result['sum']['net_total']);
$_SESSION[CART]['SUM'] = $result['sum'];
echo json_encode($result);
}
ระบบมันจะประมาณนี้
|
ประวัติการแก้ไข 2016-01-20 02:03:29
|
|
|
|
Date :
2016-01-20 01:59:59 |
By :
progamer2000 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ตัว array มันจะได้มาประมาณนี้ ที่วงกลมดำ คือรหัสสินค้า
|
|
|
|
|
Date :
2016-01-20 02:06:44 |
By :
progamer2000 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 00
|