Skip to content

Commit f7e7ac6

Browse files
authored
Merge branch 'main' into LYNX-722
2 parents 019378b + 37d4167 commit f7e7ac6

18 files changed

+440
-279
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
function rulesMatched(activeRules, rules) {
2+
return activeRules && rules.some((rule) => activeRules.includes(rule));
3+
}
4+
5+
const groupMatched = (activeGroup, groups) => groups.includes(activeGroup);
6+
7+
export default function conditionsMatched(activeRules, blockConfig) {
8+
const {
9+
'customer-segments': customerSegments,
10+
'customer-groups': customerGroups,
11+
'cart-rules': cartRules,
12+
'catalog-price-rules': catalogPriceRules,
13+
} = blockConfig;
14+
15+
const activeSegments = activeRules.customerSegments?.map(
16+
(segment) => segment.name,
17+
);
18+
const activeGroup = activeRules.customerGroup?.name;
19+
const activeCartRules = activeRules.cart?.rules?.map(
20+
(rule) => rule.name,
21+
);
22+
const activePriceRules = activeRules.catalogPriceRules?.rules?.map(
23+
(rule) => rule.name,
24+
);
25+
26+
if (customerSegments !== undefined && !rulesMatched(activeSegments, customerSegments.split(','))) {
27+
return false;
28+
}
29+
30+
if (customerGroups !== undefined && !groupMatched(activeGroup, customerGroups.split(','))) {
31+
return false;
32+
}
33+
34+
if (cartRules !== undefined && !rulesMatched(activeCartRules, cartRules.split(','))) {
35+
return false;
36+
}
37+
38+
if (catalogPriceRules !== undefined && !rulesMatched(activePriceRules, catalogPriceRules.split(','))) {
39+
return false;
40+
}
41+
42+
return true;
43+
}

blocks/targeted-block/qraphql.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { fetchGraphQl, setFetchGraphQlHeaders } from '@dropins/tools/fetch-graphql.js';
2+
import { getHeaders } from '../../scripts/configs.js';
3+
4+
export const getActiveRules = async (cartId) => {
5+
try {
6+
setFetchGraphQlHeaders(await getHeaders('cart'));
7+
const response = await fetchGraphQl(
8+
`query CUSTOMER_SEGMENTS($cartId: String!){
9+
customerSegments(cartId: $cartId) {
10+
name
11+
}
12+
customerGroup {
13+
name
14+
}
15+
cart(cart_id: $cartId) {
16+
rules {
17+
name
18+
}
19+
}
20+
}
21+
`,
22+
{
23+
method: 'GET',
24+
variables: { cartId },
25+
},
26+
);
27+
return response.data;
28+
} catch (error) {
29+
console.error('Could not retrieve customer segments', error);
30+
}
31+
return [];
32+
};
33+
34+
export const getCatalogPriceRules = async (sku) => {
35+
try {
36+
const query = `query CATALOG_PRICE_RULES($sku: String!) {
37+
products(filter: {
38+
sku: {
39+
eq: $sku
40+
}
41+
})
42+
{
43+
items {
44+
rules {
45+
name
46+
}
47+
}
48+
}
49+
}
50+
`;
51+
setFetchGraphQlHeaders(await getHeaders('cart'));
52+
const response = await fetchGraphQl(
53+
query,
54+
{
55+
method: 'GET',
56+
variables: { sku },
57+
},
58+
);
59+
return response.data?.products?.items[0];
60+
} catch (error) {
61+
console.error(`Could not retrieve catalog rules for ${sku}`, error);
62+
}
63+
return [];
64+
};
Lines changed: 13 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,38 @@
11
import { events } from '@dropins/tools/event-bus.js';
22
import * as Cart from '@dropins/storefront-cart/api.js';
3-
import { fetchGraphQl } from '@dropins/tools/fetch-graphql.js';
3+
import { getActiveRules, getCatalogPriceRules } from './qraphql.js';
4+
import conditionsMatched from './condition-matcher.js';
45
import { readBlockConfig } from '../../scripts/aem.js';
56
import { loadFragment } from '../fragment/fragment.js';
67

