WooCommerce: Redirect to Checkout after Add to Cart

There are instances where a person may want to redirect the user to checkout after adding items to cart ie. if they use Woocommerce booking appointment and can only book one time.
The below code snippet allows for changing the behaviour of the Add to Cart button.

Code

add_filter( 'woocommerce_add_to_cart_redirect', 'themename_add_to_cart_redirect' );

function themename_add_to_cart_redirect( $url ) {
    return get_permalink( get_option( 'woocommerce_checkout_page_id' ) ); 
}

Where to add this Code snippet?

Usually, the code is added in functions.php of your child theme. As shown below:

/path/to/wordpress/wp-content/themes/mytheme-child/functions.php

I don't have access to this functions.php


As an alternative, you can also install and use a plugin called Code Snippets from the admin.
  1. Log into your WordPress admin
  2. Click Plugins
  3. Click Add New
  4. Search for Code Snippets
  5. Click Install Now under "Code Snippets"
  6. Activate the plugin
  7. Under Code Snippets, click Add New
  8. Paste and publish the code above

Comments