Skip to content

Commit

Permalink
feat(remove-item): handle 'removeItem' method
Browse files Browse the repository at this point in the history
  • Loading branch information
leomp12 committed Aug 29, 2019
1 parent 764c007 commit 03551f1
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/methods/remove-item.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
export default (self, itemId, save = true) => {
// find respective item on list by ID
const { cart } = self
for (let i = 0; i < cart.items.length; i++) {
const item = cart.items[i]
if (item._id === itemId) {
// item found
// remove from items array
cart.items.splice(i, 1)
cart.save()
return item
}
}
return null
}

/**
* @method
* @name EcomCart#removeItem
* @description Remove specific item from cart by ID.
*
* @param {string} itemId - The unique object ID of item
* @param {boolean} [save=true] - Save cart data
*
* @returns {object|null} Returns the removed item object or null
* when item not found.
*
* @example
cart.removeItem('12300000000000000000000f')
*/

0 comments on commit 03551f1

Please # to comment.