wp-graphql / wp-graphql-woocommerce

Add WooCommerce support and functionality to your WPGraphQL server
https://woographql.com
GNU General Public License v3.0
633 stars 123 forks source link

QUESTION: Adding item to the cart of a certain session #854

Closed niklaszulus closed 1 month ago

niklaszulus commented 1 month ago

I've been following this guide because i want to make a Shopify like checkout, which means that i use a mutation to create a cart and then use this url to redirect to the checkout which has the predefined items already in it.

The problem that i am facing is that in the above mentioned tutorial i need to write php code on the server which i either package into a plugin or put it in the functions.php.

The guide accomplishes this by letting the frontend make an addToCart mutation and puts the items which have to be available at checkout in it

e.g. making a cart for a checkout with a single product in it mutation { addToCart(input: {productId: 13}) { cart { total } } } then save the "woocommerce-session" token out of the response header, decode it (because its a JWT) and then get the session which has the items stored in the cart (due to the cart mutation. The decoded session is then passed as an url argument e.g. (woocommerce-page.com/checkout/session_id=${sessionId}) Now the woocommerce backend takes this url argument and reconstructs the session (code found in guide).

It seems like this is possible without writing custom php code for the backend since we can create checkoutUrls and transfer the session via transfer-session endpoint.

But how do I put the item which i want to have in the checkout in the cart and not loose the checkoutUrl in the process without going through above mentioned trouble?

niklaszulus commented 1 month ago

Solved:

I got the checkoutUrl via the updateSession mutation which is also availiable with the addToCart mutation