Skip to content
Advertisement

Make WC_Cart add_to_cart method working for guests in Woocommerce

Im trying to get my code to work. Searching for hours now. I found similar questions like this one. But unfortunately noone seems to find a solution.

In my custom plugin I want to add a specific item to the WC cart and redirect the user directly to the checkout. As a logged in user it works like a charm but for guests it shows a blank page on checkout (woocommerce_checkout shortcode seems to return nothing in this case). So I came up with a check if the cart iss till empty. Apparently it is because the “cart-page” shows “There are no items in the cart yet”.

In code i check if the cart is still empty but it tells me its not!

Here is my code:

JavaScript

The code runs without errors. And it works 100% as a logged in user / admin. Just not as guest, even tho I allowed the guest checkout in the woocommerce settings.

Advertisement

Answer

You need to initiate Woocommerce User session when it’s not logged in. So you will use the following:

JavaScript

Code goes in functions.php file of your active child theme (or active theme). It should works.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement