diff --git a/src/lib/fix-item-final-price.js b/src/lib/fix-item-final-price.js new file mode 100644 index 0000000..8eca7af --- /dev/null +++ b/src/lib/fix-item-final-price.js @@ -0,0 +1,15 @@ +export default item => { + // fix item final price with customizations additions + // @TODO handle gift wrap + if (Array.isArray(item.customizations)) { + item.customizations.forEach(customization => { + if (customization.add_to_price) { + const { type, addition } = customization.add_to_price + item.final_price += type === 'percentage' + ? item.price * addition / 100 + : addition + } + }) + } + return item +} diff --git a/src/methods/add-item.js b/src/methods/add-item.js index c61df27..1e344f9 100644 --- a/src/methods/add-item.js +++ b/src/methods/add-item.js @@ -1,5 +1,6 @@ import { randomObjectId } from '@ecomplus/utils' import fixItemQuantity from './../lib/fix-item-quantity' +import fixItemFinalPrice from './../lib/fix-item-final-price' import fixSubtotal from './../lib/fix-subtotal' /** @@ -59,6 +60,7 @@ export default ({ data, save }, emitter, [newItem, canSave = false]) => { } data.items.push(itemCopy) fixedItem = fixItemQuantity(itemCopy) + fixItemFinalPrice(fixedItem) } fixSubtotal(data) diff --git a/src/methods/increase-item-qnt.js b/src/methods/increase-item-qnt.js index 0585b39..66d82b4 100644 --- a/src/methods/increase-item-qnt.js +++ b/src/methods/increase-item-qnt.js @@ -1,4 +1,5 @@ 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, [itemId, quantity = 1, canSave = true]) => { @@ -9,6 +10,7 @@ export default ({ data, save }, emitter, [itemId, quantity = 1, canSave = true]) item.quantity += quantity fixItemQuantity(item) + fixItemFinalPrice(item) fixSubtotal(data) /**