Skip to content

Commit

Permalink
feat(add-item): handling addItem method
Browse files Browse the repository at this point in the history
  • Loading branch information
leomp12 committed Aug 29, 2019
1 parent eea39e1 commit e8a09af
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/

import { _config } from '@ecomplus/utils'
import addItem from './methods/add-item'
import fixItem from './methods/fix-item'

/**
* Vanilla JS library to handle shopping cart object on E-Com Plus stores.
Expand Down Expand Up @@ -57,7 +59,13 @@ export default function (storeId, storageKey = _key, localStorage = _storage) {
* @name EcomCart#data
* @type {array<string>}
*/
this.data = {}
this.data = {
items: []
}

// instance methods
this.addItem = addItem
this.fixItem = fixItem

if (localStorage && storageKey) {
// try to preset cart data from storage
Expand Down
12 changes: 12 additions & 0 deletions src/lib/fix-item-quantity.js
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
}
70 changes: 70 additions & 0 deletions src/methods/add-item.js
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 added src/methods/add-product.js
Empty file.

0 comments on commit e8a09af

Please sign in to comment.