-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(remove-item): handle 'removeItem' method
- Loading branch information
Showing
1 changed file
with
32 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
*/ |