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

Commit 1e9f9db

Browse files
committed
Handle bundled products
This changes netlify-commerce to look for .netlify-commerce-product instead of an ID Allows multiple product metadata on the same page Allows prices to define sub items of different types for VAT calculations No longer clears the cart when creating an order
1 parent 830b536 commit 1e9f9db

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
{
22
"name": "netlify-commerce-js",
3-
"version": "1.3.1",
3+
"version": "2.0.0",
44
"description": "Netlify Commerce API client for JavaScript",
55
"main": "lib/index.js",
6+
"files": ["lib/", "README.md"],
67
"scripts": {
78
"compile": "babel --presets es2015 --plugins syntax-object-rest-spread,transform-object-rest-spread -d lib/ src/",
89
"prepublish": "npm run compile",

src/index.js

+28-4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,18 @@ function applyTax(price, quantity, percentage) {
2626
}
2727

2828
function getTax(item, taxes, country) {
29+
if (item.price.items) {
30+
return item.price.items
31+
.map((i) => getTax(
32+
Object.assign({}, item, {type: i.type, vat: i.vat, price: {amount: i.amount, currency: item.price.currency}}),
33+
taxes,
34+
country
35+
))
36+
.reduce((result, price) => {
37+
const cents = result.cents + price.cents;
38+
return {amount: centsToAmount(cents), cents, currency: price.currency}
39+
}, {amount: "0.00", cents: 0, currency: item.price.currency});
40+
}
2941
if (item.vat) {
3042
return applyTax(item.price, item.quantity, parseInt(item.vat, 10));
3143
}
@@ -73,8 +85,21 @@ export default class NetlifyCommerce {
7385
return response.text().then((html) => {
7486
const doc = document.implementation.createHTMLDocument("product");
7587
doc.documentElement.innerHTML = html;
76-
const product = JSON.parse(doc.getElementById("netlify-commerce-product").innerHTML);
77-
const {sku, title, prices, description, type, vat} = product;
88+
const products = Array.from(doc.getElementsByClassName("netlify-commerce-product"))
89+
.map((el) => JSON.parse(el.innerHTML));
90+
91+
if (products.length === 0) {
92+
return Promise.reject("No .netlify-commerce-product found in product path");
93+
}
94+
95+
const sku = products.length === 1 ? (item.sku || products[0].sku) : item.sku;
96+
97+
const product = products.find((prod) => prod.sku === sku);
98+
if (!product) {
99+
return Promise.reject(`No .netlify-commerce-product matching sku=${sku} found in product path`);
100+
}
101+
102+
const {title, prices, description, type, vat} = product;
78103
if (sku && title && prices) {
79104
if (this.line_items[sku]) {
80105
this.line_items[sku].quantity += quantity;
@@ -86,7 +111,7 @@ export default class NetlifyCommerce {
86111
return this.getCart();
87112
});
88113
} else {
89-
return Promise.reject("Failed to read sku, title and price from product path");
114+
return Promise.reject("Failed to read sku, title and price from product path: %o", {sku, title, prices});
90115
}
91116
});
92117
});
@@ -182,7 +207,6 @@ export default class NetlifyCommerce {
182207
})
183208
})).then((order) => {
184209
const cart = this.getCart();
185-
this.clearCart();
186210
return {cart, order};
187211
});
188212
} else {

0 commit comments

Comments
 (0)