How to make woocommerce add cart function works for not logged in users?

WC()->cart->add_to_cart(8, 1); 

I am using the above code to add dynamic products in the cart. It’s working fine when I am logged in but it fails to add products while I am not logged in. How to fix this?

Related posts

1 comment

  1. Answering 2 years later in case anyone has a similar problem.
    I solved this by adding first adding the item to the cart, and then setting the woocommerce session cookie:

    wc()->cart->add_to_cart($id);
    wc()->session->set_customer_session_cookie(true);
    

Comments are closed.