Skip to content

Commit

Permalink
Small cart quantity fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Moffat authored and Mark Moffat committed May 21, 2018
1 parent abc5de8 commit c674f86
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 16 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules/
data/
config/settings.json
.vscode
.vscode
**.DS_Store
6 changes: 6 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,12 @@ app.use((req, res, next) => {
next();
});

// Ran on all routes
app.use((req, res, next) => {
res.setHeader('Cache-Control', 'no-cache, no-store');
next();
});

// setup the routes
app.use('/', index);
app.use('/', customer);
Expand Down
20 changes: 12 additions & 8 deletions public/javascripts/expressCart.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ $(document).ready(function (){
cartUpdate(qtyElement);
});

$(document).on('click', '.btn-delete-from-cart', function(e){
deleteFromCart($(e.target));
});

$(document).on('click', '.orderFilterByStatus', function(e){
e.preventDefault();
window.location = '/admin/orders/bystatus/' + $('#orderStatusFilter').val();
Expand Down Expand Up @@ -457,6 +461,10 @@ $(document).ready(function (){
$(document).on('click', '.product-add-to-cart', function(e){
var productOptions = getSelectedOptions();

if(parseInt($('#product_quantity').val()) < 0){
$('#product_quantity').val(0);
}

$.ajax({
method: 'POST',
url: '/product/addtocart',
Expand Down Expand Up @@ -682,12 +690,12 @@ function slugify(str){
}

function cartUpdate(element){
if($(element).val() < 1){
deleteFromCart($(element));
}else{
if($(element).val() > 0){
if($(element).val() !== ''){
updateCart();
}
}else{
$(element).val(1);
}
}

Expand All @@ -700,11 +708,7 @@ function updateCart(){
itemQuantity: $(this).val(),
productId: $(this).attr('data-id')
};
if($(this).val() < 0){
deleteFromCart($(this));
}else{
cartItems.push(item);
}
cartItems.push(item);
});

// update cart on server
Expand Down
2 changes: 1 addition & 1 deletion public/javascripts/expressCart.min.js

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,11 @@ router.post('/product/addtocart', (req, res, next) => {
const db = req.app.db;
let productQuantity = req.body.productQuantity ? parseInt(req.body.productQuantity) : 1;

// Don't allow negative quantity
if(productQuantity < 0){
productQuantity = 1;
}

// setup cart object if it doesn't exist
if(!req.session.cart){
req.session.cart = [];
Expand Down
3 changes: 3 additions & 0 deletions views/themes/Cloth/cart.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
</span>
</div>
</div>
<span class="pull-right">
<button class="btn btn-sm btn-danger btn-delete-from-cart" data-id="{{this.productId}}" type="button"><i class="fa fa-trash" data-id="{{this.productId}}" aria-hidden="true"></i></button>
</span>
</div>
</p>
</div>
Expand Down
8 changes: 5 additions & 3 deletions views/themes/Material/cart.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,19 @@
&nbsp;{{#each this.options}} {{#if @last}} {{this}} {{else}} {{this}} / {{/if}} {{/each}}
</p>
<p>

<div class="col s12 no-pad-left">
<span class="col s2">
<button class="btn waves-effect waves-light blue darken-3 btn-qty-minus" type="button">-</button>
</span>
<div class="input-field col s8">
<div class="input-field col s5">
<input type="number" class="cart-product-quantity text-center" data-id="{{this.productId}}" id="{{@key}}" maxlength="2" value="{{this.quantity}}">
</div>
<span class="col s2">
<span class="col s3">
<button class="btn waves-effect waves-light blue darken-3 btn-qty-add" type="button">+</button>
</span>
<span class="col s1">
<button class="btn waves-effect waves-light red darken-3 btn-delete-from-cart" data-id="{{this.productId}}" type="button"><i class="fa fa-trash" data-id="{{this.productId}}" aria-hidden="true"></i></button>
</span>
</div>
</p>
</div>
Expand Down
8 changes: 5 additions & 3 deletions views/themes/Mono/cart.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,19 @@
&nbsp;{{#each this.options}} {{#if @last}} {{this}} {{else}} {{this}} / {{/if}} {{/each}}
</p>
<p>

<div class="col s12 no-pad-left">
<span class="col s2">
<button class="btn waves-effect waves-light black btn-qty-minus" type="button">-</button>
</span>
<div class="input-field col s8">
<div class="input-field col s5">
<input type="number" class="cart-product-quantity text-center" data-id="{{this.productId}}" id="{{@key}}" maxlength="2" value="{{this.quantity}}">
</div>
<span class="col s2">
<span class="col s3">
<button class="btn waves-effect waves-light black btn-qty-add" type="button">+</button>
</span>
<span class="col s1">
<button class="btn waves-effect waves-light red darken-3 btn-delete-from-cart" data-id="{{this.productId}}" type="button"><i class="fa fa-trash" data-id="{{this.productId}}" aria-hidden="true"></i></button>
</span>
</div>
</p>
</div>
Expand Down

0 comments on commit c674f86

Please sign in to comment.