@@ -148,61 +148,42 @@ export const getFormattedCart = (data: IFormattedCartProps) => {
148148 }
149149 const givenProducts = data . cart . contents . nodes ;
150150
151- // Create an empty object.
152- formattedCart . products = [ ] ;
153-
154- const product : Product = {
155- productId : 0 ,
156- cartKey : '' ,
157- name : '' ,
158- qty : 0 ,
159- price : 0 ,
160- totalPrice : '0' ,
161- image : { sourceUrl : '' , srcSet : '' , title : '' } ,
162- } ;
163-
164151 let totalProductsCount = 0 ;
165- let i = 0 ;
166152
167153 if ( ! givenProducts . length ) {
168154 return ;
169155 }
170156
171- givenProducts . forEach ( ( ) => {
172- const givenProduct = givenProducts [ Number ( i ) ] . product . node ;
157+ // Map products to the correct format
158+ formattedCart . products = givenProducts . map ( ( item ) => {
159+ const givenProduct = item . product . node ;
173160
174161 // Convert price to a float value
175- const convertedCurrency = givenProducts [ Number ( i ) ] . total . replace (
176- / [ ^ 0 - 9 . - ] + / g,
177- '' ,
178- ) ;
179-
180- product . productId = givenProduct . productId ;
181- product . cartKey = givenProducts [ Number ( i ) ] . key ;
182- product . name = givenProduct . name ;
183- product . qty = givenProducts [ Number ( i ) ] . quantity ;
184- product . price = Number ( convertedCurrency ) / product . qty ;
185- product . totalPrice = givenProducts [ Number ( i ) ] . total ;
186-
187- // Ensure we can add products without images to the cart
188-
189- product . image = givenProduct . image . sourceUrl
190- ? {
191- sourceUrl : givenProduct . image . sourceUrl ,
192- srcSet : givenProduct . image . srcSet ,
193- title : givenProduct . image . title ,
194- }
195- : {
196- sourceUrl : process . env . NEXT_PUBLIC_PLACEHOLDER_SMALL_IMAGE_URL ,
197- srcSet : process . env . NEXT_PUBLIC_PLACEHOLDER_SMALL_IMAGE_URL ,
198- title : givenProduct . name ,
199- } ;
200-
201- totalProductsCount += givenProducts [ Number ( i ) ] . quantity ;
202-
203- // Push each item into the products array.
204- formattedCart . products . push ( product ) ;
205- i ++ ;
162+ const convertedCurrency = item . total . replace ( / [ ^ 0 - 9 . - ] + / g, '' ) ;
163+
164+ // Add to total count
165+ totalProductsCount += item . quantity ;
166+
167+ // Create a new product object for each item
168+ return {
169+ productId : givenProduct . productId ,
170+ cartKey : item . key ,
171+ name : givenProduct . name ,
172+ qty : item . quantity ,
173+ price : Number ( convertedCurrency ) / item . quantity ,
174+ totalPrice : item . total ,
175+ image : givenProduct . image . sourceUrl
176+ ? {
177+ sourceUrl : givenProduct . image . sourceUrl ,
178+ srcSet : givenProduct . image . srcSet ,
179+ title : givenProduct . image . title ,
180+ }
181+ : {
182+ sourceUrl : process . env . NEXT_PUBLIC_PLACEHOLDER_SMALL_IMAGE_URL ,
183+ srcSet : process . env . NEXT_PUBLIC_PLACEHOLDER_SMALL_IMAGE_URL ,
184+ title : givenProduct . name ,
185+ }
186+ } ;
206187 } ) ;
207188 formattedCart . totalProductsCount = totalProductsCount ;
208189 formattedCart . totalProductsPrice = data . cart . total ;
0 commit comments