Ramesh
Ramesh

Reputation: 121

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?

Upvotes: 1

Views: 2005

Answers (1)

adam111
adam111

Reputation: 91

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);

Upvotes: 2

Related Questions