78
const blocks = [];
89
const displayedBlockTypes = [];
910

10-
const getActiveRules = async (cartId) => {
11-
try {
12-
const response = await fetchGraphQl(
13-
`query CUSTOMER_SEGMENTS($cartId: String!){
14-
customerSegments(cartId: $cartId) {
15-
name
16-
}
17-
customerGroup {
18-
name
19-
}
20-
cart(cart_id: $cartId) {
21-
rules {
22-
name
23-
}
24-
}
25-
}
26-
`,
27-
{
28-
method: 'GET',
29-
variables: { cartId },
30-
},
31-
);
32-
return response.data;
33-
} catch (error) {
34-
console.error('Could not retrieve customer segments', error);
35-
}
36-
return [];
37-
};
38-
39-
const segmentsMatched = (activeSegments, segments) => segments.filter(
40-
(segment) => (activeSegments.includes(segment)),
41-
).length >= 1;
42-
43-
const groupMatched = (activeGroup, groups) => groups.includes(activeGroup);
44-
45-
const cartRulesMatched = (activeRules, rules) => rules.filter(
46-
(rule) => (activeRules.includes(rule)),
47-
).length >= 1;
48-
49-
const conditionsMatched = (activeRules, blockConfig) => {
50-
const {
51-
'customer-segments': customerSegments,
52-
'customer-groups': customerGroups,
53-
'cart-rules': cartRules,
54-
} = blockConfig;
55-
56-
const activeSegments = activeRules.customerSegments?.map(
57-
(segment) => segment.name,
58-
);
59-
const activeGroup = activeRules.customerGroup?.name;
60-
const activeCartRules = activeRules.cart?.rules?.map(
61-
(rule) => rule.name,
62-
);
63-
if (customerSegments !== undefined && !segmentsMatched(activeSegments, customerSegments.split(','))) {
64-
return false;
65-
}
66-
67-
if (customerGroups !== undefined && !groupMatched(activeGroup, customerGroups.split(','))) {
68-
return false;
69-
}
70-
71-
if (cartRules !== undefined && !cartRulesMatched(activeCartRules, cartRules.split(','))) {
72-
return false;
73-
}
74-
75-
return true;
76-
};
77-
7811
const updateTargetedBlocksVisibility = async () => {
7912
const activeRules = (Cart.getCartDataFromCache() === null) ? {
8013
customerSegments: [],
8114
customerGroup: [],
8215
cart: {
8316
rules: [],
8417
},
18+
catalogPriceRules: [],
8519
} : await getActiveRules(Cart.getCartDataFromCache().id);
8620

21+
// eslint-disable-next-line no-underscore-dangle
22+
const productData = events._lastEvent?.['pdp/data']?.payload ?? null;
23+
24+
if (productData?.sku) {
25+
activeRules.catalogPriceRules = await getCatalogPriceRules(productData.sku);
26+
}
27+
8728
displayedBlockTypes.length = 0;
8829
blocks.forEach(async (blockConfig) => {
8930
const index = blocks.indexOf(blockConfig);
9031
const { fragment, type } = blockConfig;
32+
9133
const block = document.querySelector(`[data-targeted-block-key="${index}"]`);
9234
block.style.display = 'none';
35+
9336
if (!displayedBlockTypes.includes(type) && conditionsMatched(activeRules, blockConfig)) {
9437
displayedBlockTypes.push(type);
9538
if (fragment !== undefined) {
@@ -113,6 +56,7 @@ export default function decorate(block) {
11356
events.on('cart/initialized', () => {
11457
updateTargetedBlocksVisibility();
11558
}, { eager: true });
59+
11660
events.on('cart/updated', () => {
11761
updateTargetedBlocksVisibility();
11862
}, { eager: true });

package-lock.json

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@
3737
"@adobe/magento-storefront-events-sdk": "^1.8.0",
3838
"@dropins/storefront-account": "~1.0.3",
3939
"@dropins/storefront-auth": "~1.0.3",
40-
"@dropins/storefront-cart": "~1.0.2",
41-
"@dropins/storefront-checkout": "~1.1.0",
40+
"@dropins/storefront-cart": "~1.0.3",
41+
"@dropins/storefront-checkout": "~1.2.0",
4242
"@dropins/storefront-order": "~1.0.4",
4343
"@dropins/storefront-payment-services": "~1.0.1",
4444
"@dropins/storefront-pdp": "~1.0.0",
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export { CART_FRAGMENT } from './graphql/CartFragment';
22
export { CART_ITEM_FRAGMENT } from './graphql/CartItemFragment';
3+
export { DOWNLOADABLE_CART_ITEMS_FRAGMENT } from './graphql/DownloadableCartItemsFragment';
34
//# sourceMappingURL=fragments.d.ts.map
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,18 @@
1+
/********************************************************************
2+
* ADOBE CONFIDENTIAL
3+
* __________________
4+
*
5+
* Copyright 2024 Adobe
6+
* All Rights Reserved.
7+
*
8+
* NOTICE: All information contained herein is, and remains
9+
* the property of Adobe and its suppliers, if any. The intellectual
10+
* and technical concepts contained herein are proprietary to Adobe
11+
* and its suppliers and are protected by all applicable intellectual
12+
* property laws, including trade secret and copyright laws.
13+
* Dissemination of this information or reproduction of this material
14+
* is strictly forbidden unless prior written permission is obtained
15+
* from Adobe.
16+
*******************************************************************/
117
export declare const CART_ITEM_FRAGMENT: string;
218
//# sourceMappingURL=CartItemFragment.d.ts.map
Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,18 @@
1-
export declare const CUSTOMER_ACCOUNT_FRAGMENT = "\nfragment CUSTOMER_FRAGMENT on Customer {\n addresses {\n default_shipping\n country_code\n postcode\n region {\n region\n region_code\n region_id\n }\n }\n}";
1+
/********************************************************************
2+
* ADOBE CONFIDENTIAL
3+
* __________________
4+
*
5+
* Copyright 2024 Adobe
6+
* All Rights Reserved.
7+
*
8+
* NOTICE: All information contained herein is, and remains
9+
* the property of Adobe and its suppliers, if any. The intellectual
10+
* and technical concepts contained herein are proprietary to Adobe
11+
* and its suppliers and are protected by all applicable intellectual
12+
* property laws, including trade secret and copyright laws.
13+
* Dissemination of this information or reproduction of this material
14+
* is strictly forbidden unless prior written permission is obtained
15+
* from Adobe.
16+
*******************************************************************/
17+
export declare const CUSTOMER_ACCOUNT_FRAGMENT = "\n fragment CUSTOMER_FRAGMENT on Customer {\n addresses {\n default_shipping\n country_code\n postcode\n region {\n region\n region_code\n region_id\n }\n }\n }\n";
218
//# sourceMappingURL=CustomerAccountFragment.d.ts.map
Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,18 @@
1-
export declare const CUSTOMIZABLE_OPTIONS_FRAGMENT = "\n fragment CUSTOMIZABLE_OPTIONS_FRAGMENT on SelectedCustomizableOption {\n type\n customizable_option_uid\n label\n is_required\n values {\n label\n value\n price{\n type\n units\n value\n }\n }\n }\n";
1+
/********************************************************************
2+
* ADOBE CONFIDENTIAL
3+
* __________________
4+
*
5+
* Copyright 2024 Adobe
6+
* All Rights Reserved.
7+
*
8+
* NOTICE: All information contained herein is, and remains
9+
* the property of Adobe and its suppliers, if any. The intellectual
10+
* and technical concepts contained herein are proprietary to Adobe
11+
* and its suppliers and are protected by all applicable intellectual
12+
* property laws, including trade secret and copyright laws.
13+
* Dissemination of this information or reproduction of this material
14+
* is strictly forbidden unless prior written permission is obtained
15+
* from Adobe.
16+
*******************************************************************/
17+
export declare const CUSTOMIZABLE_OPTIONS_FRAGMENT = "\n fragment CUSTOMIZABLE_OPTIONS_FRAGMENT on SelectedCustomizableOption {\n type\n customizable_option_uid\n label\n is_required\n values {\n label\n value\n price {\n type\n units\n value\n }\n }\n }\n";
218
//# sourceMappingURL=CustomizableOptionsFragment.d.ts.map
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/********************************************************************
2+
* ADOBE CONFIDENTIAL
3+
* __________________
4+
*
5+
* Copyright 2024 Adobe
6+
* All Rights Reserved.
7+
*
8+
* NOTICE: All information contained herein is, and remains
9+
* the property of Adobe and its suppliers, if any. The intellectual
10+
* and technical concepts contained herein are proprietary to Adobe
11+
* and its suppliers and are protected by all applicable intellectual
12+
* property laws, including trade secret and copyright laws.
13+
* Dissemination of this information or reproduction of this material
14+
* is strictly forbidden unless prior written permission is obtained
15+
* from Adobe.
16+
*******************************************************************/
17+
export declare const DOWNLOADABLE_CART_ITEMS_FRAGMENT = "\n fragment DOWNLOADABLE_CART_ITEMS_FRAGMENT on DownloadableCartItem {\n links {\n sort_order\n title\n }\n customizable_options {\n ...CUSTOMIZABLE_OPTIONS_FRAGMENT\n }\n }\n";
18+
//# sourceMappingURL=DownloadableCartItemsFragment.d.ts.map
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,18 @@
1+
/********************************************************************
2+
* ADOBE CONFIDENTIAL
3+
* __________________
4+
*
5+
* Copyright 2024 Adobe
6+
* All Rights Reserved.
7+
*
8+
* NOTICE: All information contained herein is, and remains
9+
* the property of Adobe and its suppliers, if any. The intellectual
10+
* and technical concepts contained herein are proprietary to Adobe
11+
* and its suppliers and are protected by all applicable intellectual
12+
* property laws, including trade secret and copyright laws.
13+
* Dissemination of this information or reproduction of this material
14+
* is strictly forbidden unless prior written permission is obtained
15+
* from Adobe.
16+
*******************************************************************/
117
export declare const PRICE_RANGE_FRAGMENT = "\n fragment PRICE_RANGE_FRAGMENT on PriceRange {\n minimum_price {\n regular_price {\n value\n currency\n }\n final_price {\n value\n currency\n }\n discount {\n percent_off\n amount_off\n }\n }\n maximum_price {\n regular_price {\n value\n currency\n }\n final_price {\n value\n currency\n }\n discount {\n percent_off\n amount_off\n }\n }\n }\n";
218
//# sourceMappingURL=PriceRangeFragment.d.ts.map
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,18 @@
1+
/********************************************************************
2+
* ADOBE CONFIDENTIAL
3+
* __________________
4+
*
5+
* Copyright 2024 Adobe
6+
* All Rights Reserved.
7+
*
8+
* NOTICE: All information contained herein is, and remains
9+
* the property of Adobe and its suppliers, if any. The intellectual
10+
* and technical concepts contained herein are proprietary to Adobe
11+
* and its suppliers and are protected by all applicable intellectual
12+
* property laws, including trade secret and copyright laws.
13+
* Dissemination of this information or reproduction of this material
14+
* is strictly forbidden unless prior written permission is obtained
15+
* from Adobe.
16+
*******************************************************************/
117
export declare const CART_ITEMS_PAGINATION_ARGUMENTS = "\n $pageSize: Int! = 100,\n $currentPage: Int! = 1,\n $itemsSortInput: QuoteItemsSortInput! = {field: CREATED_AT, order: DESC}\n";
218
//# sourceMappingURL=arguments.d.ts.map

0 commit comments

Comments
 (0)