From e8a09af317a95fd1543a9d95dab39dc71dffd886 Mon Sep 17 00:00:00 2001 From: Leonardo Matos Date: Thu, 29 Aug 2019 17:23:40 -0300 Subject: [PATCH] feat(add-item): handling addItem method --- src/index.js | 10 +++++- src/lib/fix-item-quantity.js | 12 +++++++ src/methods/add-item.js | 70 ++++++++++++++++++++++++++++++++++++ src/methods/add-product.js | 0 4 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 src/lib/fix-item-quantity.js create mode 100644 src/methods/add-item.js create mode 100644 src/methods/add-product.js diff --git a/src/index.js b/src/index.js index 6ec5bfc..f0e3746 100644 --- a/src/index.js +++ b/src/index.js @@ -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. @@ -57,7 +59,13 @@ export default function (storeId, storageKey = _key, localStorage = _storage) { * @name EcomCart#data * @type {array} */ - this.data = {} + this.data = { + items: [] + } + + // instance methods + this.addItem = addItem + this.fixItem = fixItem if (localStorage && storageKey) { // try to preset cart data from storage diff --git a/src/lib/fix-item-quantity.js b/src/lib/fix-item-quantity.js new file mode 100644 index 0000000..567be16 --- /dev/null +++ b/src/lib/fix-item-quantity.js @@ -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 +} diff --git a/src/methods/add-item.js b/src/methods/add-item.js new file mode 100644 index 0000000..bc66048 --- /dev/null +++ b/src/methods/add-item.js @@ -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 +}) + + */ diff --git a/src/methods/add-product.js b/src/methods/add-product.js new file mode 100644 index 0000000..e69de29