@ordergroove/offers 2.34.1 → 2.34.2-alpha-PR-742-3.8
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/bundle-report.html +6 -4
- package/dist/offers.js +30 -30
- package/dist/offers.js.map +3 -3
- package/package.json +2 -2
- package/src/index.js +5 -1
- package/src/shopify/__tests__/makeApiSellingPlans.spec.js +56 -0
- package/src/shopify/makeApiSellingPlans.js +34 -0
- package/src/shopify/shopifyMiddleware.ts +2 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ordergroove/offers",
|
|
3
|
-
"version": "2.34.
|
|
3
|
+
"version": "2.34.2-alpha-PR-742-3.8+6864c475",
|
|
4
4
|
"description": "offer state component",
|
|
5
5
|
"author": "Eugenio Lattanzio <eugenio63@gmail.com>",
|
|
6
6
|
"homepage": "https://github.com/ordergroove/plush-toys#readme",
|
|
@@ -47,5 +47,5 @@
|
|
|
47
47
|
"devDependencies": {
|
|
48
48
|
"@ordergroove/offers-templates": "^0.9.1"
|
|
49
49
|
},
|
|
50
|
-
"gitHead": "
|
|
50
|
+
"gitHead": "6864c47538a402aa8ddd4027ba3f23b5592187d6"
|
|
51
51
|
}
|
package/src/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { makeStore } from './core/store';
|
|
2
2
|
import makeApi from './make-api';
|
|
3
|
+
import makeApiSellingPlans from './shopify/makeApiSellingPlans';
|
|
3
4
|
import defaultReducer from './core/reducer';
|
|
4
5
|
import shopifyReducer from './shopify/shopifyReducer';
|
|
5
6
|
import shopifyMiddleware from './shopify/shopifyMiddleware';
|
|
@@ -13,7 +14,10 @@ export const store = makeStore(
|
|
|
13
14
|
platform.shopify && shopifyTrackingMiddleware
|
|
14
15
|
);
|
|
15
16
|
|
|
16
|
-
export const offers =
|
|
17
|
+
export const offers = Object.assign(
|
|
18
|
+
makeApi(store),
|
|
19
|
+
platform?.shopify && platform?.shopify_selling_plans ? makeApiSellingPlans() : {}
|
|
20
|
+
);
|
|
17
21
|
|
|
18
22
|
export const isReady = offers.isReady;
|
|
19
23
|
export const addOptinChangedCallback = offers.addOptinChangedCallback;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import fetchMock from 'fetch-mock';
|
|
2
|
+
|
|
3
|
+
import makeApiSellingPlans from '../makeApiSellingPlans';
|
|
4
|
+
import { CART_JS_URL } from '../shopifyMiddleware';
|
|
5
|
+
|
|
6
|
+
describe('getOptins', () => {
|
|
7
|
+
beforeEach(() => {
|
|
8
|
+
const cart = {
|
|
9
|
+
items: [
|
|
10
|
+
{ variant_id: 123, selling_plan_allocation: { selling_plan: { id: 111 } } },
|
|
11
|
+
{ product_id: 456, selling_plan_allocation: { selling_plan: { id: 222 } } },
|
|
12
|
+
{ variant_id: 789, selling_plan_allocation: { selling_plan: { id: 333 } } }
|
|
13
|
+
]
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
fetchMock.getOnce(CART_JS_URL, cart);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
afterEach(() => {
|
|
20
|
+
fetchMock.restore();
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('should return all optins given no productIds are provided', async () => {
|
|
24
|
+
const expectedOptins = [
|
|
25
|
+
{ product: 123, selling_plan: 111 },
|
|
26
|
+
{ product: 456, selling_plan: 222 },
|
|
27
|
+
{ product: 789, selling_plan: 333 }
|
|
28
|
+
];
|
|
29
|
+
const shopifySellingPlanOffers = makeApiSellingPlans();
|
|
30
|
+
const optins = await shopifySellingPlanOffers.getOptins();
|
|
31
|
+
|
|
32
|
+
expect(optins).toEqual(expectedOptins);
|
|
33
|
+
expect(fetchMock.called(CART_JS_URL)).toBe(true);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('should return optins for productIds given existant productIds are provided', async () => {
|
|
37
|
+
const expected = [
|
|
38
|
+
{ product: 123, selling_plan: 111 },
|
|
39
|
+
{ product: 456, selling_plan: 222 }
|
|
40
|
+
];
|
|
41
|
+
const shopifySellingPlanOffers = makeApiSellingPlans();
|
|
42
|
+
const optins = await shopifySellingPlanOffers.getOptins([123, 456]);
|
|
43
|
+
|
|
44
|
+
expect(optins).toEqual(expected);
|
|
45
|
+
expect(fetchMock.called(CART_JS_URL)).toBe(true);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('should return an empty array given nonexistant productIDs are provided', async () => {
|
|
49
|
+
const expected = [];
|
|
50
|
+
const shopifySellingPlanOffers = makeApiSellingPlans();
|
|
51
|
+
const optins = await shopifySellingPlanOffers.getOptins([888, 999]);
|
|
52
|
+
|
|
53
|
+
expect(optins).toEqual(expected);
|
|
54
|
+
expect(fetchMock.called(CART_JS_URL)).toBe(true);
|
|
55
|
+
});
|
|
56
|
+
});
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { getCart } from './shopifyMiddleware';
|
|
2
|
+
|
|
3
|
+
export default function makeApiSellingPlans() {
|
|
4
|
+
const sellingPlanOffers = {
|
|
5
|
+
async getOptins(productIds = []) {
|
|
6
|
+
const cart = await getCart();
|
|
7
|
+
const { items } = cart;
|
|
8
|
+
|
|
9
|
+
const optins = items.reduce((result, item) => {
|
|
10
|
+
const product = item.variant_id || item.product_id;
|
|
11
|
+
const sellingPlan = item.selling_plan_allocation?.selling_plan?.id;
|
|
12
|
+
|
|
13
|
+
if (productIds.length === 0 || productIds.includes(product)) {
|
|
14
|
+
result.push({ product, selling_plan: sellingPlan });
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return result;
|
|
18
|
+
}, []);
|
|
19
|
+
|
|
20
|
+
return optins;
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
window.OG = window.OG || {};
|
|
25
|
+
const offers = window.OG.offers || {};
|
|
26
|
+
|
|
27
|
+
Object.assign(window.OG, {
|
|
28
|
+
offers: {
|
|
29
|
+
...offers,
|
|
30
|
+
...sellingPlanOffers
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
return sellingPlanOffers;
|
|
34
|
+
}
|
|
@@ -20,7 +20,7 @@ import { getTrackingKey } from './shopifyTrackingMiddleware';
|
|
|
20
20
|
|
|
21
21
|
const SHOPIFY_ROOT = window.Shopify?.routes?.root || '/';
|
|
22
22
|
const CART_PAGE_URL = '/cart';
|
|
23
|
-
const CART_JS_URL = `${SHOPIFY_ROOT}cart.js`;
|
|
23
|
+
export const CART_JS_URL = `${SHOPIFY_ROOT}cart.js`;
|
|
24
24
|
const PRODUCTS_URL = `${SHOPIFY_ROOT}products/`;
|
|
25
25
|
|
|
26
26
|
/**
|
|
@@ -79,7 +79,7 @@ async function setupPdp(store, offer) {
|
|
|
79
79
|
}
|
|
80
80
|
}
|
|
81
81
|
|
|
82
|
-
const getCart = async () => await (await fetch(CART_JS_URL)).json();
|
|
82
|
+
export const getCart = async () => await (await fetch(CART_JS_URL)).json();
|
|
83
83
|
|
|
84
84
|
/**
|
|
85
85
|
* Attemps to guess the product handle o
|