mirror of
https://github.com/btcpayserver/btcpayserver.git
synced 2024-11-20 02:28:31 +01:00
- fix cart item removal
- fix empty qty field - remove tip when total changes
This commit is contained in:
parent
f87339f9fa
commit
16873384a8
@ -78,7 +78,11 @@ Cart.prototype.getTotalProducts = function() {
|
|||||||
|
|
||||||
// Always calculate the total amount based on the cart content
|
// Always calculate the total amount based on the cart content
|
||||||
for (var key in this.content) {
|
for (var key in this.content) {
|
||||||
if (this.content.hasOwnProperty(key) && typeof this.content[key] != 'undefined') {
|
if (
|
||||||
|
this.content.hasOwnProperty(key) &&
|
||||||
|
typeof this.content[key] != 'undefined' &&
|
||||||
|
!this.content[key].disabled
|
||||||
|
) {
|
||||||
var price = this.toCents(this.content[key].price.value);
|
var price = this.toCents(this.content[key].price.value);
|
||||||
amount += (this.content[key].count * price);
|
amount += (this.content[key].count * price);
|
||||||
}
|
}
|
||||||
@ -126,42 +130,80 @@ Cart.prototype.addItem = function(item) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Cart.prototype.incrementItem = function(id) {
|
Cart.prototype.incrementItem = function(id) {
|
||||||
// Increment the existing item count
|
var self = this;
|
||||||
|
this.items = 0; // Calculate total # of items from scratch just to make sure
|
||||||
|
|
||||||
this.content.filter(function(obj){
|
this.content.filter(function(obj){
|
||||||
|
// Increment the item count
|
||||||
if (obj.id === id){
|
if (obj.id === id){
|
||||||
obj.count++;
|
obj.count++;
|
||||||
|
delete(obj.disabled);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Increment the total # of items
|
||||||
|
self.items += obj.count;
|
||||||
|
});
|
||||||
|
|
||||||
|
this.updateAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Disable cart item so it doesn't count towards total amount
|
||||||
|
Cart.prototype.disableItem = function(id) {
|
||||||
|
var self = this;
|
||||||
|
|
||||||
|
this.content.filter(function(obj){
|
||||||
|
if (obj.id === id){
|
||||||
|
obj.disabled = true;
|
||||||
|
self.items -= obj.count;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
this.updateAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Enable cart item so it counts towards total amount
|
||||||
|
Cart.prototype.enableItem = function(id) {
|
||||||
|
var self = this;
|
||||||
|
|
||||||
|
this.content.filter(function(obj){
|
||||||
|
if (obj.id === id){
|
||||||
|
delete(obj.disabled);
|
||||||
|
self.items += obj.count;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
this.items++;
|
|
||||||
this.updateAll();
|
this.updateAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
Cart.prototype.decrementItem = function(id) {
|
Cart.prototype.decrementItem = function(id) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
this.items = 0; // Calculate total # of items from scratch just to make sure
|
||||||
|
|
||||||
// Decrement the existing item count
|
|
||||||
this.content.filter(function(obj, index, arr){
|
this.content.filter(function(obj, index, arr){
|
||||||
|
// Decrement the item count
|
||||||
if (obj.id === id)
|
if (obj.id === id)
|
||||||
{
|
{
|
||||||
obj.count--;
|
obj.count--;
|
||||||
|
delete(obj.disabled);
|
||||||
|
|
||||||
// It's the last item with the same ID, remove it
|
// It's the last item with the same ID, remove it
|
||||||
if (obj.count === 0) {
|
if (obj.count <= 0) {
|
||||||
self.removeItem(id, index, arr);
|
self.removeItem(id, index, arr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Decrement the total # of items
|
||||||
|
self.items += obj.count;
|
||||||
});
|
});
|
||||||
|
|
||||||
this.items--;
|
|
||||||
this.updateAll();
|
this.updateAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
Cart.prototype.removeItemAll = function(id) {
|
Cart.prototype.removeItemAll = function(id) {
|
||||||
var self = this;
|
var self = this;
|
||||||
|
|
||||||
// Remove by item
|
// Remove by item
|
||||||
if (id) {
|
if (typeof id != 'undefined') {
|
||||||
this.content.filter(function(obj, index, arr){
|
this.content.filter(function(obj, index, arr){
|
||||||
if (obj.id === id)
|
if (obj.id === id)
|
||||||
{
|
{
|
||||||
@ -174,8 +216,8 @@ Cart.prototype.removeItemAll = function(id) {
|
|||||||
});
|
});
|
||||||
} else { // Remove all
|
} else { // Remove all
|
||||||
this.$list.find('tbody').empty();
|
this.$list.find('tbody').empty();
|
||||||
self.content = [];
|
this.content = [];
|
||||||
self.items = 0;
|
this.items = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
this.emptyCartToggle();
|
this.emptyCartToggle();
|
||||||
@ -249,6 +291,23 @@ Cart.prototype.updateAmount = function() {
|
|||||||
$('#js-cart-amount').val(this.getTotal(true));
|
$('#js-cart-amount').val(this.getTotal(true));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Cart.prototype.resetDiscount = function() {
|
||||||
|
this.setDiscount(0);
|
||||||
|
this.updateDiscount(0);
|
||||||
|
$('.js-cart-discount').val('');
|
||||||
|
}
|
||||||
|
|
||||||
|
Cart.prototype.resetTip = function() {
|
||||||
|
this.setTip(0);
|
||||||
|
this.updateTip(0);
|
||||||
|
$('.js-cart-tip').val('');
|
||||||
|
}
|
||||||
|
|
||||||
|
Cart.prototype.resetCustomAmount = function() {
|
||||||
|
this.setCustomAmount(0);
|
||||||
|
$('.js-cart-custom-amount').val('');
|
||||||
|
}
|
||||||
|
|
||||||
// Escape html characters
|
// Escape html characters
|
||||||
Cart.prototype.escape = function(input) {
|
Cart.prototype.escape = function(input) {
|
||||||
return ('' + input) /* Forces the conversion to string. */
|
return ('' + input) /* Forces the conversion to string. */
|
||||||
@ -335,32 +394,51 @@ Cart.prototype.listItems = function() {
|
|||||||
$('.js-cart-item-count').off().on('input', function(event){
|
$('.js-cart-item-count').off().on('input', function(event){
|
||||||
var _this = this,
|
var _this = this,
|
||||||
id = $(this).closest('tr').data('id'),
|
id = $(this).closest('tr').data('id'),
|
||||||
count = parseInt($(this).val()),
|
qty = parseInt($(this).val()),
|
||||||
prevCount = parseInt($(this).data('prev')),
|
isQty = !isNaN(qty),
|
||||||
increased = count > prevCount;
|
prevQty = parseInt($(this).data('prev')),
|
||||||
|
qtyDiff = Math.abs(qty - prevQty),
|
||||||
|
qtyIncreased = qty > prevQty;
|
||||||
|
|
||||||
// User hasn't inputed any number so stop here
|
if (isQty) {
|
||||||
if (isNaN(count)) {
|
$(this).data('prev', qty);
|
||||||
return false;
|
self.resetTip();
|
||||||
|
} else {
|
||||||
|
// User hasn't inputed any quantity
|
||||||
|
qty = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
$(this).data('prev', count);
|
// Quantity was increased
|
||||||
|
if (qtyIncreased) {
|
||||||
|
var item = self.content.filter(function(obj){
|
||||||
|
return obj.id === id;
|
||||||
|
});
|
||||||
|
|
||||||
var item = self.content.filter(function(obj){
|
// Quantity may have been increased by more than one
|
||||||
return obj.id === id
|
for (var i = 0; i < qtyDiff; i++) {
|
||||||
});
|
|
||||||
|
|
||||||
// Must be in the loop because user may change the count manually by more than 1
|
|
||||||
for (var i = 0; i < Math.abs(count - prevCount); i++) {
|
|
||||||
if (increased) {
|
|
||||||
self.addItem({
|
self.addItem({
|
||||||
id: id,
|
id: id,
|
||||||
title: item.title,
|
title: item.title,
|
||||||
price: item.price,
|
price: item.price,
|
||||||
image: typeof item.image != 'underfined' ? item.image : null
|
image: typeof item.image != 'underfined' ? item.image : null
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
} else if (!qtyIncreased) { // Quantity decreased
|
||||||
|
// No quantity set (e.g. empty string)
|
||||||
|
if (!isQty) {
|
||||||
|
// Disable the item so it doesn't count towards total amount
|
||||||
|
self.disableItem(id);
|
||||||
} else {
|
} else {
|
||||||
self.decrementItem(id);
|
// Quantity vas decreased
|
||||||
|
if (qtyDiff > 0) {
|
||||||
|
// Quantity may have been decreased by more than one
|
||||||
|
for (var i = 0; i < qtyDiff; i++) {
|
||||||
|
self.decrementItem(id);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Quantity hasn't changed, enable the item so it counts towards the total amount
|
||||||
|
self.enableItem(id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -369,6 +447,7 @@ Cart.prototype.listItems = function() {
|
|||||||
$('.js-cart-item-remove').off().on('click', function(event){
|
$('.js-cart-item-remove').off().on('click', function(event){
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
|
|
||||||
|
self.resetTip();
|
||||||
self.removeItemAll($(this).closest('tr').data('id'));
|
self.removeItemAll($(this).closest('tr').data('id'));
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -376,9 +455,12 @@ Cart.prototype.listItems = function() {
|
|||||||
$('.js-cart-item-plus').off().on('click', function(event){
|
$('.js-cart-item-plus').off().on('click', function(event){
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
|
|
||||||
var $val = $(this).parents('.input-group').find('.js-cart-item-count');
|
var $val = $(this).parents('.input-group').find('.js-cart-item-count'),
|
||||||
|
val = parseInt($val.val() || $val.data('prev')) + 1;
|
||||||
|
|
||||||
$val.val(parseInt($val.val()) + 1);
|
$val.val(val);
|
||||||
|
$val.data('prev', val);
|
||||||
|
self.resetTip();
|
||||||
self.incrementItem($(this).closest('tr').data('id'));
|
self.incrementItem($(this).closest('tr').data('id'));
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -388,12 +470,15 @@ Cart.prototype.listItems = function() {
|
|||||||
|
|
||||||
var $val = $(this).parents('.input-group').find('.js-cart-item-count'),
|
var $val = $(this).parents('.input-group').find('.js-cart-item-count'),
|
||||||
id = $(this).closest('tr').data('id'),
|
id = $(this).closest('tr').data('id'),
|
||||||
val = parseInt($val.val());
|
val = parseInt($val.val() || $val.data('prev')) - 1;
|
||||||
|
|
||||||
if (val === 1) {
|
self.resetTip();
|
||||||
|
|
||||||
|
if (val === 0) {
|
||||||
self.removeItemAll(id);
|
self.removeItemAll(id);
|
||||||
} else {
|
} else {
|
||||||
$val.val(val - 1);
|
$val.val(val);
|
||||||
|
$val.data('prev', val);
|
||||||
self.decrementItem(id);
|
self.decrementItem(id);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -500,6 +585,9 @@ Cart.prototype.loadLocalStorage = function() {
|
|||||||
for (var key in this.content) {
|
for (var key in this.content) {
|
||||||
if (this.content.hasOwnProperty(key) && typeof this.content[key] != 'undefined' && this.content[key] != null) {
|
if (this.content.hasOwnProperty(key) && typeof this.content[key] != 'undefined' && this.content[key] != null) {
|
||||||
this.items += this.content[key].count;
|
this.items += this.content[key].count;
|
||||||
|
|
||||||
|
// Delete the disabled flag if any
|
||||||
|
delete(this.content[key].disabled);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -509,26 +597,22 @@ Cart.prototype.loadLocalStorage = function() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Cart.prototype.destroy = function(keepAmount) {
|
Cart.prototype.destroy = function(keepAmount) {
|
||||||
this.setTip(0);
|
this.resetDiscount();
|
||||||
this.setDiscount(0);
|
this.resetTip();
|
||||||
this.setCustomAmount(0);
|
this.resetCustomAmount();
|
||||||
|
|
||||||
// When form is sent
|
// When form is sent
|
||||||
if (keepAmount) {
|
if (keepAmount) {
|
||||||
this.content = [];
|
this.content = [];
|
||||||
this.items = 0;
|
this.items = 0;
|
||||||
} else {
|
} else {
|
||||||
this.updateDiscount(0);
|
|
||||||
this.updateTip(0);
|
|
||||||
|
|
||||||
this.removeItemAll();
|
this.removeItemAll();
|
||||||
$('.js-cart-discount').val('');
|
|
||||||
$('.js-cart-tip').val('');
|
|
||||||
$('.js-cart-custom-amount').val('');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
localStorage.removeItem(this.getStorageKey('cart'));
|
localStorage.removeItem(this.getStorageKey('cart'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* jQuery helpers
|
* jQuery helpers
|
||||||
*/
|
*/
|
||||||
@ -542,12 +626,14 @@ $.fn.inputAmount = function(obj, type) {
|
|||||||
obj.updateDiscount();
|
obj.updateDiscount();
|
||||||
obj.updateSummaryProducts();
|
obj.updateSummaryProducts();
|
||||||
obj.updateTotal();
|
obj.updateTotal();
|
||||||
|
obj.resetTip();
|
||||||
break;
|
break;
|
||||||
case 'discount':
|
case 'discount':
|
||||||
obj.setDiscount(val);
|
obj.setDiscount(val);
|
||||||
obj.updateDiscount();
|
obj.updateDiscount();
|
||||||
obj.updateSummaryProducts();
|
obj.updateSummaryProducts();
|
||||||
obj.updateTotal();
|
obj.updateTotal();
|
||||||
|
obj.resetTip();
|
||||||
break;
|
break;
|
||||||
case 'tip':
|
case 'tip':
|
||||||
obj.setTip(val);
|
obj.setTip(val);
|
||||||
@ -567,26 +653,16 @@ $.fn.removeAmount = function(obj, type) {
|
|||||||
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case 'customAmount':
|
case 'customAmount':
|
||||||
obj.setCustomAmount(0);
|
obj.resetCustomAmount();
|
||||||
obj.updateSummaryProducts();
|
obj.updateSummaryProducts();
|
||||||
$('.js-cart-custom-amount').val('');
|
|
||||||
break;
|
break;
|
||||||
case 'discount':
|
case 'discount':
|
||||||
obj.setDiscount(0);
|
obj.resetDiscount();
|
||||||
obj.updateDiscount(0);
|
|
||||||
obj.updateSummaryProducts();
|
obj.updateSummaryProducts();
|
||||||
$('.js-cart-discount').val('');
|
|
||||||
break;
|
|
||||||
case 'tip':
|
|
||||||
obj.setTip(0);
|
|
||||||
obj.updateTip(0);
|
|
||||||
$('.js-cart-tip').val('');
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
obj.resetTip();
|
||||||
obj.updateTotal();
|
obj.updateTotal();
|
||||||
obj.updateSummaryTotal();
|
obj.updateSummaryTotal();
|
||||||
obj.emptyCartToggle();
|
obj.emptyCartToggle();
|
||||||
|
Loading…
Reference in New Issue
Block a user