48 lines
1.7 KiB
JavaScript
48 lines
1.7 KiB
JavaScript
const domesticShippingAmount = 10;
|
|
const internationalShippingAmount = 25;
|
|
const freeThreshold = 100;
|
|
const shippingFromCountry = 'Australia';
|
|
|
|
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;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
// 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;
|
|
};
|
|
|
|
module.exports = {
|
|
calculateShipping
|
|
};
|