Commit 8f19b08d authored by Malcolm Blaney's avatar Malcolm Blaney

Use localStorage for storing cart data.

parent 74d2b43b
......@@ -347,20 +347,6 @@ class Cart extends Base {
public function Update() {
// This is called when the version of the module is updated,
// to provide a way to update or modify tables etc..
$mysqli = connect_db();
$query = 'CREATE TABLE IF NOT EXISTS cart_method ('.
'user VARCHAR(50) NOT NULL,'.
'name ENUM("credit", "paypal") NOT NULL,'.
'available TINYINT(1),'.
'gateway ENUM("eway"),'.
'gateway_api_key TEXT,'.
'gateway_password VARCHAR(100),'.
'PRIMARY KEY(user, name)'.
') ENGINE=MyISAM';
if (!$mysqli->query($query)) {
$this->Log('Cart->Update: '.$mysqli->error);
}
$mysqli->close();
}
public function UpdateScript($path) {
......@@ -421,6 +407,9 @@ class Cart extends Base {
}
if ($item_names != "") $item_names .= ", ";
$item_names .= $item;
if ($quantity > 1) {
$item_names .= " (".$quantity.")";
}
$total_price += $price * $quantity;
$total_weight += $weight * $quantity;
}
......@@ -501,7 +490,7 @@ class Cart extends Base {
'price is displayed.</i><br>'.
'<label for="item-price-'.$count.'">Price: </label>'.
'<input id="item-price-'.$count.'" class="item-price" '.
'value="'.$item["price"].'" size="6" maxlength="11"> '.
'value="'.$item["price"].'" size="6" maxlength="11"> '.
$minimum_content.$download_content;
}
// If an item can be downloaded, display an "add to cart" button
......
......@@ -25,8 +25,9 @@
// for the JavaScript code in this page.
if(!this.dobrado.cart){dobrado.cart={};}
(function(){'use strict';var cart={};var settings=null;var editor=null;var itemIndex=0;var shippingIndex=0;var browserTarget="";$(function(){if($(".cart").length===0){return;}
checkDownloads();$(".cart-item .item-add").button().click(cartAdd);$(".cart-item .item-quantity").val("0");$(".cart-item .item-quantity").spinner({min:0,spin:cartQuantity,change:cartQuantity});$("#cart-checkout").button({disabled:true}).click(checkout);$("#customer-detail-submit").button().click(customerDetail);$(".cart-item .item-name a").click(cartItem);var width=450;$("#cart-checkout-dialog").dialog({autoOpen:false,modal:true,position:{my:"top",at:"top+50",of:"body"},title:"Checkout",width:width,height:500});width=720;$("#cart-editor").dialog({autoOpen:false,close:close,position:{my:"top",at:"top+50",of:"body"},title:"Cart Editor",width:width,height:500});$("#cart-item-dialog").dialog({autoOpen:false,position:{my:"top",at:"top+50",of:"body"},title:"Cart Item",width:width,height:500});$("#cart-edit-button").button({icons:{primary:"ui-icon-pencil"},text:false}).click(cartEditor);});function checkDownloads(){$(".cart-download-list li").each(function(i){var file=$(this).html();setTimeout(function(){location.href="/php/download.php?file="+file;},i*10000);});}
(function(){'use strict';var cart={};var settings=null;var editor=null;var itemIndex=0;var shippingIndex=0;var browserTarget="";$(function(){if(dobrado.localStorage()){if(localStorage.cart){cart=JSON.parse(localStorage.cart);cartMiniTotal();}}
if($(".cart").length===0){return;}
checkDownloads();$(".cart-item .item-add").button().click(cartAdd);$(".cart-item .item-quantity").val("0");$(".cart-item .item-quantity").spinner({min:0,spin:cartQuantity,change:cartQuantity});$("#cart-checkout").button({disabled:true}).click(checkout);$("#customer-detail-submit").button().click(customerDetail);$(".cart-item .item-name a").click(cartItem);var width=450;$("#cart-checkout-dialog").dialog({autoOpen:false,modal:true,position:{my:"top",at:"top+50",of:"body"},title:"Checkout",width:width,height:500});width=720;$("#cart-editor").dialog({autoOpen:false,close:close,position:{my:"top",at:"top+50",of:"body"},title:"Cart Editor",width:width,height:500});$("#cart-item-dialog").dialog({autoOpen:false,position:{my:"top",at:"top+50",of:"body"},title:"Cart Item",width:width,height:500});$("#cart-edit-button").button({icons:{primary:"ui-icon-pencil"},text:false}).click(cartEditor);$("#cart-total").html(cartTotal());$.each(cart,function(name,item){var id=item.id;if(id){var count=id.match(/([0-9]+)$/);if(item.selected&&count&&count.length===2){$("#"+id+" .item-add").button({label:"Remove from cart"});$("#item-quantity-"+count).spinner("value",item.quantity);}}});});function checkDownloads(){$(".cart-download-list li").each(function(i){var file=$(this).html();setTimeout(function(){location.href="/php/download.php?file="+file;},i*10000);});}
function cartPrice(item){var price=0;var checkMinimum=false;if(item.attr("id")){price=item.val();checkMinimum=true;}
else{price=item.html();}
var fields=price.match(/([0-9.]+)$/);if(fields&&fields.length===2){price=parseFloat(fields[1]);}
......@@ -39,7 +40,8 @@ else{$(this).siblings(".item-download").hide();}
if(cart[name]){if(cart[name].selected){cart[name].selected=false;$(this).button({label:"Add to cart"});}
else{cart[name].selected=true;$(this).button({label:"Remove from cart"});}
cart[name].price=price;}
else{cart[name]={selected:true,price:price,quantity:1};$(this).button({label:"Remove from cart"});}
else{cart[name]={selected:true,price:price,quantity:1,id:$(this).parents(".cart-item").attr("id")};$(this).button({label:"Remove from cart"});}
if(dobrado.localStorage()){localStorage.cart=JSON.stringify(cart);}
$("#cart-total").html(cartTotal());return false;}
function cartQuantity(event,ui){var item=$(this).parents(".cart-item");var name="";if(item.children(".item-name").children("a").length===0){name=item.children(".item-name").html();}
else{name=item.children(".item-name").children("a").html();}
......@@ -48,9 +50,11 @@ else{quantity=parseFloat($(this).val());if(quantity<0){quantity=0;$(this).val("0
if(cart[name]){if(quantity===0){cart[name].selected=false;}
else{cart[name].selected=true;}
cart[name].price=price;cart[name].quantity=quantity;}
else if(quantity!==0){cart[name]={selected:true,price:price,quantity:quantity};}
else if(quantity!==0){cart[name]={selected:true,price:price,quantity:quantity,id:item.attr("id")};}
if(dobrado.localStorage()){localStorage.cart=JSON.stringify(cart);}
$("#cart-total").html(cartTotal());}
function cartTotal(shipping){var total=0;var quantity=0;var table='<table class="cart-total-table">';$.each(cart,function(name,item){if(item.selected&&item.price!==0){total+=item.price*item.quantity;quantity+=item.quantity;table+='<tr><td>'+name+'</td><td>'+item.quantity+'</td><td>$'+(item.price*item.quantity).toFixed(2)+'</td></tr>';}});if(shipping){total+=shipping;table+='<tr><td>shipping</td><td></td><td>$'+
function cartMiniTotal(){var total=0;$.each(cart,function(name,item){if(item.selected&&item.price!==0){total+=item.price*item.quantity;}});$(".cart-total .number").html("$"+total.toFixed(2));}
function cartTotal(shipping){var total=0;var quantity=0;var table='<table class="cart-total-table">';$.each(cart,function(name,item){if(item.selected&&item.price!==0){total+=item.price*item.quantity;quantity+=item.quantity;table+='<tr><td>'+name+'</td><td>'+item.quantity+'</td><td>$'+(item.price*item.quantity).toFixed(2)+'</td></tr>';}});$(".cart-total .number").html("$"+total.toFixed(2));if(shipping){total+=shipping;table+='<tr><td>shipping</td><td></td><td>$'+
shipping.toFixed(2)+'</td></tr>';}
table+='<tr><td><b>Total</b></td><td>'+quantity+'</td><td>$'+
total.toFixed(2)+'</td></tr></table>';if(total===0){table="";$("#cart-checkout").button("option","disabled",true);}
......@@ -68,8 +72,8 @@ if($("#customer-detail-postcode").val()===""){$("#cart-checkout-info").html("Ple
if($("#customer-detail-city").val()===""){$("#cart-checkout-info").html("Please enter your city.");return false;}
if($("#customer-detail-state").val()===""){$("#cart-checkout-info").html("Please enter your state.");return false;}
if($("#customer-detail-country").val()===""){$("#cart-checkout-info").html("Please enter your country.");return false;}
$("#cart-checkout-info").html("");$.post("/php/request.php",{request:"cart",action:"checkout",cart:JSON.stringify(cart),first:$("#customer-detail-first").val(),last:$("#customer-detail-last").val(),email:$("#customer-detail-email").val(),address:$("#customer-detail-address").val(),city:$("#customer-detail-city").val(),state:$("#customer-detail-state").val(),country:$("#customer-detail-country").val(),postcode:$("#customer-detail-postcode").val(),method:$("#customer-detail-method").val(),url:location.href,token:dobrado.token},function(response){if(dobrado.checkResponseError(response,"cart shipping")){return;}
var shipping=JSON.parse(response);$("#cart-customer-details-form").hide();$("#cart-summary").html("You have selected the following items:<br>"+
$("#cart-checkout-info").html("Processing... please wait.");$.post("/php/request.php",{request:"cart",action:"checkout",cart:JSON.stringify(cart),first:$("#customer-detail-first").val(),last:$("#customer-detail-last").val(),email:$("#customer-detail-email").val(),address:$("#customer-detail-address").val(),city:$("#customer-detail-city").val(),state:$("#customer-detail-state").val(),country:$("#customer-detail-country").val(),postcode:$("#customer-detail-postcode").val(),method:$("#customer-detail-method").val(),url:location.href,token:dobrado.token},function(response){if(dobrado.checkResponseError(response,"cart shipping")){return;}
var shipping=JSON.parse(response);$("#cart-checkout-info").html("");$("#cart-customer-details-form").hide();$("#cart-summary").html("You have selected the following items:<br>"+
cartTotal(shipping.cost));$("#cart-checkout-message").html(shipping.content);$("#cart-payment").button();});return false;}
function close(){if(editor){editor.destroy();}}
function cartEditor(){dobrado.log("editor loading...","info");$.post("/php/request.php",{request:"cart",action:"list",url:location.href,token:dobrado.token},function(response){if(dobrado.checkResponseError(response,"cart edit")){return;}
......
......@@ -44,7 +44,13 @@ if (!this.dobrado.cart) {
var browserTarget = "";
$(function() {
// Don't run if the module isn't on the page.
// Some pages display the cart total without the cart module on the page.
if (dobrado.localStorage()) {
if (localStorage.cart) {
cart = JSON.parse(localStorage.cart);
cartMiniTotal();
}
}
if ($(".cart").length === 0) {
return;
}
......@@ -91,6 +97,19 @@ if (!this.dobrado.cart) {
icons: { primary: "ui-icon-pencil" },
text: false
}).click(cartEditor);
$("#cart-total").html(cartTotal());
// Also update quantities in spinners and labels for buttons.
$.each(cart, function(name, item) {
var id = item.id;
if (id) {
var count = id.match(/([0-9]+)$/);
if (item.selected && count && count.length === 2) {
$("#" + id + " .item-add").button({ label: "Remove from cart" });
$("#item-quantity-" + count).spinner("value", item.quantity);
}
}
});
});
function checkDownloads() {
......@@ -164,9 +183,13 @@ if (!this.dobrado.cart) {
}
else {
// Not already in the cart, so the user is selecting it.
cart[name] = { selected: true, price: price, quantity: 1 };
cart[name] = { selected: true, price: price, quantity: 1,
id: $(this).parents(".cart-item").attr("id") };
$(this).button({ label: "Remove from cart" });
}
if (dobrado.localStorage()) {
localStorage.cart = JSON.stringify(cart);
}
$("#cart-total").html(cartTotal());
return false;
}
......@@ -213,11 +236,25 @@ if (!this.dobrado.cart) {
}
else if (quantity !== 0) {
// Not already in the cart, so the user is selecting it.
cart[name] = { selected: true, price: price, quantity: quantity };
cart[name] = { selected: true, price: price, quantity: quantity,
id: item.attr("id") };
}
if (dobrado.localStorage()) {
localStorage.cart = JSON.stringify(cart);
}
$("#cart-total").html(cartTotal());
}
function cartMiniTotal() {
var total = 0;
$.each(cart, function(name, item) {
if (item.selected && item.price !== 0) {
total += item.price * item.quantity;
}
});
$(".cart-total .number").html("$" + total.toFixed(2));
}
function cartTotal(shipping) {
var total = 0;
var quantity = 0;
......@@ -230,6 +267,8 @@ if (!this.dobrado.cart) {
'</td><td>$' + (item.price * item.quantity).toFixed(2) + '</td></tr>';
}
});
$(".cart-total .number").html("$" + total.toFixed(2));
if (shipping) {
total += shipping;
table += '<tr><td>shipping</td><td></td><td>$' +
......@@ -306,7 +345,7 @@ if (!this.dobrado.cart) {
$("#cart-checkout-info").html("Please enter your country.");
return false;
}
$("#cart-checkout-info").html("");
$("#cart-checkout-info").html("Processing... please wait.");
$.post("/php/request.php",
{ request: "cart",
......@@ -329,6 +368,7 @@ if (!this.dobrado.cart) {
}
var shipping = JSON.parse(response);
$("#cart-checkout-info").html("");
$("#cart-customer-details-form").hide();
$("#cart-summary").html("You have selected the following items:<br>" +
cartTotal(shipping.cost));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment