How can I add a private item from my store to the cart?
$product_id = 1234;
WC()->cart->add_to_cart($product_id);
Whenever I try to do this, the item is not being added (unless I make it public). I don't want to make it public however since I want to keep these items hidden.
I have found this example after doing a search, but it doesn't explain how to actually implement it. How to display/allow 'add to cart' for private products in Woocommerce
Solution
Redirect to the WooCommerce 'shop' page if the product is viewed directly
// redirect if the product is viewed directly
add_action('template_redirect', 'redirect_to_shop');
function redirect_to_shop()
{
$product_id = 1234;
if (get_the_id() == $product_id)
{
wp_redirect(wc_get_page_permalink('shop'));
exit;
}
}