How to give the customer a direct link to purchase through WooCommerce?

I have such a problem. There is a product in WooCommerce. I need to give a link to the products to the customer, so he got right to the order page of this product.
How it can be implemented, even spike method? (for example, give a link to the product, but it redirects to the order page for this product)
Thanks for ideas in advance!
March 23rd 20 at 18:47
1 answer
March 23rd 20 at 18:49
Solution
Roughly:
prefix_automatically_add_to_cart function()
{
 // First decide how you are going to pass item ID via 
 // GET the link via a custom rewrite, POST, cookies, etc.
 // Next, get the ID here, depending on the decision above.
 $product_id = 1;

 if ( WC()->cart->get_cart_contents_count() === 0 ) {
 // Programmatically add product to cart
 WC()->cart->add_to_cart( $product_id );

 // Redirect to the cart (or checkout)
 wp_redirect( WC()->cart->get_cart_url() );
 // wp_redirect( WC()->cart->get_checkout_url() );

die;
}
}
add_action( 'template_redirect', 'prefix_automatically_add_to_cart' );
Interesting solution, thanks. but this hook template_redirect where I hang turns? to view the product?
well, I will give as it looks in the admin product list.
that is .../product/testovyj-tovar/ - Julio commented on March 23rd 20 at 18:52
@Julio,
Interesting solution, thanks. but this funzio prefix_automatically_add_to_cart I hang up do much?

You are not careful:
add_action( 'template_redirect', 'prefix_automatically_add_to_cart' );

Code hangs on ache template_redirect. This is the optimal hook at this point you have access to the data in the REQUEST (GET/POST), the headers have not been sent. Determine whether the request is automatically added to the cart (more on this in the code in the comment is written), taking the passed item ID and well add it to the cart, and then make the redirect there too.

to view the product?

No, you need to decide how you will form the link which will automatically add to your cart, then this link to display somewhere (in the email send or whatever you need). When you click on this special link will otrabatyvat your logic (see above).

well, I will give as it looks in the admin product list.
that is .../product/testovyj-tovar/

Are you kidding me? Want to link to the information about the product automatic passed the product page and threw in the bin? I used this to monitor spat. - gerson commented on March 23rd 20 at 18:55

Find more questions by tags WooCommerceWordPress