@athoscommerce/snap-platforms 1.0.0
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/LICENSE +21 -0
- package/README.md +12 -0
- package/dist/cjs/bigcommerce/src/addToCart.d.ts +7 -0
- package/dist/cjs/bigcommerce/src/addToCart.d.ts.map +1 -0
- package/dist/cjs/bigcommerce/src/addToCart.js +181 -0
- package/dist/cjs/bigcommerce/src/index.d.ts +4 -0
- package/dist/cjs/bigcommerce/src/index.d.ts.map +1 -0
- package/dist/cjs/bigcommerce/src/index.js +9 -0
- package/dist/cjs/bigcommerce/src/plugins/pluginBigcommerceAddToCart.d.ts +6 -0
- package/dist/cjs/bigcommerce/src/plugins/pluginBigcommerceAddToCart.d.ts.map +1 -0
- package/dist/cjs/bigcommerce/src/plugins/pluginBigcommerceAddToCart.js +61 -0
- package/dist/cjs/bigcommerce/src/plugins/pluginBigcommerceBackgroundFilters.d.ts +10 -0
- package/dist/cjs/bigcommerce/src/plugins/pluginBigcommerceBackgroundFilters.d.ts.map +1 -0
- package/dist/cjs/bigcommerce/src/plugins/pluginBigcommerceBackgroundFilters.js +103 -0
- package/dist/cjs/common/src/index.d.ts +5 -0
- package/dist/cjs/common/src/index.d.ts.map +1 -0
- package/dist/cjs/common/src/index.js +11 -0
- package/dist/cjs/common/src/plugins/pluginAddToCart.d.ts +8 -0
- package/dist/cjs/common/src/plugins/pluginAddToCart.d.ts.map +1 -0
- package/dist/cjs/common/src/plugins/pluginAddToCart.js +66 -0
- package/dist/cjs/common/src/plugins/pluginBackgroundFilters.d.ts +7 -0
- package/dist/cjs/common/src/plugins/pluginBackgroundFilters.d.ts.map +1 -0
- package/dist/cjs/common/src/plugins/pluginBackgroundFilters.js +123 -0
- package/dist/cjs/common/src/plugins/pluginLogger.d.ts +5 -0
- package/dist/cjs/common/src/plugins/pluginLogger.d.ts.map +1 -0
- package/dist/cjs/common/src/plugins/pluginLogger.js +58 -0
- package/dist/cjs/common/src/plugins/pluginScrollToTop.d.ts +13 -0
- package/dist/cjs/common/src/plugins/pluginScrollToTop.d.ts.map +1 -0
- package/dist/cjs/common/src/plugins/pluginScrollToTop.js +75 -0
- package/dist/cjs/common/src/types.d.ts +27 -0
- package/dist/cjs/common/src/types.d.ts.map +1 -0
- package/dist/cjs/common/src/types.js +2 -0
- package/dist/cjs/magento2/src/addToCart.d.ts +9 -0
- package/dist/cjs/magento2/src/addToCart.d.ts.map +1 -0
- package/dist/cjs/magento2/src/addToCart.js +156 -0
- package/dist/cjs/magento2/src/getFormKey.d.ts +2 -0
- package/dist/cjs/magento2/src/getFormKey.d.ts.map +1 -0
- package/dist/cjs/magento2/src/getFormKey.js +9 -0
- package/dist/cjs/magento2/src/getUenc.d.ts +2 -0
- package/dist/cjs/magento2/src/getUenc.d.ts.map +1 -0
- package/dist/cjs/magento2/src/getUenc.js +9 -0
- package/dist/cjs/magento2/src/index.d.ts +7 -0
- package/dist/cjs/magento2/src/index.d.ts.map +1 -0
- package/dist/cjs/magento2/src/index.js +15 -0
- package/dist/cjs/magento2/src/plugins/pluginMagento2AddToCart.d.ts +6 -0
- package/dist/cjs/magento2/src/plugins/pluginMagento2AddToCart.d.ts.map +1 -0
- package/dist/cjs/magento2/src/plugins/pluginMagento2AddToCart.js +61 -0
- package/dist/cjs/magento2/src/plugins/pluginMagento2BackgroundFilters.d.ts +10 -0
- package/dist/cjs/magento2/src/plugins/pluginMagento2BackgroundFilters.d.ts.map +1 -0
- package/dist/cjs/magento2/src/plugins/pluginMagento2BackgroundFilters.js +89 -0
- package/dist/cjs/magento2/src/plugins/pluginMagento2Base.d.ts +9 -0
- package/dist/cjs/magento2/src/plugins/pluginMagento2Base.d.ts.map +1 -0
- package/dist/cjs/magento2/src/plugins/pluginMagento2Base.js +123 -0
- package/dist/cjs/shopify/src/addToCart.d.ts +12 -0
- package/dist/cjs/shopify/src/addToCart.d.ts.map +1 -0
- package/dist/cjs/shopify/src/addToCart.js +117 -0
- package/dist/cjs/shopify/src/index.d.ts +5 -0
- package/dist/cjs/shopify/src/index.d.ts.map +1 -0
- package/dist/cjs/shopify/src/index.js +11 -0
- package/dist/cjs/shopify/src/plugins/pluginShopifyAddToCart.d.ts +6 -0
- package/dist/cjs/shopify/src/plugins/pluginShopifyAddToCart.d.ts.map +1 -0
- package/dist/cjs/shopify/src/plugins/pluginShopifyAddToCart.js +61 -0
- package/dist/cjs/shopify/src/plugins/pluginShopifyBackgroundFilters.d.ts +12 -0
- package/dist/cjs/shopify/src/plugins/pluginShopifyBackgroundFilters.d.ts.map +1 -0
- package/dist/cjs/shopify/src/plugins/pluginShopifyBackgroundFilters.js +107 -0
- package/dist/cjs/shopify/src/plugins/pluginShopifyMutateResults.d.ts +16 -0
- package/dist/cjs/shopify/src/plugins/pluginShopifyMutateResults.d.ts.map +1 -0
- package/dist/cjs/shopify/src/plugins/pluginShopifyMutateResults.js +80 -0
- package/dist/esm/bigcommerce/src/addToCart.d.ts +7 -0
- package/dist/esm/bigcommerce/src/addToCart.d.ts.map +1 -0
- package/dist/esm/bigcommerce/src/addToCart.js +103 -0
- package/dist/esm/bigcommerce/src/index.d.ts +4 -0
- package/dist/esm/bigcommerce/src/index.d.ts.map +1 -0
- package/dist/esm/bigcommerce/src/index.js +3 -0
- package/dist/esm/bigcommerce/src/plugins/pluginBigcommerceAddToCart.d.ts +6 -0
- package/dist/esm/bigcommerce/src/plugins/pluginBigcommerceAddToCart.d.ts.map +1 -0
- package/dist/esm/bigcommerce/src/plugins/pluginBigcommerceAddToCart.js +11 -0
- package/dist/esm/bigcommerce/src/plugins/pluginBigcommerceBackgroundFilters.d.ts +10 -0
- package/dist/esm/bigcommerce/src/plugins/pluginBigcommerceBackgroundFilters.d.ts.map +1 -0
- package/dist/esm/bigcommerce/src/plugins/pluginBigcommerceBackgroundFilters.js +53 -0
- package/dist/esm/common/src/index.d.ts +5 -0
- package/dist/esm/common/src/index.d.ts.map +1 -0
- package/dist/esm/common/src/index.js +4 -0
- package/dist/esm/common/src/plugins/pluginAddToCart.d.ts +8 -0
- package/dist/esm/common/src/plugins/pluginAddToCart.d.ts.map +1 -0
- package/dist/esm/common/src/plugins/pluginAddToCart.js +15 -0
- package/dist/esm/common/src/plugins/pluginBackgroundFilters.d.ts +7 -0
- package/dist/esm/common/src/plugins/pluginBackgroundFilters.d.ts.map +1 -0
- package/dist/esm/common/src/plugins/pluginBackgroundFilters.js +63 -0
- package/dist/esm/common/src/plugins/pluginLogger.d.ts +5 -0
- package/dist/esm/common/src/plugins/pluginLogger.d.ts.map +1 -0
- package/dist/esm/common/src/plugins/pluginLogger.js +9 -0
- package/dist/esm/common/src/plugins/pluginScrollToTop.d.ts +13 -0
- package/dist/esm/common/src/plugins/pluginScrollToTop.d.ts.map +1 -0
- package/dist/esm/common/src/plugins/pluginScrollToTop.js +24 -0
- package/dist/esm/common/src/types.d.ts +27 -0
- package/dist/esm/common/src/types.d.ts.map +1 -0
- package/dist/esm/common/src/types.js +1 -0
- package/dist/esm/magento2/src/addToCart.d.ts +9 -0
- package/dist/esm/magento2/src/addToCart.d.ts.map +1 -0
- package/dist/esm/magento2/src/addToCart.js +80 -0
- package/dist/esm/magento2/src/getFormKey.d.ts +2 -0
- package/dist/esm/magento2/src/getFormKey.d.ts.map +1 -0
- package/dist/esm/magento2/src/getFormKey.js +5 -0
- package/dist/esm/magento2/src/getUenc.d.ts +2 -0
- package/dist/esm/magento2/src/getUenc.d.ts.map +1 -0
- package/dist/esm/magento2/src/getUenc.js +4 -0
- package/dist/esm/magento2/src/index.d.ts +7 -0
- package/dist/esm/magento2/src/index.d.ts.map +1 -0
- package/dist/esm/magento2/src/index.js +6 -0
- package/dist/esm/magento2/src/plugins/pluginMagento2AddToCart.d.ts +6 -0
- package/dist/esm/magento2/src/plugins/pluginMagento2AddToCart.d.ts.map +1 -0
- package/dist/esm/magento2/src/plugins/pluginMagento2AddToCart.js +11 -0
- package/dist/esm/magento2/src/plugins/pluginMagento2BackgroundFilters.d.ts +10 -0
- package/dist/esm/magento2/src/plugins/pluginMagento2BackgroundFilters.d.ts.map +1 -0
- package/dist/esm/magento2/src/plugins/pluginMagento2BackgroundFilters.js +39 -0
- package/dist/esm/magento2/src/plugins/pluginMagento2Base.d.ts +9 -0
- package/dist/esm/magento2/src/plugins/pluginMagento2Base.d.ts.map +1 -0
- package/dist/esm/magento2/src/plugins/pluginMagento2Base.js +64 -0
- package/dist/esm/shopify/src/addToCart.d.ts +12 -0
- package/dist/esm/shopify/src/addToCart.d.ts.map +1 -0
- package/dist/esm/shopify/src/addToCart.js +63 -0
- package/dist/esm/shopify/src/index.d.ts +5 -0
- package/dist/esm/shopify/src/index.d.ts.map +1 -0
- package/dist/esm/shopify/src/index.js +4 -0
- package/dist/esm/shopify/src/plugins/pluginShopifyAddToCart.d.ts +6 -0
- package/dist/esm/shopify/src/plugins/pluginShopifyAddToCart.d.ts.map +1 -0
- package/dist/esm/shopify/src/plugins/pluginShopifyAddToCart.js +11 -0
- package/dist/esm/shopify/src/plugins/pluginShopifyBackgroundFilters.d.ts +12 -0
- package/dist/esm/shopify/src/plugins/pluginShopifyBackgroundFilters.d.ts.map +1 -0
- package/dist/esm/shopify/src/plugins/pluginShopifyBackgroundFilters.js +56 -0
- package/dist/esm/shopify/src/plugins/pluginShopifyMutateResults.d.ts +16 -0
- package/dist/esm/shopify/src/plugins/pluginShopifyMutateResults.d.ts.map +1 -0
- package/dist/esm/shopify/src/plugins/pluginShopifyMutateResults.js +28 -0
- package/package.json +54 -0
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
13
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.pluginShopifyMutateResults = void 0;
|
|
40
|
+
var pluginShopifyMutateResults = function (cntrlr, config) {
|
|
41
|
+
var _a, _b, _c, _d;
|
|
42
|
+
// do nothing if plugin is disabled
|
|
43
|
+
if ((config === null || config === void 0 ? void 0 : config.enabled) === false)
|
|
44
|
+
return;
|
|
45
|
+
// mutation collectionInUrl
|
|
46
|
+
var collectionHandle = (_a = cntrlr.context.collection) === null || _a === void 0 ? void 0 : _a.handle;
|
|
47
|
+
if (((_c = (_b = config === null || config === void 0 ? void 0 : config.mutations) === null || _b === void 0 ? void 0 : _b.collectionInUrl) === null || _c === void 0 ? void 0 : _c.enabled) === true && collectionHandle) {
|
|
48
|
+
if (!window.Shopify) {
|
|
49
|
+
cntrlr.log.warn('shopify/pluginMutateResults: window.Shopify not found!');
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
// only proceed if controller is search and page type is category
|
|
53
|
+
if (cntrlr.type == 'search' && ((_d = cntrlr.context.page) === null || _d === void 0 ? void 0 : _d.type) == 'category') {
|
|
54
|
+
cntrlr.on('afterStore', function (_a, next_1) { return __awaiter(void 0, [_a, next_1], void 0, function (_b, next) {
|
|
55
|
+
var results;
|
|
56
|
+
var controller = _b.controller;
|
|
57
|
+
return __generator(this, function (_c) {
|
|
58
|
+
switch (_c.label) {
|
|
59
|
+
case 0:
|
|
60
|
+
results = controller.store.results;
|
|
61
|
+
results.forEach(function (result) {
|
|
62
|
+
var _a, _b;
|
|
63
|
+
var resultHandle = result.attributes.handle;
|
|
64
|
+
if (result.type != 'banner' && resultHandle) {
|
|
65
|
+
var routeShopify = ((_b = (_a = window === null || window === void 0 ? void 0 : window.Shopify) === null || _a === void 0 ? void 0 : _a.routes) === null || _b === void 0 ? void 0 : _b.root) || '/';
|
|
66
|
+
var routeCollection = "collections/".concat(collectionHandle, "/");
|
|
67
|
+
result.mappings.core.url = "".concat(routeShopify).concat(routeCollection, "products/").concat(resultHandle);
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
return [4 /*yield*/, next()];
|
|
71
|
+
case 1:
|
|
72
|
+
_c.sent();
|
|
73
|
+
return [2 /*return*/];
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
}); });
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
exports.pluginShopifyMutateResults = pluginShopifyMutateResults;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Product } from '@athoscommerce/snap-store-mobx';
|
|
2
|
+
export type BigCommerceAddToCartConfig = {
|
|
3
|
+
redirect?: boolean | string;
|
|
4
|
+
idFieldName?: string;
|
|
5
|
+
};
|
|
6
|
+
export declare const addToCart: (items: Product[], config?: BigCommerceAddToCartConfig) => Promise<any>;
|
|
7
|
+
//# 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,gCAAgC,CAAC;AAE9D,MAAM,MAAM,0BAA0B,GAAG;IACxC,QAAQ,CAAC,EAAE,OAAO,GAAG,MAAM,CAAC;IAC5B,WAAW,CAAC,EAAE,MAAM,CAAC;CACrB,CAAC;AAQF,eAAO,MAAM,SAAS,UAAiB,OAAO,EAAE,WAAW,0BAA0B,iBA+DpF,CAAC"}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
export const addToCart = async (items, config) => {
|
|
2
|
+
if (!items) {
|
|
3
|
+
console.error('bigcommerce/addToCart: No products to add!');
|
|
4
|
+
return;
|
|
5
|
+
}
|
|
6
|
+
const lineItems = [];
|
|
7
|
+
items.map((item) => {
|
|
8
|
+
let id = item?.display?.mappings?.core?.uid;
|
|
9
|
+
// try to find custom field in data
|
|
10
|
+
if (config?.idFieldName) {
|
|
11
|
+
let level = item;
|
|
12
|
+
config.idFieldName.split('.').map((field) => {
|
|
13
|
+
if (level && level[field]) {
|
|
14
|
+
level = level[field];
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
console.error(`bigcommerce/addToCart: Could not find column in item data. Please verify 'idFieldName' in the config.`);
|
|
18
|
+
level = undefined;
|
|
19
|
+
id = undefined;
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
if (level && level !== item) {
|
|
24
|
+
id = level;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
if (id && item.quantity) {
|
|
28
|
+
const productDetails = {
|
|
29
|
+
product_id: id,
|
|
30
|
+
quantity: item.quantity,
|
|
31
|
+
};
|
|
32
|
+
const options = item.variants?.active?.options;
|
|
33
|
+
if (options) {
|
|
34
|
+
productDetails.optionSelections = [];
|
|
35
|
+
Object.keys(options).forEach((option) => {
|
|
36
|
+
const optionId = options[option].optionId;
|
|
37
|
+
const optionValue = options[option].optionValue;
|
|
38
|
+
if (optionId && optionValue) {
|
|
39
|
+
productDetails.optionSelections?.push({ optionId, optionValue });
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
lineItems.push(productDetails);
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
if (lineItems.length) {
|
|
47
|
+
const addToCartResponse = await addLineItemsToCart(lineItems);
|
|
48
|
+
// do redirect (or not)
|
|
49
|
+
if (config?.redirect !== false) {
|
|
50
|
+
setTimeout(() => (window.location.href = typeof config?.redirect == 'string' ? config?.redirect : '/cart.php'));
|
|
51
|
+
}
|
|
52
|
+
return addToCartResponse;
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
async function addLineItemsToCart(lineItems) {
|
|
56
|
+
try {
|
|
57
|
+
const cartId = await getExistingCartId();
|
|
58
|
+
// if existing cartId use it, otherwise create new cart with items
|
|
59
|
+
let addToCartUrl = '/api/storefront/carts';
|
|
60
|
+
if (cartId) {
|
|
61
|
+
addToCartUrl = `/api/storefront/carts/${cartId}/items`;
|
|
62
|
+
}
|
|
63
|
+
const body = JSON.stringify({ lineItems });
|
|
64
|
+
const response = await fetch(addToCartUrl, {
|
|
65
|
+
method: 'POST',
|
|
66
|
+
headers: {
|
|
67
|
+
Accept: 'application/json',
|
|
68
|
+
'Content-Type': 'application/json',
|
|
69
|
+
},
|
|
70
|
+
body,
|
|
71
|
+
});
|
|
72
|
+
if (response.status !== 200) {
|
|
73
|
+
throw new Error(`API rejected addToCart: ${response.status}`);
|
|
74
|
+
}
|
|
75
|
+
const responseData = await response.json();
|
|
76
|
+
if (responseData?.id) {
|
|
77
|
+
// cart Id should exist now.
|
|
78
|
+
return responseData;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
catch (err) {
|
|
82
|
+
console.error('bigcommerce/addToCart: Encountered an error!');
|
|
83
|
+
console.error(err);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
async function getExistingCartId() {
|
|
87
|
+
try {
|
|
88
|
+
const response = await fetch('/api/storefront/carts', {
|
|
89
|
+
method: 'GET',
|
|
90
|
+
headers: {
|
|
91
|
+
Accept: 'application/json',
|
|
92
|
+
'Content-Type': 'application/json',
|
|
93
|
+
},
|
|
94
|
+
});
|
|
95
|
+
const responseData = await response.json();
|
|
96
|
+
if (Array.isArray(responseData) && responseData.length) {
|
|
97
|
+
return responseData[0].id;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
catch (err) {
|
|
101
|
+
// error...
|
|
102
|
+
}
|
|
103
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { addToCart, BigCommerceAddToCartConfig as AddToCartConfig } from './addToCart';
|
|
2
|
+
export { pluginBigcommerceBackgroundFilters as pluginBackgroundFilters, PluginBigcommerceBackgroundFiltersConfig as PluginBackgroundFiltersConfig, } from './plugins/pluginBigcommerceBackgroundFilters';
|
|
3
|
+
export { pluginBigcommerceAddToCart as pluginAddToCart, PluginBigCommerceAddToCartConfig as PluginAddToCartConfig, } from './plugins/pluginBigcommerceAddToCart';
|
|
4
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../bigcommerce/src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,0BAA0B,IAAI,eAAe,EAAE,MAAM,aAAa,CAAC;AACvF,OAAO,EACN,kCAAkC,IAAI,uBAAuB,EAC7D,wCAAwC,IAAI,6BAA6B,GACzE,MAAM,8CAA8C,CAAC;AACtD,OAAO,EACN,0BAA0B,IAAI,eAAe,EAC7C,gCAAgC,IAAI,qBAAqB,GACzD,MAAM,sCAAsC,CAAC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { AbstractController } from '@athoscommerce/snap-controller';
|
|
2
|
+
import { BigCommerceAddToCartConfig } from '../addToCart';
|
|
3
|
+
import type { AbstractPluginConfig } from '../../../common/src/types';
|
|
4
|
+
export type PluginBigCommerceAddToCartConfig = AbstractPluginConfig & BigCommerceAddToCartConfig;
|
|
5
|
+
export declare const pluginBigcommerceAddToCart: (cntrlr: AbstractController, config?: PluginBigCommerceAddToCartConfig) => void;
|
|
6
|
+
//# sourceMappingURL=pluginBigcommerceAddToCart.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pluginBigcommerceAddToCart.d.ts","sourceRoot":"","sources":["../../../../../bigcommerce/src/plugins/pluginBigcommerceAddToCart.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,gCAAgC,CAAC;AAEzE,OAAO,EAAE,0BAA0B,EAAE,MAAM,cAAc,CAAC;AAE1D,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AAEtE,MAAM,MAAM,gCAAgC,GAAG,oBAAoB,GAAG,0BAA0B,CAAC;AAEjG,eAAO,MAAM,0BAA0B,WAAY,kBAAkB,WAAW,gCAAgC,SAU/G,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { addToCart as addToCartBigcommerceFunction } from '../addToCart';
|
|
2
|
+
export const pluginBigcommerceAddToCart = (cntrlr, config) => {
|
|
3
|
+
// do nothing if plugin is disabled
|
|
4
|
+
if (config?.enabled === false)
|
|
5
|
+
return;
|
|
6
|
+
const addToCartBigcommerceEvent = async ({ products }, next) => {
|
|
7
|
+
await addToCartBigcommerceFunction(products, config);
|
|
8
|
+
await next();
|
|
9
|
+
};
|
|
10
|
+
cntrlr.on('addToCart', addToCartBigcommerceEvent);
|
|
11
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { AbstractController } from '@athoscommerce/snap-controller';
|
|
2
|
+
import type { AbstractPluginConfig } from '../../../common/src/types';
|
|
3
|
+
export type PluginBigcommerceBackgroundFiltersConfig = AbstractPluginConfig & {
|
|
4
|
+
fieldNames?: {
|
|
5
|
+
brand?: string;
|
|
6
|
+
category?: string;
|
|
7
|
+
};
|
|
8
|
+
};
|
|
9
|
+
export declare const pluginBigcommerceBackgroundFilters: (cntrlr: AbstractController, config?: PluginBigcommerceBackgroundFiltersConfig) => void;
|
|
10
|
+
//# sourceMappingURL=pluginBigcommerceBackgroundFilters.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pluginBigcommerceBackgroundFilters.d.ts","sourceRoot":"","sources":["../../../../../bigcommerce/src/plugins/pluginBigcommerceBackgroundFilters.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,gCAAgC,CAAC;AAEzE,OAAO,KAAK,EAAE,oBAAoB,EAAgC,MAAM,2BAA2B,CAAC;AAEpG,MAAM,MAAM,wCAAwC,GAAG,oBAAoB,GAAG;IAC7E,UAAU,CAAC,EAAE;QACZ,KAAK,CAAC,EAAE,MAAM,CAAC;QACf,QAAQ,CAAC,EAAE,MAAM,CAAC;KAClB,CAAC;CACF,CAAC;AAEF,eAAO,MAAM,kCAAkC,WAAY,kBAAkB,WAAW,wCAAwC,SAwC/H,CAAC"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
export const pluginBigcommerceBackgroundFilters = (cntrlr, config) => {
|
|
2
|
+
// do nothing if plugin is disabled
|
|
3
|
+
if (config?.enabled === false)
|
|
4
|
+
return;
|
|
5
|
+
// only applies to search controllers
|
|
6
|
+
if (cntrlr.type != 'search')
|
|
7
|
+
return;
|
|
8
|
+
const backgroundFilters = [];
|
|
9
|
+
if (cntrlr.context?.category?.path) {
|
|
10
|
+
const categoryPath = replaceCharacters(cntrlr.context.category.path);
|
|
11
|
+
backgroundFilters.push({
|
|
12
|
+
type: 'value',
|
|
13
|
+
field: config?.fieldNames?.category || 'categories_hierarchy',
|
|
14
|
+
value: categoryPath,
|
|
15
|
+
background: true,
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
else if (cntrlr.context?.brand?.name) {
|
|
19
|
+
const brandName = replaceCharacters(cntrlr.context.brand.name);
|
|
20
|
+
backgroundFilters.push({
|
|
21
|
+
type: 'value',
|
|
22
|
+
field: config?.fieldNames?.brand || 'brand',
|
|
23
|
+
value: brandName,
|
|
24
|
+
background: true,
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
// don't do anything if there are no background filters found
|
|
28
|
+
if (!backgroundFilters.length) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
cntrlr.on('init', async ({ controller }, next) => {
|
|
32
|
+
controller.config = controller.config || {};
|
|
33
|
+
controller.config.globals = controller.config.globals || {};
|
|
34
|
+
controller.config.globals.filters = controller.config.globals.filters || [];
|
|
35
|
+
controller.config.globals.filters = controller.config.globals.filters.concat(backgroundFilters);
|
|
36
|
+
await next();
|
|
37
|
+
});
|
|
38
|
+
};
|
|
39
|
+
function replaceCharacters(value) {
|
|
40
|
+
if (value) {
|
|
41
|
+
return value
|
|
42
|
+
.replace(/\&\;/g, '&')
|
|
43
|
+
.replace(/\<\;/g, '<')
|
|
44
|
+
.replace(/\>\;/g, '>')
|
|
45
|
+
.replace(/\"\;/g, '"')
|
|
46
|
+
.replace(/\'\;/g, "'")
|
|
47
|
+
.replace(/\'\;/g, "'")
|
|
48
|
+
.trim();
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
return '';
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { pluginBackgroundFilters, PluginBackgroundFiltersConfig } from './plugins/pluginBackgroundFilters';
|
|
2
|
+
export { pluginScrollToTop, PluginScrollToTopConfig } from './plugins/pluginScrollToTop';
|
|
3
|
+
export { pluginLogger, PluginLoggerConfig } from './plugins/pluginLogger';
|
|
4
|
+
export { pluginAddToCart, PluginAddToCartConfig } from './plugins/pluginAddToCart';
|
|
5
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../common/src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,uBAAuB,EAAE,6BAA6B,EAAE,MAAM,mCAAmC,CAAC;AAC3G,OAAO,EAAE,iBAAiB,EAAE,uBAAuB,EAAE,MAAM,6BAA6B,CAAC;AACzF,OAAO,EAAE,YAAY,EAAE,kBAAkB,EAAE,MAAM,wBAAwB,CAAC;AAC1E,OAAO,EAAE,eAAe,EAAE,qBAAqB,EAAE,MAAM,2BAA2B,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Product } from '@athoscommerce/snap-store-mobx';
|
|
2
|
+
import { AbstractController } from '@athoscommerce/snap-controller';
|
|
3
|
+
import type { AbstractPluginConfig } from '../../../common/src/types';
|
|
4
|
+
export type PluginAddToCartConfig = AbstractPluginConfig & {
|
|
5
|
+
function?: (products: Product[], controller?: AbstractController) => void | Promise<void>;
|
|
6
|
+
};
|
|
7
|
+
export declare const pluginAddToCart: (cntrlr: AbstractController, config?: PluginAddToCartConfig) => void;
|
|
8
|
+
//# sourceMappingURL=pluginAddToCart.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pluginAddToCart.d.ts","sourceRoot":"","sources":["../../../../../common/src/plugins/pluginAddToCart.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,gCAAgC,CAAC;AAC9D,OAAO,EAAE,kBAAkB,EAAE,MAAM,gCAAgC,CAAC;AAEpE,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AAEtE,MAAM,MAAM,qBAAqB,GAAG,oBAAoB,GAAG;IAC1D,QAAQ,CAAC,EAAE,CAAC,QAAQ,EAAE,OAAO,EAAE,EAAE,UAAU,CAAC,EAAE,kBAAkB,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAC1F,CAAC;AAEF,eAAO,MAAM,eAAe,WAAY,kBAAkB,WAAW,qBAAqB,SAczF,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export const pluginAddToCart = (cntrlr, config) => {
|
|
2
|
+
// do nothing if plugin is disabled
|
|
3
|
+
if (config?.enabled === false)
|
|
4
|
+
return;
|
|
5
|
+
const addToCartEvent = async ({ controller, products }, next) => {
|
|
6
|
+
if (config?.function) {
|
|
7
|
+
await (config?.function && config.function(products, controller));
|
|
8
|
+
}
|
|
9
|
+
else {
|
|
10
|
+
cntrlr.log.error('common/addToCart: Error - No function provided in config!');
|
|
11
|
+
}
|
|
12
|
+
await next();
|
|
13
|
+
};
|
|
14
|
+
cntrlr.on('addToCart', addToCartEvent);
|
|
15
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { AbstractController } from '@athoscommerce/snap-controller';
|
|
2
|
+
import type { AbstractPluginConfig, PluginBackgroundFilter, PluginControl } from '../types';
|
|
3
|
+
export type PluginBackgroundFiltersConfig = AbstractPluginConfig & {
|
|
4
|
+
filters?: (PluginBackgroundFilter & PluginControl)[];
|
|
5
|
+
};
|
|
6
|
+
export declare const pluginBackgroundFilters: (cntrlr: AbstractController, config?: PluginBackgroundFiltersConfig) => void;
|
|
7
|
+
//# sourceMappingURL=pluginBackgroundFilters.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pluginBackgroundFilters.d.ts","sourceRoot":"","sources":["../../../../../common/src/plugins/pluginBackgroundFilters.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,gCAAgC,CAAC;AACzE,OAAO,KAAK,EAAE,oBAAoB,EAAE,sBAAsB,EAAgC,aAAa,EAAE,MAAM,UAAU,CAAC;AAE1H,MAAM,MAAM,6BAA6B,GAAG,oBAAoB,GAAG;IAClE,OAAO,CAAC,EAAE,CAAC,sBAAsB,GAAG,aAAa,CAAC,EAAE,CAAC;CACrD,CAAC;AAEF,eAAO,MAAM,uBAAuB,WAAY,kBAAkB,WAAW,6BAA6B,SA8DzG,CAAC"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
export const pluginBackgroundFilters = (cntrlr, config) => {
|
|
2
|
+
// do nothing if plugin is disabled
|
|
3
|
+
if (config?.enabled === false)
|
|
4
|
+
return;
|
|
5
|
+
const backgroundFilters = [];
|
|
6
|
+
let contextBackgroundFilters = [];
|
|
7
|
+
if (Array.isArray(cntrlr.context?.backgroundFilters)) {
|
|
8
|
+
contextBackgroundFilters = contextBackgroundFilters.concat(cntrlr.context.backgroundFilters);
|
|
9
|
+
}
|
|
10
|
+
else if (cntrlr.context?.backgroundFilters) {
|
|
11
|
+
cntrlr.log.warn('Context supplied backgroundFilters must be an array!');
|
|
12
|
+
}
|
|
13
|
+
const configBackgroundFilters = [...(config?.filters?.length ? config.filters : [])];
|
|
14
|
+
const combinedBackgroundFilters = contextBackgroundFilters.concat(configBackgroundFilters);
|
|
15
|
+
combinedBackgroundFilters.forEach((filter) => {
|
|
16
|
+
if (filter.field &&
|
|
17
|
+
filter.value &&
|
|
18
|
+
filter.type &&
|
|
19
|
+
((filter.type === 'value' && (typeof filter.value === 'string' || typeof filter.value === 'number')) ||
|
|
20
|
+
(filter.type === 'range' && typeof filter.value === 'object'))) {
|
|
21
|
+
// filter by controller type
|
|
22
|
+
if (Array.isArray(filter.controllerTypes) && !filter.controllerTypes.includes(cntrlr.type)) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
// filter by controller id
|
|
26
|
+
if (Array.isArray(filter.controllerIds) &&
|
|
27
|
+
!filter.controllerIds.includes(cntrlr.id) &&
|
|
28
|
+
!filter.controllerIds.some((id) => id instanceof RegExp && cntrlr.id.match(id))) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
backgroundFilters.push({
|
|
32
|
+
type: filter.type,
|
|
33
|
+
field: filter.field,
|
|
34
|
+
value: filter.value,
|
|
35
|
+
background: true,
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
cntrlr.log.error('Invalid filter in backgroundFilters: ', filter);
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
cntrlr.on('init', async ({ controller }, next) => {
|
|
43
|
+
if (backgroundFilters.length) {
|
|
44
|
+
controller.config = controller.config || {};
|
|
45
|
+
controller.config.globals = controller.config.globals || {};
|
|
46
|
+
controller.config.globals.filters = controller.config.globals.filters || [];
|
|
47
|
+
controller.config.globals.filters = controller.config.globals.filters.concat(removeDuplicateFilters(backgroundFilters));
|
|
48
|
+
}
|
|
49
|
+
await next();
|
|
50
|
+
});
|
|
51
|
+
};
|
|
52
|
+
function removeDuplicateFilters(filters) {
|
|
53
|
+
const seen = new Set();
|
|
54
|
+
return filters.filter((filter) => {
|
|
55
|
+
const filterValue = filter.type === 'range' ? `${filter.value?.low}:${filter.value?.high}` : filter.value;
|
|
56
|
+
const key = `${filter.type}:${filter.field}:${filterValue}`;
|
|
57
|
+
if (!seen.has(key)) {
|
|
58
|
+
seen.add(key);
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
61
|
+
return false;
|
|
62
|
+
});
|
|
63
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { AbstractController } from '@athoscommerce/snap-controller';
|
|
2
|
+
import { AbstractPluginConfig } from '../types';
|
|
3
|
+
export type PluginLoggerConfig = AbstractPluginConfig;
|
|
4
|
+
export declare const pluginLogger: (cntrlr: AbstractController, config?: PluginLoggerConfig) => void;
|
|
5
|
+
//# sourceMappingURL=pluginLogger.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pluginLogger.d.ts","sourceRoot":"","sources":["../../../../../common/src/plugins/pluginLogger.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,gCAAgC,CAAC;AACzE,OAAO,EAAE,oBAAoB,EAAE,MAAM,UAAU,CAAC;AAEhD,MAAM,MAAM,kBAAkB,GAAG,oBAAoB,CAAC;AAEtD,eAAO,MAAM,YAAY,WAAY,kBAAkB,WAAW,kBAAkB,SASnF,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export const pluginLogger = (cntrlr, config) => {
|
|
2
|
+
// do nothing if plugin is disabled
|
|
3
|
+
if (config?.enabled === false)
|
|
4
|
+
return;
|
|
5
|
+
cntrlr.on('afterStore', async ({ controller }, next) => {
|
|
6
|
+
controller.log.debug('store', controller.store.toJSON());
|
|
7
|
+
await next();
|
|
8
|
+
});
|
|
9
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { AbstractController } from '@athoscommerce/snap-controller';
|
|
2
|
+
import { AbstractPluginConfig } from '../types';
|
|
3
|
+
export type ScrollBehavior = 'auto' | 'instant' | 'smooth';
|
|
4
|
+
export type PluginScrollToTopConfig = {
|
|
5
|
+
selector?: string;
|
|
6
|
+
options?: {
|
|
7
|
+
top?: number;
|
|
8
|
+
left?: number;
|
|
9
|
+
behavior?: ScrollBehavior;
|
|
10
|
+
};
|
|
11
|
+
} & AbstractPluginConfig;
|
|
12
|
+
export declare const pluginScrollToTop: (cntrlr: AbstractController, config?: PluginScrollToTopConfig) => void;
|
|
13
|
+
//# sourceMappingURL=pluginScrollToTop.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pluginScrollToTop.d.ts","sourceRoot":"","sources":["../../../../../common/src/plugins/pluginScrollToTop.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAwC,MAAM,gCAAgC,CAAC;AAC/G,OAAO,EAAE,oBAAoB,EAAE,MAAM,UAAU,CAAC;AAEhD,MAAM,MAAM,cAAc,GAAG,MAAM,GAAG,SAAS,GAAG,QAAQ,CAAC;AAC3D,MAAM,MAAM,uBAAuB,GAAG;IACrC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,OAAO,CAAC,EAAE;QACT,GAAG,CAAC,EAAE,MAAM,CAAC;QACb,IAAI,CAAC,EAAE,MAAM,CAAC;QACd,QAAQ,CAAC,EAAE,cAAc,CAAC;KAC1B,CAAC;CACF,GAAG,oBAAoB,CAAC;AAEzB,eAAO,MAAM,iBAAiB,WAAY,kBAAkB,WAAW,uBAAuB,SA6B7F,CAAC"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export const pluginScrollToTop = (cntrlr, config) => {
|
|
2
|
+
// do nothing if plugin is disabled
|
|
3
|
+
if (config?.enabled === false)
|
|
4
|
+
return;
|
|
5
|
+
// only applies to search controllers
|
|
6
|
+
if (cntrlr.type != 'search')
|
|
7
|
+
return;
|
|
8
|
+
cntrlr.on('restorePosition', async ({ element }, next) => {
|
|
9
|
+
if (!element && !cntrlr.config.settings?.infinite?.enabled) {
|
|
10
|
+
const options = Object.assign({ top: 0, left: 0, behavior: 'smooth' }, config?.options || {});
|
|
11
|
+
if (config?.selector) {
|
|
12
|
+
const element = document.querySelector(config.selector);
|
|
13
|
+
if (element) {
|
|
14
|
+
const { top } = element.getBoundingClientRect();
|
|
15
|
+
options.top += top;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
setTimeout(() => {
|
|
19
|
+
window.scroll(options);
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
await next();
|
|
23
|
+
});
|
|
24
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { ControllerTypes } from '@athoscommerce/snap-controller';
|
|
2
|
+
type ValueBackgroundFilter = {
|
|
3
|
+
type: 'value';
|
|
4
|
+
field: string;
|
|
5
|
+
value: string | number;
|
|
6
|
+
};
|
|
7
|
+
type RangeBackgroundFilter = {
|
|
8
|
+
type: 'range';
|
|
9
|
+
field: string;
|
|
10
|
+
value: {
|
|
11
|
+
low: number;
|
|
12
|
+
high: number;
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
export type PluginBackgroundFilter = ValueBackgroundFilter | RangeBackgroundFilter;
|
|
16
|
+
export type PluginBackgroundFilterGlobal = PluginBackgroundFilter & {
|
|
17
|
+
background: true;
|
|
18
|
+
};
|
|
19
|
+
export type PluginControl = {
|
|
20
|
+
controllerIds?: (string | RegExp)[];
|
|
21
|
+
controllerTypes?: (ControllerTypes | keyof typeof ControllerTypes)[];
|
|
22
|
+
};
|
|
23
|
+
export type AbstractPluginConfig = {
|
|
24
|
+
enabled?: boolean;
|
|
25
|
+
};
|
|
26
|
+
export {};
|
|
27
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../../common/src/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,gCAAgC,CAAC;AAEjE,KAAK,qBAAqB,GAAG;IAC5B,IAAI,EAAE,OAAO,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,MAAM,GAAG,MAAM,CAAC;CACvB,CAAC;AAEF,KAAK,qBAAqB,GAAG;IAC5B,IAAI,EAAE,OAAO,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC;CACrC,CAAC;AAEF,MAAM,MAAM,sBAAsB,GAAG,qBAAqB,GAAG,qBAAqB,CAAC;AACnF,MAAM,MAAM,4BAA4B,GAAG,sBAAsB,GAAG;IAAE,UAAU,EAAE,IAAI,CAAA;CAAE,CAAC;AACzF,MAAM,MAAM,aAAa,GAAG;IAC3B,aAAa,CAAC,EAAE,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,CAAC;IACpC,eAAe,CAAC,EAAE,CAAC,eAAe,GAAG,MAAM,OAAO,eAAe,CAAC,EAAE,CAAC;CACrE,CAAC;AAEF,MAAM,MAAM,oBAAoB,GAAG;IAClC,OAAO,CAAC,EAAE,OAAO,CAAC;CAClB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Product } from '@athoscommerce/snap-store-mobx';
|
|
2
|
+
export type Magento2AddToCartConfig = {
|
|
3
|
+
formKey?: string;
|
|
4
|
+
uenc?: string;
|
|
5
|
+
redirect?: boolean | string;
|
|
6
|
+
idFieldName?: string;
|
|
7
|
+
};
|
|
8
|
+
export declare const addToCart: (data: Product[], config?: Magento2AddToCartConfig) => Promise<void>;
|
|
9
|
+
//# sourceMappingURL=addToCart.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"addToCart.d.ts","sourceRoot":"","sources":["../../../../magento2/src/addToCart.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,gCAAgC,CAAC;AAI9D,MAAM,MAAM,uBAAuB,GAAG;IACrC,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,OAAO,GAAG,MAAM,CAAC;IAC5B,WAAW,CAAC,EAAE,MAAM,CAAC;CACrB,CAAC;AAQF,eAAO,MAAM,SAAS,SAAgB,OAAO,EAAE,WAAW,uBAAuB,kBAuFhF,CAAC"}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { getFormKey } from './getFormKey';
|
|
2
|
+
import { getUenc } from './getUenc';
|
|
3
|
+
export const addToCart = async (data, config) => {
|
|
4
|
+
if (!data) {
|
|
5
|
+
console.error('magento2/addToCart: No products to add!');
|
|
6
|
+
return;
|
|
7
|
+
}
|
|
8
|
+
const form_key = config?.formKey || getFormKey();
|
|
9
|
+
const uenc = config?.uenc || getUenc();
|
|
10
|
+
const lineItems = [];
|
|
11
|
+
data.map(async (item) => {
|
|
12
|
+
let sku = item?.display?.mappings.core?.uid;
|
|
13
|
+
if (config?.idFieldName) {
|
|
14
|
+
let level = item;
|
|
15
|
+
config.idFieldName.split('.').map((field) => {
|
|
16
|
+
if (level[field]) {
|
|
17
|
+
level = level[field];
|
|
18
|
+
}
|
|
19
|
+
else {
|
|
20
|
+
console.error(`magento2/addToCart: Could not find column in item data. Please verify 'idFieldName' in the config.`);
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
if (level && level !== item) {
|
|
25
|
+
sku = level;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
if (sku && item.quantity) {
|
|
29
|
+
const attributes = [];
|
|
30
|
+
const options = item.variants?.active?.options;
|
|
31
|
+
if (options) {
|
|
32
|
+
Object.keys(options).forEach((option) => {
|
|
33
|
+
const attrId = options[option].attributeId;
|
|
34
|
+
const optionId = options[option].optionId;
|
|
35
|
+
const attributeObj = {
|
|
36
|
+
name: attrId,
|
|
37
|
+
val: optionId,
|
|
38
|
+
};
|
|
39
|
+
attributes.push(attributeObj);
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
lineItems.push({
|
|
43
|
+
product_id: sku,
|
|
44
|
+
quantity: item.quantity,
|
|
45
|
+
attributes: attributes,
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
if (lineItems.length) {
|
|
50
|
+
for (let i = 0; i < lineItems.length; i++) {
|
|
51
|
+
const itemData = lineItems[i];
|
|
52
|
+
const quantity = itemData.quantity || 1;
|
|
53
|
+
const form = new FormData();
|
|
54
|
+
form.append('product', itemData.product_id);
|
|
55
|
+
form.append('form_key', form_key || '');
|
|
56
|
+
form.append('uenc', uenc || '');
|
|
57
|
+
form.append('qty', quantity.toString());
|
|
58
|
+
itemData.attributes.forEach((att) => {
|
|
59
|
+
form.append(`super_attribute[${att.name}]`, att.val);
|
|
60
|
+
});
|
|
61
|
+
try {
|
|
62
|
+
const response = await fetch(window.location.origin + '/checkout/cart/add/uenc/' + uenc + '/product/' + itemData.product_id + '/addon_product/1/', {
|
|
63
|
+
method: 'POST',
|
|
64
|
+
body: form,
|
|
65
|
+
});
|
|
66
|
+
if (response.status !== 200) {
|
|
67
|
+
throw new Error(`API rejected addToCart: ${response.status}`);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
catch (err) {
|
|
71
|
+
console.error('magento2/addToCart: Encountered an error!');
|
|
72
|
+
console.error(err);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
// do redirect (or not)
|
|
76
|
+
if (config?.redirect !== false) {
|
|
77
|
+
setTimeout(() => (window.location.href = typeof config?.redirect == 'string' ? config?.redirect : '/checkout/cart/'));
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getFormKey.d.ts","sourceRoot":"","sources":["../../../../magento2/src/getFormKey.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,UAAU,cAGtB,CAAC"}
|