#StackBounty: #woocommerce Woocommerce add_to_cart non-register visitor not working

Bounty: 50

I’ve created a plugin that requires on-the-fly WooCommerce product creation (Simple Product) and adding to the visitor’s cart. All of this happens on the front end / public side – i.e. visitor fills in a form on a page and submit it. I’ve used resources such as this answer to get started. Once the product is successfully created, I use:

$product_id = $this->create_product($product);
// Error checking, etc
$result = WC()->cart->add_to_cart($product_id);

To add it to the cart. This works well while I’m logged in (on the admin side – front-end / public side probably picks up that I’m logged in).

But when I’m not logged in: Only the product creation seems to work as I can see the product in the db but the visitor’s cart is empty. And the $result seems fine.

$result = 44f683a84163b3523afe57c2e008bc8c

I’ve change the setting of WooCommerce but it made no difference.
enter image description here

Update

For clarity: I don’t want to force a visitor to register before adding the product to the cart.

Side effect: I get no products in my cart when I’m not logged in but when I log in and add another product then I get both in my cart.

How do I ensure that a product created like this gets added to the visitors cart?


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.