Improvements to the shipping module & cart updates

master
Mark Moffat 2020-01-04 17:40:58 +10:30
parent 3373a60d8f
commit 11302e2255
11 changed files with 71 additions and 40 deletions

View File

@ -131,15 +131,15 @@ const updateTotalCart = (req, res) => {
// Update the total items in cart for the badge
req.session.totalCartItems = Object.keys(req.session.cart).length;
// Net cart amount
const netCartAmount = req.session.totalCartAmount - req.session.totalCartShipping || 0;
// Calculate shipping using the loaded module
const shippingCost = config.modules.loaded.shipping.calculateShipping(req.session.totalCartAmount, config);
if(shippingCost > 0){
req.session.totalCartShipping = parseInt(shippingCost);
req.session.totalCartAmount = req.session.totalCartAmount + parseInt(shippingCost);
req.session.shippingCostApplied = true;
}else{
req.session.shippingCostApplied = false;
}
config.modules.loaded.shipping.calculateShipping(
netCartAmount,
config,
req
);
};
const emptyCart = async (req, res, type, customMessage) => {

View File

@ -1,15 +1,40 @@
const shippingAmount = 10;
const domesticShippingAmount = 10;
const internationalShippingAmount = 25;
const freeThreshold = 100;
const shippingFromCountry = 'Australia';
const calculateShipping = (amount, config) => {
const calculateShipping = (amount, config, req) => {
// When set to instore shipping is not applicable.
if(config.paymentGateway === 'instore'){
return 0;
// Update message and amount
req.session.shippingMessage = 'Instore pickup';
req.session.totalCartShipping = 0;
return;
}
if(amount >= freeThreshold){
return 0;
req.session.shippingMessage = 'FREE shipping';
req.session.totalCartShipping = 0;
return;
}
return shippingAmount;
// If there is no country set, we estimate shipping
if(!req.session.customerCountry){
req.session.shippingMessage = 'Estimated shipping';
req.session.totalCartShipping = domesticShippingAmount;
return;
}
// Check for international
if(req.session.customerCountry.toLowerCase() !== shippingFromCountry.toLowerCase()){
req.session.shippingMessage = 'International shipping';
req.session.totalCartShipping = internationalShippingAmount;
return;
}
// Domestic shipping
req.session.shippingMessage = 'Domestic shipping';
req.session.totalCartShipping = domesticShippingAmount;
};
module.exports = {

View File

@ -486,9 +486,9 @@ function updateCartDiv(){
// Work out the shipping
var shippingTotalAmt = numeral(session.totalCartShipping).format('0.00');
var shippingTotal = `<strong id="shipping-amount">${result.currencySymbol}${shippingTotalAmt}</strong>`;
if(shippingTotalAmt === 0){
shippingTotal = '<strong id="shipping-amount">FREE</strong>';
var shippingTotal = `${session.shippingMessage} :<strong id="shipping-amount">${result.currencySymbol}${shippingTotalAmt}</strong>`;
if(session.totalCartShipping === 0){
shippingTotal = `<strong id="shipping-amount">${session.shippingMessage}</strong>`;
}
// If the cart has contents
@ -550,10 +550,10 @@ function updateCartDiv(){
</div>`;
});
$('#cartBodyWrapper').html(productHtml);
$('.cartBodyWrapper').html(productHtml);
$('#cart-count').text(session.totalCartItems);
}else{
$('#cartBodyWrapper').html('');
$('.cartBodyWrapper').html('');
}
// Set the totals section
@ -561,7 +561,7 @@ function updateCartDiv(){
<div class="row">
<div class="cart-contents-shipping col-md-12 no-pad-right">
<div class="text-right">
Shipping: ${shippingTotal}
${shippingTotal}
</div>
<div class="text-right">
Total:
@ -579,11 +579,11 @@ function updateCartDiv(){
// Set depending on cart contents
if(cart){
$('#cartTotalsWrapper').html(cartTotalsHtml);
$('#cart-buttons').removeClass('d-none');
$('.cartTotalsWrapper').html(cartTotalsHtml);
$('.cart-buttons').removeClass('d-none');
}else{
$('#cartTotalsWrapper').html(cartTotalsEmptyHtml);
$('#cart-buttons').addClass('d-none');
$('.cartTotalsWrapper').html(cartTotalsEmptyHtml);
$('.cart-buttons').addClass('d-none');
}
})
.fail(function(result){

File diff suppressed because one or more lines are too long

View File

@ -95,7 +95,6 @@ router.get('/checkout/information', async (req, res, next) => {
config: req.app.config,
session: req.session,
paymentType,
cartSize: 'part',
cartClose: false,
page: 'checkout-information',
countryList,
@ -124,12 +123,21 @@ router.get('/checkout/shipping', async (req, res, next) => {
return;
}
// Net cart amount
const netCartAmount = req.session.totalCartAmount - req.session.totalCartShipping || 0;
// Recalculate shipping
config.modules.loaded.shipping.calculateShipping(
netCartAmount,
config,
req
);
// render the payment page
res.render(`${config.themeViews}checkout-shipping`, {
title: 'Checkout',
config: req.app.config,
session: req.session,
cartSize: 'part',
cartClose: false,
cartReadOnly: true,
page: 'checkout-shipping',
@ -146,7 +154,6 @@ router.get('/checkout/cart', (req, res) => {
res.render(`${config.themeViews}checkout-cart`, {
page: req.query.path,
cartSize: 'full',
config,
session: req.session,
message: clearSessionValue(req.session, 'message'),
@ -189,7 +196,6 @@ router.get('/checkout/payment', (req, res) => {
session: req.session,
paymentPage: true,
paymentType,
cartSize: 'part',
cartClose: true,
cartReadOnly: true,
page: 'checkout-information',

View File

@ -139,7 +139,7 @@
<div id="cart" class="col-md-12">
{{> (getTheme 'cart')}}
<div class="row">
<div id="cart-buttons" class="col-sm-12 {{showCartButtons @root.session.cart}}">
<div class="col-sm-12 {{showCartButtons @root.session.cart}} cart-buttons">
<button class="btn btn-outline-danger float-left" id="empty-cart" type="button">{{ @root.__ "Empty cart" }}</button>
<a href="/checkout/information" class="btn btn-outline-primary float-right">Checkout</a>
</div>

View File

@ -1,7 +1,7 @@
<div class="card top-marg-15 bottom-marg-15">
<div class="card-body cart-body">
<h5 class="card-title">{{ @root.__ "Cart contents" }}</h5>
<div id="cartBodyWrapper">
<div class="cartBodyWrapper">
{{#each @root.session.cart}}
<div class="d-flex flex-row bottom-pad-15">
<div class="col-4 col-md-3">
@ -47,11 +47,11 @@
</div>
{{/each}}
</div>
<div id="cartTotalsWrapper" class="container-fluid">
<div class="cartTotalsWrapper container-fluid">
{{#if @root.session.cart}}
<div class="row">
<div class="cart-contents-shipping col-md-12 no-pad-right">
{{#ifCond @root.session.shippingCostApplied '===' true}}
{{#ifCond @root.session.totalCartShipping '>' 0}}
<div class="text-right">
{{ @root.__ "Shipping" }}
<strong id="shipping-amount">{{currencySymbol @root.config.currencySymbol}}{{formatAmount @root.session.totalCartShipping}}</strong>

View File

@ -3,7 +3,7 @@
<div id="cart" class="col-md-12">
{{> (getTheme 'cart')}}
<div class="row">
<div id="cart-buttons" class="col-sm-12 {{showCartButtons @root.session.cart}}">
<div class="col-sm-12 {{showCartButtons @root.session.cart}} cart-buttons">
<button class="btn btn-outline-danger float-left" id="empty-cart" type="button">{{ @root.__ "Empty cart" }}</button>
<a href="/checkout/information" class="btn btn-outline-danger float-right">Checkout</a>
</div>

View File

@ -88,7 +88,7 @@
{{> (getTheme 'cart')}}
{{#if @root.session.cart}}
<div class="row">
<div id="cart-buttons" class="col-sm-12 {{showCartButtons @root.session.cart}}">
<div class="col-sm-12 {{showCartButtons @root.session.cart}} cart-buttons">
<button class="btn btn-outline-danger float-right" id="empty-cart" type="button">{{ @root.__ "Empty cart" }}</button>
</div>
{{/if}}

View File

@ -23,7 +23,7 @@
</li>
</ul>
<ul class="list-group bottom-pad-15">
{{#if @root.session.shippingCostApplied}}
{{#ifCond @root.session.totalCartShipping '>' 0}}
<li class="list-group-item">
<div class="row">
<div class="col-md-6">
@ -37,7 +37,7 @@
</li>
{{else}}
<li class="list-group-item">FREE shipping <span class="float-right"><a href="/checkout/shipping">Change</a></span></li>
{{/if}}
{{/ifCond}}
</ul>
<form id="shipping-form" class="shipping-form" action="/{{config.paymentGateway}}/checkout_action{{@root.paymentType}}" method="post" role="form" data-toggle="validator" novalidate="false">
{{#if session.customerPresent}}

View File

@ -14,11 +14,11 @@
<h5 class="card-title">{{ @root.__ "Shipping options" }}</h5>
{{#unless @root.session.shippingOptions}}
<ul class="list-group">
{{#if @root.session.shippingCostApplied}}
<li class="list-group-item">Standard shipping <span class="float-right"><strong>{{currencySymbol @root.config.currencySymbol}}{{formatAmount @root.session.totalCartShipping}}</strong></span></li>
{{#ifCond @root.session.totalCartShipping '>' 0}}
<li class="list-group-item">{{@root.session.shippingMessage}} <span class="float-right"><strong>{{currencySymbol @root.config.currencySymbol}}{{formatAmount @root.session.totalCartShipping}}</strong></span></li>
{{else}}
<li class="list-group-item">FREE shipping <span class="float-right"></li>
{{/if}}
{{/ifCond}}
</ul>
{{/unless}}
</div>
@ -29,7 +29,7 @@
<div id="cart" class="col-md-7 d-none d-sm-block">
{{> (getTheme 'cart')}}
<div class="row">
<div id="cart-buttons" class="col-sm-12 {{showCartButtons @root.session.cart}}">
<div class="col-sm-12 {{showCartButtons @root.session.cart}} cart-buttons">
<button class="btn btn-outline-danger float-right" id="empty-cart" type="button">{{ @root.__ "Empty cart" }}</button>
</div>
</div>