Support » Plugin: WooCommerce » add_to_cart function not adding to cart when logged out

  • Resolved pinksharpii


    I’m using single-product.php to display my WC product in my theme with a custom form because the designed layout is completely different than the typical WC product layout. So I’m using the WC function add_to_cart at the top of the php file. It works perfectly when I’m logged in, but not when there is no user logged in.

    Here’s the necessary code. This should be fine, as I said it works with logged in users.
    If I var_dump $success it always is the same return value for everyone. Not sure if that’s accurate or not. But it does not return false/error out. The cart remains empty though.

    global $woocommerce;
     $success = $woocommerce->cart->add_to_cart( $post->ID,$quantity, $variation_parts[1], $var_arr );

    On another thread there was a similar issue solved by

    function prod_var_js()
            wp_enqueue_script('add-to-cart-variation', get_template_directory_uri() . '/js/add-to-cart-variation.js',array('jquery'),'1.0',true);

    But this does not work for me.

    The only plugins I have installed are ACF, WC and WP Super Cache (which is disabled. So this should not be the problem). Nothing should be getting cached.

    And of course Guest Checkout is toggled on in the settings.

Viewing 15 replies - 1 through 15 (of 24 total)
  • Same problem here, I’ve looked around but not found a solution.

    The most topics related with this, seems to be unanswered and other are only talking about the AJAX related issue with this problem.

    Did you found a solution? This bug seems really strange. (and such a thing for a eCommerce Software)

    Same here, yesterday a customer told me that she could not add product to the cart and I tried doing it, logged in as admin, it worked. And then I logged out, it didn´t work. And then I logged in again, and this time it didn´t work neither.

    My woocommerce is 2.3.9.

    At the same time I found that I can´t add a new post nor new product. Both of these actions take me to the list of post I have at the backend. This is the error I get:

    Warning: Creating default object from empty value in /homepages/wp-admin/includes/post.php on line 631

    The website was working perfectly yesterday, and I didnt do any update or adding new plugins.

    Then I updated the super cache (I thought that the error could be related to the fact that a number of plugins are not updated). Then all of the available updates of the plugins disappeared… I deleted the plugin the updates didnt come back. So I can´t try updating woocommerce to see if the problem could be fixed.

    I then increase the memory limit up to 96M in the wp-config php, no luck.

    I checked my website in, everything seems ok.

    I tried to desactivate all the plugins, except the woocommerce and a back up plugin, didnt work. I then desactivate all plugins and also woocommerce, I can´t add new post nor product still.

    Also tried this and it didnt fix the problem neither:
    define(‘WP_ALLOW_REPAIR’, true);

    Can anyone please help?? I really can´t find anyother thing I can try…

    Thanks a lot!

    When I click on add product to cart, it actually shows the value of the product in the cart icon/button for a few seconds, and then it change to zero again…

    Same here, it’s fine when logged in as an admin, but not as a usual visitor.. My client just emailed me about it. Is this due to an update?

    I have a similar issue.
    Cart is acting crazy when not logged in! You can’t either add to the cart or to change the quantity. Everything is going smoothly when logged in.

    I recorded a vid to show what I am dealing with

    I tried to send this as issue to Github, but because I call this Function in an external file, the Collaborator closed my issue.

    I will try to contact him again and show him this page, but I would recommend you to open issues for your problems for “woocommerce” on Github, too.

    I would bet, that when your problem can be solved, mine will be it, too.

    Hi Resachris, for my case neither does the cart nor the cart preview works… in the preview it appeared for a few seconds and disappear, and I cannot add new post, new product nor new user…

    I changed my password in case it was a hack and now I am logged out from the dashbroad…

    would someone please please help?

    My issue is fixed. Turned out that my hosting had some kind of a caching option turned on the server. They turned it off and everything is working smoothly now.

    Plugin Support Caleb Burks


    Automattic Happiness Engineer

    Caching is usually the issue here. Related:

    This was not a caching plugin but a caching option turned on by the hosting. Anyway I am glad it is ok now.

    Resachris, glad to know that your web is working fine now, I am going to talk to the host, but i deleted all the cache plugins and cache folders, and also cleared cookies but it still don’t work.

    My cart won’t work even if I am logged in, so I guess the problem might be different.

    I have open another thread about the problems I have encountered, I have been looking for solutions for days but no luck:

    help please! "empty value" error, Woocommerce carta empty automatically and more

    Sorry to hear that. I contacted the host, woocommerce and the theme authors in pursuit of an answer. The host turned out to be the reason for my issues.
    I did not have any caching plugins and yet there was some caching option on the server, that I was not aware of. I also remember having an issue on another site that was directly connected with the caching plugin – turning it off did not help. I had to delete it from the database to fix the problem. But you seem to have done that.
    Ask your host and talk to the theme authors.

    I am out of other ideas and cannot help more I am afraid.

    Good luck!

    Solved my problem by adding this to the initialization of my external application:


    In my case the problem was exactly that, what the function solves. I didn’t thought that such important little thing could be necessary for a external script. (because I thought this would be executed, too. When wp.load.php is required)

    Where exactly did you add that, _sascha_?

    In my initialization-function of my external application. (visual product editor). So this place couldn’t help you. Where did you call your:

    $success = $woocommerce->cart->add_to_cart( $post->ID,$quantity, $variation_parts[1], $var_arr );

    In a external script, too? If yes, try to add the set_customer_session_cookie()-function to your header. In a theme the same thing should do it. But… if in a theme, then I’m a little confused why it isn’t already fired.

Viewing 15 replies - 1 through 15 (of 24 total)
  • The topic ‘add_to_cart function not adding to cart when logged out’ is closed to new replies.