Skip to content

Instantly share code, notes, and snippets.

@micc83
Last active April 11, 2024 00:13
Show Gist options
  • Star 47 You must be signed in to star a gist
  • Fork 16 You must be signed in to fork a gist
  • Save micc83/a129fe061e37932c44ea to your computer and use it in GitHub Desktop.
Save micc83/a129fe061e37932c44ea to your computer and use it in GitHub Desktop.
Skip cart and redirect to direct checkout on WooCommerce
<?php
/**
* Skip cart and redirect to direct checkout
*
* @package WooCommerce
* @version 1.0.0
* @author Alessandro Benoit
*/
// Skip the cart and redirect to check out url when clicking on Add to cart
add_filter ( 'add_to_cart_redirect', 'redirect_to_checkout' );
function redirect_to_checkout() {
global $woocommerce;
// Remove the default `Added to cart` message
wc_clear_notices();
return $woocommerce->cart->get_checkout_url();
}
// Global redirect to check out when hitting cart page
add_action( 'template_redirect', 'redirect_to_checkout_if_cart' );
function redirect_to_checkout_if_cart() {
if ( !is_cart() ) return;
global $woocommerce;
if ( $woocommerce->cart->is_empty() ) {
// If empty cart redirect to home
wp_redirect( get_home_url(), 302 );
} else {
// Else redirect to check out url
wp_redirect( $woocommerce->cart->get_checkout_url(), 302 );
}
exit;
}
// Empty cart each time you click on add cart to avoid multiple element selected
add_action( 'woocommerce_add_cart_item_data', 'clear_cart', 0 );
function clear_cart () {
global $woocommerce;
$woocommerce->cart->empty_cart();
}
// Edit default add_to_cart button text
add_filter( 'add_to_cart_text', 'woo_custom_cart_button_text' );
add_filter( 'woocommerce_product_single_add_to_cart_text', 'custom_cart_button_text' );
function custom_cart_button_text() {
return __( 'Buy', 'woocommerce' );
}
// Unset all options related to the cart
update_option( 'woocommerce_cart_redirect_after_add', 'no' );
update_option( 'woocommerce_enable_ajax_add_to_cart', 'no' );
@fauzanrozaq
Copy link

where i can to put this file in the folder woocommerce

@advokatb
Copy link

Code in lines 23-35 redirects only users, that are not logged in.. why?

@edwardkay
Copy link

Note that if the cart is empty, the checkout page redirects back to the cart. With redirect_to_checkout_if_cart this creates an infinite redirect loop.

Fix by adding:

    if ( $woocommerce->cart->is_empty() ) {
      // Checkout views with empty carts are redirected to cart, which would create an infinite redirect loop
      wp_redirect( get_home_url() );
      exit();
    }

to line 30.

@TaisaDesigner
Copy link

Really nice and complete 👍 Thanks!

@rajeshsingh520
Copy link

You can use this plugin it offers to redirect even on the Ajax add to cart on Archive pages as well https://wordpress.org/plugins/add-to-cart-direct-checkout-for-woocommerce/

@patrickcameron
Copy link

If you're using this script, be sure to use edwardkay's fix in the comments above!

@micc83
Copy link
Author

micc83 commented Feb 3, 2020

@edwardkay Fixed the infinite redirect issue on empty cart.

@Njengah
Copy link

Njengah commented Mar 23, 2020

get_checkout_url() has since been deprecated and should be replaced with : wc_get_checkout_url()

@micc83
Copy link
Author

micc83 commented Mar 23, 2020

@Njengah I'm not in WP anymore so I don't feel comfortable editing the gist. Still, thank you for noticing it. BTW you can fork it and link the updated version here in the comments.

@ArneSaknussemm
Copy link

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment