If you need to redirect the user directly to the checkout page instead of the current product page or cart page, put this snippet to your themes functions.php
.
[php]
/**
* Redirect user to checkout page directly after adding to cart
*
* @return string
*/
function wc_redirect_to_checkout() {
$checkout_url = WC()->cart->get_checkout_url();
return $checkout_url;
}
add_filter( ‘woocommerce_add_to_cart_redirect’, ‘wc_redirect_to_checkout’ );
[/php]
Tareq,
Thanks for this. I’ve been searching high and low for a variation on this. Specifically, I want normal Woocommerce functionality for all products presented in the store. (They go into cart but user is not directed to checkout.) But I also want some of those products to also to have long-form WordPress sales pages external to Woocommerce with Buy Now buttons that just put the product in the cart and redirect right to the checkout page.
Put another way, I want to sell the same products two different ways: 1) as a normal part of the store/cart/checkout/ shopping process and 2) as part of a sales funnel where the visitor is not even aware of other products or a store construct.
Any ideas would be greatly appreciated. (Caveat: I know enough about coding to make myself dangerous!)
Thanks!
If you are using a simple product (not variable product), it’s easy enough. Just put the link in your custom sales page with this format: http://example.com/your-product-url/?add-to-cart=THE_PRODUCT_ID (ex: http://example.com/your-product-url/?add-to-cart=1232). WooCommerce will trigger a add-to-cart event when it sees the
add-to-cart
parameter in the url no matter where it comes from.thank you for this code. for online shopping go to http://gents.speedupcd.com
Thanks Tareq,
I am using add-to-cart=product-ID which works fine and the product is added to the cart. However, I don’t know why the amount is not shown on the checkout page. I would like to see both the total amount that client has to pay (cart amount and items) and the Place Order or Check Out on the same page. Is that possible? Your help is appreciated.