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
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?