Skip to content

Commit

Permalink
LYNX-759: Conditionally query for segments and cart rules
Browse files Browse the repository at this point in the history
  • Loading branch information
bl4de committed Feb 13, 2025
1 parent ed9cf76 commit 5d3444c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
7 changes: 5 additions & 2 deletions blocks/targeted-block/graphql.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ const getCartRules = async (cartId) => {
try {
// setFetchGraphQlHeaders(await getHeaders('cart'));
const response = await fetchGraphQl(
`query CART_RULES($cartId: String!){
`query CUSTOMER_SEGMENTS($cartId: String!){
customerSegments(cartId: $cartId) {
name
}
cart(cart_id: $cartId) {
rules {
name
Expand All @@ -62,7 +65,7 @@ const getCartRules = async (cartId) => {
variables: { cartId },
},
);
return response.data?.cart?.rules || [];
return response.data || [];
} catch (error) {
console.error('Could not retrieve customer cart rules', error);
}
Expand Down
16 changes: 13 additions & 3 deletions blocks/targeted-block/targeted-block.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,30 @@ import {
import conditionsMatched from './condition-matcher.js';
import { readBlockConfig } from '../../scripts/aem.js';
import { loadFragment } from '../fragment/fragment.js';
import { getUserTokenCookie } from '../../scripts/initializers/index.js';

const blocks = [];
const displayedBlockTypes = [];

const updateTargetedBlocksVisibility = async () => {
const activeRules = {
customerSegments: await getCustomerSegments(),
customerSegments: [],
customerGroup: await getCustomerGroups(),
cart: [],
catalogPriceRules: [],
};

if (Cart.getCartDataFromCache() !== null) {
activeRules.cart = await getCartRules(Cart.getCartDataFromCache().id);
if (getUserTokenCookie()) {
if (Cart.getCartDataFromCache() === null) {
activeRules.customerSegments = await getCustomerSegments();
} else {
const cartId = Cart.getCartDataFromCache().id;
if (cartId) {
const response = await getCartRules(cartId);
activeRules.cart = response.cart?.rules || [];
activeRules.customerSegments = response.customerSegments || [];
}
}
}

// eslint-disable-next-line no-underscore-dangle
Expand Down

0 comments on commit 5d3444c

Please sign in to comment.