peet86 / cart-localstorage

JavaScript Shopping Cart library on top of localStorage
https://peet86.github.io/cart-localstorage/
MIT License
91 stars 18 forks source link

Custom reducer function example for Total #16

Closed bkoranda closed 3 years ago

bkoranda commented 3 years ago

Thank you for this library.

By default returns with the total price: or you can pass a custom reducer function to have full control over the calculation.

Do you have an example of using a reducer function for total? I'm trying to implement applying "discount" functionality (ie: 10% off the subtotal) but not sure what the best way to implement that would be.

Thank you in advance if you are able to recommend anything.

peet86 commented 3 years ago

Yes, you can extend this calculation (default subtotal) with product.discount:

const myReducer = (sum, product) => (sum += (product.price * product.quantity))
total(myReducer)
peet86 commented 3 years ago

@peet86 todo: add example to the readme / demo.