@@ -39,7 +39,9 @@ export const useCart = defineStore("cartState", {
39
39
) ;
40
40
41
41
if ( foundProductInCartIndex > - 1 ) {
42
- this . cart [ foundProductInCartIndex ] . quantity += newCartItem . quantity ;
42
+ // We need to update the quantity
43
+
44
+ this . cart [ foundProductInCartIndex ] . quantity += 1 ;
43
45
} else {
44
46
// We need to construct a cart item that matches the expected structure in `this.cart`
45
47
const productCopy = {
@@ -48,6 +50,7 @@ export const useCart = defineStore("cartState", {
48
50
price : newCartItem . total , // Assuming 'total' is the price for one item
49
51
slug : newCartItem . product . node . slug ,
50
52
} ;
53
+
51
54
this . cart . push ( productCopy ) ;
52
55
}
53
56
} else {
@@ -80,12 +83,24 @@ export const useCart = defineStore("cartState", {
80
83
getCartTotal ( ) {
81
84
const currencySymbol = useRuntimeConfig ( ) . public . currencySymbol || "kr" ;
82
85
83
- return this . cart . reduce (
84
- ( total , product ) =>
85
- total +
86
- Number ( product . price . replace ( currencySymbol , "" ) ) * product . quantity ,
87
- 0
88
- ) ;
86
+ //console.log("Cart:", this.cart);
87
+
88
+ const total = this . cart . reduce ( ( total , product ) => {
89
+ // Assuming product.price is a string that includes the currency symbol
90
+ const numericPrice = product . price
91
+ . replace ( currencySymbol , "" )
92
+ . replace ( / [ ^ 0 - 9 . ] + / g, "" ) ;
93
+
94
+ // Convert the cleaned string to a floating-point number
95
+ const price = parseFloat ( numericPrice ) ;
96
+
97
+ const productTotal = price * product . quantity ;
98
+
99
+ return total + productTotal ;
100
+ } , 0 ) ;
101
+
102
+ // Format the total with the currency symbol and return it
103
+ return `${ currencySymbol } ${ total . toFixed ( 2 ) } ` ;
89
104
} ,
90
105
} ,
91
106
persist : true ,
0 commit comments