expressCart/lib/modules/shipping-basic.js

48 lines
1.7 KiB
JavaScript
Raw Normal View History

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