Skip to content

Commit

Permalink
feat(method): add new 'fixItem' method
Browse files Browse the repository at this point in the history
force item quantity and final price fix by reference
  • Loading branch information
leomp12 committed Oct 25, 2020
1 parent 0f59647 commit bf66b4c
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as EventEmitter from 'eventemitter3'

import addItem from './methods/add-item'
import addPoduct from './methods/add-product'
import fixItem from './methods/fix-item'
import increaseItemQnt from './methods/increase-item-qnt'
import parseProduct from './methods/parse-product'
import removeItem from './methods/remove-item'
Expand Down Expand Up @@ -111,6 +112,10 @@ const EcomCart = function (storeId, storageKey = 'ecomShoppingCart', localStorag
return methodsMiddleware(addPoduct, [product, variationId, quantity, canSave])
}

this.fixItem = (item, canSave) => {
return methodsMiddleware(fixItem, [item, canSave])
}

this.increaseItemQnt = (itemId, quantity, canSave) => {
return methodsMiddleware(increaseItemQnt, [itemId, quantity, canSave])
}
Expand Down
65 changes: 65 additions & 0 deletions src/methods/fix-item.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import fixItemQuantity from './../lib/fix-item-quantity'
import fixItemFinalPrice from './../lib/fix-item-final-price'
import fixSubtotal from './../lib/fix-subtotal'

export default ({ data, save }, emitter, [item, canSave = true]) => {
if (!item) {
return null
}

fixItemQuantity(item)
fixItemFinalPrice(item)
fixSubtotal(data)

/**
* @event EcomCart#fixItem
* @type {object}
* @property {object} data - Shopping cart data
* @property {object} item - Cart item changed
* @example ecomCart.on('fixItem', ({ data, item }) => { console.log(data, item) })
*/
emitter.emit('fixItem', { data, item })

if (canSave) {
save(false)
}
return item
}

/**
* @method
* @name EcomCart#fixItem
* @description Check and fix (if needed) item quantity, final price and cart subtotal.
*
* @param {string} item - The cart item object (by reference)
* @param {boolean} [canSave=true] - Save cart data
*
* @returns {object|null} Returns the updated item object or null
* when item not found.
*
* @example
const item = ecomCart.addItem({
_id: '12300000000000000000000f',
product_id: '123a5432109876543210cdef',
sku: 's-MP_2B4',
name: 'Mens Pique Polo Shirt',
quantity: 4,
price: 42.9,
keep_item_price: false
})
item.customizations = [{
_id: 'c2300000000000000000000f',
label: 'Custom name',
option: {
text: 'Jonh Doe'
},
add_to_price: {
type: 'fixed',
addition: 10
}
}]
item.quantity = 6
ecomCart.fixItemQnt(item)
*/

0 comments on commit bf66b4c

Please sign in to comment.