I need some help. I am trying to prevent direct access to a page that my customers get redirected to after checkout. I want the page to be accessible only after checkout.
I use the following code snippet to redirect users after checkout:
add_action( 'template_redirect', 'woocommerce_redirect_after_checkout' );
function woocommerce_redirect_after_checkout() {
global $wp;
if ( is_checkout() && ! empty( $wp->query_vars['order-received'] ) ) {
$redirect_url = '/';
wp_redirect($redirect_url );
exit;
}
}
I have found this topic: prevent/block direct access to a thank you page
I placed the following code snippet to my functions.php to prevent direct access to the redirect page:
add_action('template_redirect', function() {
// ID of the redirect page
if (!is_page(2072)) {
return;
}
// coming from the checkout, so all is fine
if (wp_get_referer() === '/') {
return;
}
// we are on thank you page
// visitor is not coming from form
// so redirect to home
wp_redirect(get_home_url());
exit;
} );
This works fine if the customer pays through Stripe. However, it does not work if the customer chooses to pay through PayPal because PayPal redirects the customer to their website to make the payment.
Can something be done here to fix this issue?