Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save JeroenSormani/44c070d01dd34e20fb06 to your computer and use it in GitHub Desktop.
Save JeroenSormani/44c070d01dd34e20fb06 to your computer and use it in GitHub Desktop.
WooCommerce redirect after add to cart
/**
* Redirect to checkout at add-to-cart action.
*/
function redirect_to_checkout() {
return WC()->cart->get_checkout_url();
}
add_filter( 'woocommerce_add_to_cart_redirect', 'redirect_to_checkout' );
------------------------------------
/**
* Redirect to checkout at add-to-cart action on specific product IDs.
* When not in the list of IDs it will go to the cart (default behaviour).
*/
function redirect_to_checkout_product_ids( $url ) {
if ( in_array( $_REQUEST['add-to-cart'], array( 1, 2, 3 ) ) ) :
$url = WC()->cart->get_checkout_url();
endif;
return $url;
}
add_filter( 'woocommerce_add_to_cart_redirect', 'redirect_to_checkout_product_ids' );
@Yahyaconsultants
Copy link

Thank you for sharing this.

if I wanted to add a product ID at the end of the URL how would I do that this is what I have done

function custom_add_to_cart_redirect() {

global $post,$product;
$id = $product->id;

$URL= "https://yahyaprints.com/product/$id";

 return $URL 
    
    ;

}

add_filter( 'woocommerce_add_to_cart_redirect', 'custom_add_to_cart_redirect' );

but it does not work ? can you please help me

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