Skip to content

Commit c7b6c46

Browse files
committed
Fiks bug med tom handlekurv
1 parent 46f1247 commit c7b6c46

File tree

5 files changed

+14
-1
lines changed

5 files changed

+14
-1
lines changed

components/Cart/AddToCartButton.component.jsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,13 @@ const AddToCartButton = ({ product }) => {
3636
onCompleted: () => {
3737
// Update cart in the localStorage.
3838
const updatedCart = getFormattedCart(data);
39+
40+
if (!updatedCart) {
41+
return;
42+
}
43+
3944
localStorage.setItem('woocommerce-cart', JSON.stringify(updatedCart));
45+
4046
// Update cart data in React Context.
4147
setCart(updatedCart);
4248
},

components/Cart/CartPage/CartItemsContainer.component.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const CartItemsContainer = () => {
2323
// Update cart in the localStorage.
2424
const updatedCart = getFormattedCart(data);
2525
localStorage.setItem('woocommerce-cart', JSON.stringify(updatedCart));
26+
2627
// Update cart data in React Context.
2728
setCart(updatedCart);
2829
},

components/Checkout/CheckoutForm.component.jsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ const CheckoutForm = () => {
2828
// Update cart in the localStorage.
2929
const updatedCart = getFormattedCart(data);
3030
localStorage.setItem('woocommerce-cart', JSON.stringify(updatedCart));
31+
console.log(
32+
'setItem har blitt kalt med følgende data fra CheckoutForm: ',
33+
JSON.stringify(updatedCart)
34+
);
3135
// Update cart data in React Context.
3236
setCart(updatedCart);
3337
},

utils/context/AppContext.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ export const AppProvider = ({ children }) => {
1313
// Check if we are client-side before we access the localStorage
1414
if (process.browser) {
1515
let cartData = localStorage.getItem('woocommerce-cart');
16+
1617
cartData = null !== cartData ? JSON.parse(cartData) : '';
18+
1719
setCart(cartData);
1820
}
1921
}, []);

utils/functions/functions.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export const getFloatVal = (string) => {
4545
*/
4646
export const getFormattedCart = (data) => {
4747
let formattedCart = null;
48-
if (!data || !data.cart.contents.nodes.length) {
48+
if (!data || !data.cart.contents.nodes.length || !data.cart) {
4949
return formattedCart;
5050
}
5151
const givenProducts = data.cart.contents.nodes;

0 commit comments

Comments
 (0)