From 26206b9ad6f9914e42e0080173b4cacfabfade65 Mon Sep 17 00:00:00 2001 From: Leonardo Matos Date: Thu, 29 Aug 2019 19:20:20 -0300 Subject: [PATCH] feat(save): add 'save' method --- src/methods/save.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/methods/save.js diff --git a/src/methods/save.js b/src/methods/save.js new file mode 100644 index 0000000..b6fd4d3 --- /dev/null +++ b/src/methods/save.js @@ -0,0 +1,26 @@ +export default self => { + const { cart, storageKey, localStorage } = self + // fix cart subtotal first + cart.subtotal = 0 + for (let i = 0; i < cart.items.length; i++) { + const item = cart.items[i] + cart.subtotal += item.quantity * (item.final_price || item.price) + } + if (typeof localStorage === 'object' && localStorage) { + localStorage.setItem(storageKey, JSON.stringify(cart)) + } + return self +} + +/** + * @method + * @name EcomCart#save + * @description Save cart object to local storage. + * + * @returns {self} + * + * @example + +cart.save() + + */