Last active
April 19, 2021 01:58
-
-
Save AmeenJalali/86f42e5e826d1efe5b9d22d48e3ffb57 to your computer and use it in GitHub Desktop.
Redirects to my account page after the customer logged in on any pages except checkout and cart page / WooCommerce
This file contains 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 | |
/* | |
It improves your WooCommerce website's UX. Useful if you have a login form on all pages (e.g. in the header), | |
so whenever a customer logged in, he redirects to my-account page, | |
except on checkout and cart page. | |
Add it in your theme functions.php file. | |
*/ | |
function wc_custom_user_redirect_to_my_account( $redirect, $user ) { | |
// get current url | |
$redirect_page_id = url_to_postid( $redirect ); | |
// get page IDs | |
$checkout_page_id = wc_get_page_id( 'checkout' ); | |
$cart_page_id = wc_get_page_id( 'cart' ); | |
// prevent redirecting on checkout and cart page | |
if( $redirect_page_id === $checkout_page_id || $redirect_page_id === $cart_page_id ) { | |
return $redirect; | |
} | |
// redirect to my-account page after logged in on any pages except checkout and cart pages | |
$redirect = get_permalink( wc_get_page_id( 'myaccount' ) ); | |
return $redirect; | |
} | |
add_filter( 'woocommerce_login_redirect', 'wc_custom_user_redirect_to_my_account', 10, 2 ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment