How to merge cart items of guest user to logged in user cart items using Store API? #49448
Replies: 3 comments 4 replies
-
in 4th step, did you send existing cookies as well? are you using Cart Token or just WooCommerce cookies? |
Beta Was this translation helpful? Give feedback.
-
@senadir @ahsanshaheen199 I've managed to get merged carts working as a basic proof of concept today but only because I'm testing something on the API backend of actually logging the user in and using wp_set_current_user(). The code I've been going through on github for WooCommerce merging carts, leans heavily on get_current_user_id() and php sessions by the looks of it and not sure how this works with JWT tokens, Cart-Token, Nonce headers. From all my testing, it doesn't... This seems like a bug to me bearing in mind a lot of users may not be interacting with this on a monolithic woocommerce site with php sessions / default auth flow. |
Beta Was this translation helpful? Give feedback.
-
I took a look at the code and yeah merging carts won't work with Store API's Cart Token because the cart merging flow is handled in the cookie session handler. woocommerce/plugins/woocommerce/includes/class-wc-cart-session.php Lines 108 to 118 in 9bdf763 If the JWT plugin is triggering |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Suppose I add X and Y items as a logged-in user to a cart. Then I logout and add Z item to the cart as a guest user. Later, if I log in again and check my cart, I will see X, Y, and Z items. This is how WooCommerce merges its carts. I would like to achieve the same behavior in a headless app using Store API. Using Store Api I get the logged-in user cart correctly but it does not merge guest user cart items.
As a guest user:
1st step: {{WORDPRESS_URL}}/wc/store/v1/cart - it gives me the cart nonce and cart token
2nd step: {{WORDPRESS_URL}}/wc/store/v1/cart/add-item?id=100&quantity=1(in headers I added cart nonce) - it gives me updated cart
3rd step: {{WORDPRESS_URL}}/wc/store/v1/cart (in headers I added cart token that I got from 2nd step) - cart information is correct.
4th step: {{WORDPRESS_URL}}/wc/store/v1/cart (in headers I added bearer token of a user) - it gives me the previous cart information of logged in user not the merged version of 2 carts.
Beta Was this translation helpful? Give feedback.
All reactions