@@ -90,30 +90,23 @@ async function loadRecommendation(block, context) {
90
90
// Only proceed if all required data is available
91
91
if ( ! context . pageType
92
92
|| ( context . pageType === 'Product' && ! context . currentSku )
93
- || ( context . pageType === 'Category' && ! context . category ) ) {
93
+ || ( context . pageType === 'Category' && ! context . category )
94
+ || ( context . pageType === 'Cart' && ! context . cartSkus ) ) {
94
95
return ;
95
96
}
96
97
97
98
if ( recommendationsPromise ) {
98
99
return ;
99
100
}
100
101
101
- // Get user view history
102
- // TODO
103
- /* let productViews = window
104
- .adobeDataLayer.getState('productContext', [-10, 0], { flatten: false }) || [];
105
- if (!Array.isArray(productViews) && productViews) {
106
- productViews = [productViews];
102
+ // Get product view history
103
+ try {
104
+ const viewHistory = window . sessionStorage . getItem ( 'productViewHistory' ) || '[]' ;
105
+ context . userViewHistory = JSON . parse ( viewHistory ) . map ( ( sku ) => ( { sku } ) ) ;
106
+ } catch ( e ) {
107
+ window . sessionStorage . removeItem ( 'productViewHistory' ) ;
108
+ console . error ( 'Error parsing product view history' , e ) ;
107
109
}
108
- context.userViewHistory = productViews
109
- .map(({ sku }) => ({ sku }))
110
- .reduce((acc, current) => {
111
- const x = acc.find((p) => p.sku === current.sku);
112
- if (!x) {
113
- return acc.concat([current]);
114
- }
115
- return acc;
116
- }, []); */
117
110
118
111
recommendationsPromise = performCatalogServiceQuery ( recommendationsQuery , context ) ;
119
112
const { recommendations } = await recommendationsPromise ;
@@ -140,9 +133,15 @@ export default async function decorate(block) {
140
133
loadRecommendation ( block , context ) ;
141
134
}
142
135
136
+ function handleCartChanges ( { shoppingCartContext } ) {
137
+ context . cartSkus = shoppingCartContext . items . map ( ( { product } ) => product . sku ) ;
138
+ loadRecommendation ( block , context ) ;
139
+ }
140
+
143
141
window . adobeDataLayer . push ( ( dl ) => {
144
142
dl . addEventListener ( 'adobeDataLayer:change' , handlePageTypeChanges , { path : 'pageContext' } ) ;
145
143
dl . addEventListener ( 'adobeDataLayer:change' , handleProductChanges , { path : 'productContext' } ) ;
146
144
dl . addEventListener ( 'adobeDataLayer:change' , handleCategoryChanges , { path : 'categoryContext' } ) ;
145
+ dl . addEventListener ( 'adobeDataLayer:change' , handleCartChanges , { path : 'shoppingCartContext' } ) ;
147
146
} ) ;
148
147
}
0 commit comments