@searchspring/snap-platforms 0.56.1 → 0.56.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/esm/bigcommerce/src/addToCart.d.ts +8 -0
- package/dist/esm/bigcommerce/src/addToCart.d.ts.map +1 -0
- package/dist/esm/bigcommerce/src/addToCart.js +81 -0
- package/dist/esm/bigcommerce/src/index.d.ts +2 -0
- package/dist/esm/bigcommerce/src/index.d.ts.map +1 -0
- package/dist/esm/bigcommerce/src/index.js +1 -0
- package/package.json +6 -4
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Product } from '@searchspring/snap-store-mobx';
|
|
2
|
+
type BigCommerceAddToCartConfig = {
|
|
3
|
+
redirect?: boolean | string;
|
|
4
|
+
idFieldName?: string;
|
|
5
|
+
};
|
|
6
|
+
export declare const addToCart: (items: Product[], config?: BigCommerceAddToCartConfig) => Promise<void>;
|
|
7
|
+
export {};
|
|
8
|
+
//# sourceMappingURL=addToCart.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"addToCart.d.ts","sourceRoot":"","sources":["../../../../bigcommerce/src/addToCart.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,+BAA+B,CAAC;AAE7D,KAAK,0BAA0B,GAAG;IACjC,QAAQ,CAAC,EAAE,OAAO,GAAG,MAAM,CAAC;IAC5B,WAAW,CAAC,EAAE,MAAM,CAAC;CACrB,CAAC;AAWF,eAAO,MAAM,SAAS,UAAiB,OAAO,EAAE,WAAW,0BAA0B,kBAkDpF,CAAC"}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
export const addToCart = async (items, config) => {
|
|
2
|
+
if (!items) {
|
|
3
|
+
console.error('Error: no products to add');
|
|
4
|
+
return;
|
|
5
|
+
}
|
|
6
|
+
const formData = {
|
|
7
|
+
line_items: [],
|
|
8
|
+
};
|
|
9
|
+
items.map((item) => {
|
|
10
|
+
let id = item?.display?.mappings?.core?.uid;
|
|
11
|
+
// try to find custom field in data
|
|
12
|
+
if (config?.idFieldName) {
|
|
13
|
+
let level = item;
|
|
14
|
+
config.idFieldName.split('.').map((field) => {
|
|
15
|
+
if (level[field]) {
|
|
16
|
+
level = level[field];
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
console.error('Error: couldnt find column in item data. please check your idFieldName is correct in the config.');
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
if (level && level !== item) {
|
|
24
|
+
id = level;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
if (id && item.quantity) {
|
|
28
|
+
const obj = {
|
|
29
|
+
product_id: id,
|
|
30
|
+
quantity: item.quantity,
|
|
31
|
+
};
|
|
32
|
+
formData.line_items.push(obj);
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
// first check how many products we are adding
|
|
36
|
+
if (formData.line_items.length) {
|
|
37
|
+
for (let i = 0; i < formData.line_items.length; i++) {
|
|
38
|
+
await addSingleProductv1(formData.line_items[i]);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
// do redirect (or not)
|
|
42
|
+
if (config?.redirect !== false) {
|
|
43
|
+
setTimeout(() => (window.location.href = typeof config?.redirect == 'string' ? config?.redirect : '/cart.php'));
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
const addSingleProductv1 = async (item) => {
|
|
47
|
+
if (!item) {
|
|
48
|
+
console.error('Error: no product to add');
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
const endpoint = {
|
|
52
|
+
route: `/remote/v1/cart/add`,
|
|
53
|
+
method: 'POST',
|
|
54
|
+
accept: 'application/json',
|
|
55
|
+
content: 'application/json',
|
|
56
|
+
success: 200,
|
|
57
|
+
};
|
|
58
|
+
try {
|
|
59
|
+
const payload = JSON.stringify({
|
|
60
|
+
...item,
|
|
61
|
+
action: 'add',
|
|
62
|
+
});
|
|
63
|
+
const init = {
|
|
64
|
+
method: endpoint.method,
|
|
65
|
+
credentials: 'same-origin',
|
|
66
|
+
headers: {
|
|
67
|
+
// note: no authorization
|
|
68
|
+
Accept: endpoint.accept,
|
|
69
|
+
'Content-Type': endpoint.content,
|
|
70
|
+
},
|
|
71
|
+
body: payload,
|
|
72
|
+
};
|
|
73
|
+
const response = await fetch(endpoint.route, init);
|
|
74
|
+
if (response.status !== endpoint.success) {
|
|
75
|
+
throw new Error(`Error: addToCart responded with ${response.status}, ${response}`);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
catch (err) {
|
|
79
|
+
console.error(err);
|
|
80
|
+
}
|
|
81
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../bigcommerce/src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './addToCart';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@searchspring/snap-platforms",
|
|
3
|
-
"version": "0.56.
|
|
3
|
+
"version": "0.56.3",
|
|
4
4
|
"description": "Snap Platforms Library",
|
|
5
5
|
"author": "Searchspring",
|
|
6
6
|
"license": "MIT",
|
|
@@ -18,8 +18,10 @@
|
|
|
18
18
|
"test:watch": "jest --watch"
|
|
19
19
|
},
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@searchspring/snap-
|
|
22
|
-
|
|
21
|
+
"@searchspring/snap-toolbox": "^0.56.3"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"@searchspring/snap-store-mobx": "^0.56.3"
|
|
23
25
|
},
|
|
24
26
|
"sideEffects": false,
|
|
25
27
|
"files": [
|
|
@@ -42,5 +44,5 @@
|
|
|
42
44
|
"require": "./dist/cjs/bigcommerce/src/index.js"
|
|
43
45
|
}
|
|
44
46
|
},
|
|
45
|
-
"gitHead": "
|
|
47
|
+
"gitHead": "adfae18ce3d54a471fa84feb5dd88a074c204520"
|
|
46
48
|
}
|