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 // Update the total items in cart for the badge
req.session.totalCartItems = Object.keys(req.session.cart).length; 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 // Calculate shipping using the loaded module
const shippingCost = config.modules.loaded.shipping.calculateShipping(req.session.totalCartAmount, config); config.modules.loaded.shipping.calculateShipping(
if(shippingCost > 0){ netCartAmount,
req.session.totalCartShipping = parseInt(shippingCost); config,
req.session.totalCartAmount = req.session.totalCartAmount + parseInt(shippingCost); req
req.session.shippingCostApplied = true; );
}else{
req.session.shippingCostApplied = false;
}
}; };
const emptyCart = async (req, res, type, customMessage) => { 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 freeThreshold = 100;
const shippingFromCountry = 'Australia';
const calculateShipping = (amount, config) => { const calculateShipping = (amount, config, req) => {
// When set to instore shipping is not applicable. // When set to instore shipping is not applicable.
if(config.paymentGateway === 'instore'){ if(config.paymentGateway === 'instore'){
return 0; // Update message and amount
req.session.shippingMessage = 'Instore pickup';
req.session.totalCartShipping = 0;
return;
} }
if(amount >= freeThreshold){ 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 = { module.exports = {

View File

@ -486,9 +486,9 @@ function updateCartDiv(){
// Work out the shipping // Work out the shipping
var shippingTotalAmt = numeral(session.totalCartShipping).format('0.00'); var shippingTotalAmt = numeral(session.totalCartShipping).format('0.00');
var shippingTotal = `<strong id="shipping-amount">${result.currencySymbol}${shippingTotalAmt}</strong>`; var shippingTotal = `${session.shippingMessage} :<strong id="shipping-amount">${result.currencySymbol}${shippingTotalAmt}</strong>`;
if(shippingTotalAmt === 0){ if(session.totalCartShipping === 0){
shippingTotal = '<strong id="shipping-amount">FREE</strong>'; shippingTotal = `<strong id="shipping-amount">${session.shippingMessage}</strong>`;
} }
// If the cart has contents // If the cart has contents
@ -550,10 +550,10 @@ function updateCartDiv(){
</div>`; </div>`;
}); });
$('#cartBodyWrapper').html(productHtml); $('.cartBodyWrapper').html(productHtml);
$('#cart-count').text(session.totalCartItems); $('#cart-count').text(session.totalCartItems);
}else{ }else{
$('#cartBodyWrapper').html(''); $('.cartBodyWrapper').html('');
} }
// Set the totals section // Set the totals section
@ -561,7 +561,7 @@ function updateCartDiv(){
<div class="row"> <div class="row">
<div class="cart-contents-shipping col-md-12 no-pad-right"> <div class="cart-contents-shipping col-md-12 no-pad-right">
<div class="text-right"> <div class="text-right">
Shipping: ${shippingTotal} ${shippingTotal}
</div> </div>
<div class="text-right"> <div class="text-right">
Total: Total:
@ -579,11 +579,11 @@ function updateCartDiv(){
// Set depending on cart contents // Set depending on cart contents
if(cart){ if(cart){
$('#cartTotalsWrapper').html(cartTotalsHtml); $('.cartTotalsWrapper').html(cartTotalsHtml);
$('#cart-buttons').removeClass('d-none'); $('.cart-buttons').removeClass('d-none');
}else{ }else{
$('#cartTotalsWrapper').html(cartTotalsEmptyHtml); $('.cartTotalsWrapper').html(cartTotalsEmptyHtml);
$('#cart-buttons').addClass('d-none'); $('.cart-buttons').addClass('d-none');
} }
}) })
.fail(function(result){ .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, config: req.app.config,
session: req.session, session: req.session,
paymentType, paymentType,
cartSize: 'part',
cartClose: false, cartClose: false,
page: 'checkout-information', page: 'checkout-information',
countryList, countryList,
@ -124,12 +123,21 @@ router.get('/checkout/shipping', async (req, res, next) => {
return; 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 // render the payment page
res.render(`${config.themeViews}checkout-shipping`, { res.render(`${config.themeViews}checkout-shipping`, {
title: 'Checkout', title: 'Checkout',
config: req.app.config, config: req.app.config,
session: req.session, session: req.session,
cartSize: 'part',
cartClose: false, cartClose: false,
cartReadOnly: true, cartReadOnly: true,
page: 'checkout-shipping', page: 'checkout-shipping',
@ -146,7 +154,6 @@ router.get('/checkout/cart', (req, res) => {
res.render(`${config.themeViews}checkout-cart`, { res.render(`${config.themeViews}checkout-cart`, {
page: req.query.path, page: req.query.path,
cartSize: 'full',
config, config,
session: req.session, session: req.session,
message: clearSessionValue(req.session, 'message'), message: clearSessionValue(req.session, 'message'),
@ -189,7 +196,6 @@ router.get('/checkout/payment', (req, res) => {
session: req.session, session: req.session,
paymentPage: true, paymentPage: true,
paymentType, paymentType,
cartSize: 'part',
cartClose: true, cartClose: true,
cartReadOnly: true, cartReadOnly: true,
page: 'checkout-information', page: 'checkout-information',

View File

@ -139,7 +139,7 @@
<div id="cart" class="col-md-12"> <div id="cart" class="col-md-12">
{{> (getTheme 'cart')}} {{> (getTheme 'cart')}}
<div class="row"> <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> <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> <a href="/checkout/information" class="btn btn-outline-primary float-right">Checkout</a>
</div> </div>

View File

@ -1,7 +1,7 @@
<div class="card top-marg-15 bottom-marg-15"> <div class="card top-marg-15 bottom-marg-15">
<div class="card-body cart-body"> <div class="card-body cart-body">
<h5 class="card-title">{{ @root.__ "Cart contents" }}</h5> <h5 class="card-title">{{ @root.__ "Cart contents" }}</h5>
<div id="cartBodyWrapper"> <div class="cartBodyWrapper">
{{#each @root.session.cart}} {{#each @root.session.cart}}
<div class="d-flex flex-row bottom-pad-15"> <div class="d-flex flex-row bottom-pad-15">
<div class="col-4 col-md-3"> <div class="col-4 col-md-3">
@ -47,11 +47,11 @@
</div> </div>
{{/each}} {{/each}}
</div> </div>
<div id="cartTotalsWrapper" class="container-fluid"> <div class="cartTotalsWrapper container-fluid">
{{#if @root.session.cart}} {{#if @root.session.cart}}
<div class="row"> <div class="row">
<div class="cart-contents-shipping col-md-12 no-pad-right"> <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"> <div class="text-right">
{{ @root.__ "Shipping" }} {{ @root.__ "Shipping" }}
<strong id="shipping-amount">{{currencySymbol @root.config.currencySymbol}}{{formatAmount @root.session.totalCartShipping}}</strong> <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"> <div id="cart" class="col-md-12">
{{> (getTheme 'cart')}} {{> (getTheme 'cart')}}
<div class="row"> <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> <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> <a href="/checkout/information" class="btn btn-outline-danger float-right">Checkout</a>
</div> </div>

View File

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

View File

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

View File

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