Skip to content

Instantly share code, notes, and snippets.

@atxiii
Created December 7, 2019 09:45
Show Gist options
  • Save atxiii/002209ee37667c38cb8a18f073fe607f to your computer and use it in GitHub Desktop.
Save atxiii/002209ee37667c38cb8a18f073fe607f to your computer and use it in GitHub Desktop.
Redirect to checkout after click add to cart
<?php
// redirect to checkout after click 'add to cart' - Woocommerce
add_filter ('add_to_cart_redirect', 'redirect_to_checkout');
function redirect_to_checkout() {
global $woocommerce;
$checkout_url = $woocommerce->cart->get_checkout_url();
return $checkout_url;
}
// redirect checkout to shop when cart is empty
add_action( 'template_redirect', function() {
if ( ! is_cart() ) { return; }
if ( WC()->cart->get_cart_contents_count() == 0 ) {
wp_redirect( apply_filters( 'wmsc_redirect', wc_get_page_permalink( 'shop' ) ) );
exit;
}
// Redirect to checkout page
wp_redirect( wc_get_checkout_url(), '301' );
exit;
} );
add_action('template_redirect', 'redirection_function');
function redirection_function(){
global $woocommerce;
if( is_checkout() && 0 == sprintf(_n('%d', '%d', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count) && !isset($_GET['key']) ) {
wp_redirect( apply_filters( 'wmsc_redirect', wc_get_page_permalink( 'shop' ) ) );
exit;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment