Skip to content
This repository was archived by the owner on Sep 8, 2021. It is now read-only.

Commit e1c605b

Browse files
committed
Add support for addons
1 parent 4d2c742 commit e1c605b

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "netlify-commerce-js",
3-
"version": "3.4.2",
3+
"version": "3.5.0",
44
"description": "Netlify Commerce API client for JavaScript",
55
"main": "lib/index.js",
66
"files": ["lib/", "README.md"],

src/index.js

+22-1
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ export default class NetlifyCommerce {
121121
} else {
122122
this.line_items[sku] = Object.assign(product, {path, meta, quantity});
123123
}
124+
if (item.addons && product.addons) {
125+
this.line_items[sku].addons = product.addons.filter((addon) => item.addons.indexOf(addon.sku) !== -1);
126+
} else {
127+
delete this.line_items[sku].addons;
128+
}
124129
return this.loadSettings().then(() => {
125130
this.persistCart();
126131
return this.getCart();
@@ -146,8 +151,24 @@ export default class NetlifyCommerce {
146151
const item = cart.items[key] = Object.assign({}, this.line_items[key], {
147152
price: getPrice(this.line_items[key].prices, this.currency, this.user)
148153
});
154+
if (this.line_items[key].addons) {
155+
item.addons = [];
156+
this.line_items[key].addons.forEach((addon) => {
157+
item.addons.push(Object.assign({}, addon, {
158+
price: getPrice(addon.prices, this.currency, this.user)
159+
}));
160+
});
161+
}
149162
item.tax = getTax(item, this.settings && this.settings.taxes, this.billing_country);
150-
cart.subtotal.cents += parseFloat(item.price.amount * item.quantity * 100);
163+
cart.subtotal.cents += parseFloat(item.price.amount) * item.quantity * 100;
164+
if (item.addons) {
165+
item.addonPrice = {
166+
currency: this.currency,
167+
cents: item.addons.reduce((sum, addon) => sum + parseFloat(addon.price.amount) * 100, 0)
168+
};
169+
item.addonPrice.amount = (item.addonPrice.cents / 100).toFixed(2);
170+
cart.subtotal.cents += item.addonPrice.cents * item.quantity;
171+
}
151172
cart.taxes.cents += parseFloat(item.tax.amount * 100);
152173
}
153174
cart.subtotal.amount = centsToAmount(cart.subtotal.cents);

0 commit comments

Comments
 (0)