Adding in a maxQuantity value when adding to cart

master
Mark Moffat 2020-01-08 19:10:45 +10:30
parent fe14f28994
commit 3bda249c68
5 changed files with 26 additions and 3 deletions

View File

@ -121,7 +121,7 @@
]
}
},
"productStock": 10
"productStock": 100
},
{
"productPermalink": "hudderton-backpack",

View File

@ -110,6 +110,9 @@
"type": "boolean",
"default": true
},
"maxQuantity": {
"type": "number"
},
"modules": {
"type": "object",
"properties": {

View File

@ -25,6 +25,7 @@
"orderHook": "",
"availableLanguages": ["en", "it"],
"defaultLocale": "en",
"maxQuantity": 25,
"modules": {
"enabled": {
"shipping": "shipping-basic"

View File

@ -365,6 +365,13 @@ router.post('/product/addtocart', async (req, res, next) => {
let productQuantity = req.body.productQuantity ? parseInt(req.body.productQuantity) : 1;
const productComment = req.body.productComment ? req.body.productComment : null;
// If maxQuantity set, ensure the quantity doesn't exceed that value
if(config.maxQuantity && productQuantity > config.maxQuantity){
return res.status(400).json({
message: 'The quantity exceeds the max amount. Please contact us for larger orders.'
});
}
// Don't allow negative quantity
if(productQuantity < 1){
productQuantity = 1;

View File

@ -40,13 +40,25 @@ test('[Fail] Add product to cart when subscription already added', async t => {
.post('/product/addtocart')
.send({
productId: g.products[1]._id,
productQuantity: 100,
productQuantity: 1,
productOptions: JSON.stringify(g.products[1].productOptions)
})
.expect(400);
t.deepEqual(res.body.message, 'Subscription already existing in cart. You cannot add more.');
});
test('[Fail] Add quantity which exceeds the maxQuantity', async t => {
const res = await g.request
.post('/product/addtocart')
.send({
productId: g.products[4]._id,
productQuantity: 75,
productOptions: {}
})
.expect(400);
t.deepEqual(res.body.message, 'The quantity exceeds the max amount. Please contact us for larger orders.');
});
test('[Success] Empty cart', async t => {
const res = await g.request
.post('/product/emptycart')
@ -113,7 +125,7 @@ test('[Fail] Add product to cart with not enough stock', async t => {
.post('/product/addtocart')
.send({
productId: g.products[0]._id,
productQuantity: 100,
productQuantity: 20,
productOptions: JSON.stringify(g.products[0].productOptions)
})
.expect(400);