Skip to content

Commit acd907a

Browse files
authored
Merge pull request #824 from w3bdesign/develop
Fix bug with empty cart
2 parents 50b6f98 + c7b6c46 commit acd907a

File tree

7 files changed

+49
-30
lines changed

7 files changed

+49
-30
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
},

package-lock.json

Lines changed: 33 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
"next": "^12.1.6",
2424
"nprogress": "^0.2.0",
2525
"postcss": "^8.4.13",
26-
"react": "^18.1.0",
27-
"react-dom": "^18.1.0",
26+
"react": "^17.0.2",
27+
"react-dom": "^17.0.2",
2828
"react-hook-form": "^7.30.0",
2929
"react-instantsearch-dom": "^6.24.1",
3030
"react-spring": "8.0.27",

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)