-
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(add-item): handling addItem method
- Loading branch information
Showing
4 changed files
with
91 additions
and
1 deletion.
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
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,12 @@ | ||
export default item => { | ||
// fix item quantity if needed | ||
// use minimun quantity 1 by default | ||
const min = item.min_quantity || 1 | ||
const max = item.max_quantity | ||
if (typeof item.quantity !== 'number' || isNaN(item.quantity) || item.quantity < min) { | ||
item.quantity = min | ||
} else if (max && item.quantity > max) { | ||
item.quantity = max | ||
} | ||
return item | ||
} |
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,70 @@ | ||
import { randomObjectId } from '@ecomplus/utils' | ||
import fixItemQuantity from './../lib/fix-item-quantity' | ||
|
||
// add item to cart | ||
export default ({ cart, saveCart }, newItem, save = true) => { | ||
// check required fields | ||
if (typeof newItem.product_id !== 'string' || | ||
typeof newItem.quantity !== 'number' || !(newItem.quantity >= 0) || | ||
typeof newItem.price !== 'number' || !(newItem.price >= 0)) { | ||
// trying to add invalid item object | ||
return null | ||
} | ||
|
||
let fixedItem | ||
for (let i = 0; i < cart.items.length; i++) { | ||
const item = cart.items[i] | ||
// check IDs | ||
if (item.product_id === newItem.product_id && item.variation_id === newItem.variation_id) { | ||
// same product and variation | ||
// update quantity and price | ||
item.quantity += newItem.quantity | ||
if (newItem.price) { | ||
item.price = newItem.price | ||
} | ||
if (newItem.final_price) { | ||
item.final_price = newItem.final_price | ||
} | ||
fixedItem = fixItemQuantity(item) | ||
} | ||
} | ||
|
||
if (!fixedItem) { | ||
if (!newItem._id) { | ||
// generate random ObjectID | ||
newItem._id = randomObjectId() | ||
} | ||
// add item to cart | ||
cart.items.push(newItem) | ||
fixedItem = fixItemQuantity(newItem) | ||
} | ||
|
||
if (save) { | ||
saveCart() | ||
} | ||
return fixedItem | ||
} | ||
|
||
/** | ||
* @method | ||
* @name EcomCart#addItem | ||
* @description Push new item to cart data and save. | ||
* | ||
* @param {item} newItem - New cart item object | ||
* @param {boolean} [save=true] - Save cart data | ||
* | ||
* @returns {item|null} Returns the saved item object (with ID) or null | ||
* when new item object is invalid. | ||
* | ||
* @example | ||
cart.addItem({ | ||
product_id: '123a5432109876543210cdef', | ||
sku: 's-MP_2B4', | ||
name: 'Mens Pique Polo Shirt', | ||
quantity: 4, | ||
price: 42.9, | ||
keep_item_price: false | ||
}) | ||
*/ |
Empty file.