Redirecting to Checkout when Adding an Item to the Cart

If you want users to be immediately redirected to the checkout page after clicking on the “Purchase” button, you can place the code below into your functions.php or any custom plugin.

 function edd_redirect_to_cart_on_add( $data ) {
	global $edd_options;

	$redirect_url = get_permalink( $edd_options['purchase_page'] );
	
	if ( edd_get_current_page_url() != $redirect_url ) {
		wp_redirect( $redirect_url, 303 ); 
		exit;
	}
}
add_action( 'edd_add_to_cart', 'edd_redirect_to_cart_on_add', 999 );

Note, this only applies to sites that do not have ajax enabled in the Misc settings.

Comments

  1. michalw

    This should use a 303 redirect:

    wp_redirect( $redirect_url, 303 );

    307 will cause Firefox to show a prompt window with following message:

    “This web page is being redirected to a new location. Would you like to resend the form data you have typed to the new location?”

    Reference: http://en.wikipedia.org/wiki/HTTP_303

    Reply

  2. Note: if writing a custom plugin you need to ensure that your call to add_action() is performed before the ‘init’ action.

    edd invokes this action during its init processing, whenever it sees the edd_action keyword in the $GET or $POST.

    The $data that’s passed is either the $POST or the $GET array

    It’s not necessary to return( $data );

    Reply

  3. Edo Melters

    Can somebody help me with this code. I have tried everything but can’t get it to work! I made a custom plugin with the code above but it doesn’t function. I’m not a php expert so getting desperate on this…

    Thanks

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>