Last active
October 11, 2023 15:10
-
-
Save FrancoStino/ad705a46ad73ae64dda6a34d25aa3448 to your computer and use it in GitHub Desktop.
Check and redirect to login before checkout - Force login and registration sign up sign in - Woocommerce
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* WooCommerce check and redirect to login before checkout | |
*/ | |
add_action('template_redirect', 'check_if_logged_in'); | |
function check_if_logged_in() | |
{ | |
$pageid = get_option('woocommerce_checkout_page_id'); | |
// Check if the user is not logged in and on the checkout page | |
if (!is_user_logged_in() && is_page($pageid)) { | |
$url = add_query_arg('redirect_to', get_permalink($pageid), site_url('/mio-account/')); // your my account URL | |
wp_redirect($url); | |
exit; | |
} | |
// Check if the user is logged in and on the my account page | |
if (is_user_logged_in() && is_page(get_option('woocommerce_myaccount_page_id'))) { | |
$redirect = isset($_GET['redirect_to']) ? esc_url($_GET['redirect_to']) : ''; | |
if (!empty($redirect)) { | |
echo '<script>window.location.href = "' . $redirect . '";</script>'; | |
exit; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment