From 764c007c5c8b9088119e76b11137ea967d0d021d Mon Sep 17 00:00:00 2001 From: Leonardo Matos Date: Thu, 29 Aug 2019 19:22:01 -0300 Subject: [PATCH] feat(increase-item-qnt): handle 'increaseItemQnt' method --- src/methods/increase-item-qnt.js | 34 ++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 src/methods/increase-item-qnt.js diff --git a/src/methods/increase-item-qnt.js b/src/methods/increase-item-qnt.js new file mode 100644 index 0000000..20684e4 --- /dev/null +++ b/src/methods/increase-item-qnt.js @@ -0,0 +1,34 @@ +import fixItemQuantity from './../lib/fix-item-quantity' + +export default (self, itemId, quantity = 1, save = true) => { + // find respective item on list by ID + const item = self.cart.items.find(({ _id }) => _id === itemId) + if (item) { + item.quantity += quantity + fixItemQuantity(item) + if (save) { + self.save() + } + } else { + return null + } + return item +} + +/** + * @method + * @name EcomCart#increaseItemQnt + * @description Increase quantity of specific item by ID and save cart. + * + * @param {string} itemId - The unique object ID of item + * @param {integer} [quantity=1] - Quantity to increase (can be negative) + * @param {boolean} [save=true] - Save cart data + * + * @returns {object|null} Returns the updated item object or null + * when item not found. + * + * @example + +cart.increaseItemQnt('12300000000000000000000f', 3) + + */