Skip to content

Commit 922c8fc

Browse files
authored
Merge pull request #1297 from w3bdesign/dev
Dev
2 parents a7c30cb + 04c3596 commit 922c8fc

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

components/Products/ProductsSingleProduct.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666

6767
<script setup>
6868
/**
69-
* Vue.js component that displays a single product.
69+
* Component that displays a single product.
7070
*
7171
* @component
7272
* @example
@@ -119,7 +119,7 @@ watch(
119119
* @return {Promise<void>} A Promise that resolves when the product has been successfully added to the cart.
120120
*/
121121
const addProductToCart = async (product) => {
122-
cart.addToCart(product);
122+
await cart.addToCart(product);
123123
124124
watchEffect(() => {
125125
if (isLoading.value === false) {

store/useCart.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export const useCart = defineStore("cartState", {
3939
);
4040

4141
if (foundProductInCartIndex > -1) {
42-
this.cart[foundProductInCartIndex].quantity += newCartItem.quantity;
42+
this.cart[foundProductInCartIndex].quantity += 1;
4343
} else {
4444
// We need to construct a cart item that matches the expected structure in `this.cart`
4545
const productCopy = {
@@ -48,6 +48,7 @@ export const useCart = defineStore("cartState", {
4848
price: newCartItem.total, // Assuming 'total' is the price for one item
4949
slug: newCartItem.product.node.slug,
5050
};
51+
5152
this.cart.push(productCopy);
5253
}
5354
} else {
@@ -80,12 +81,22 @@ export const useCart = defineStore("cartState", {
8081
getCartTotal() {
8182
const currencySymbol = useRuntimeConfig().public.currencySymbol || "kr";
8283

83-
return this.cart.reduce(
84-
(total, product) =>
85-
total +
86-
Number(product.price.replace(currencySymbol, "")) * product.quantity,
87-
0
88-
);
84+
const total = this.cart.reduce((total, product) => {
85+
// Assuming product.price is a string that includes the currency symbol
86+
const numericPrice = product.price
87+
.replace(currencySymbol, "")
88+
.replace(/[^0-9.]+/g, "");
89+
90+
// Convert the cleaned string to a floating-point number
91+
const price = parseFloat(numericPrice);
92+
93+
const productTotal = price * product.quantity;
94+
95+
return total + productTotal;
96+
}, 0);
97+
98+
// Format the total with the currency symbol and return it
99+
return `${currencySymbol} ${total.toFixed(2)}`;
89100
},
90101
},
91102
persist: true,

0 commit comments

Comments
 (0)