One way that you could do this is to set a session variable on the checkout page before you redirect the user to the register/login page. On the registration page you can check for this session variable and it's value. If it is set to the value from the checkout page you can redirect back to checkout page, and if not send them to the default go to page.
You can set a session based weather or not the user is logged in like this:
if(!session_id()) session_start();
if( (!(isset($_SESSION['userID']))) || ($_SESSION['userID'] == "")){
$_SESSION['checkout'] = 1;
}
On the registration page where the go to is specified you can set it like this:
$goto = (isset($_SESSION['checkout']) && ($_SESSION['checkout'] == 1))?checkout.php:default_return_page.php;