How to disable Woocommerce login redirect if on Checkout Page - woocommerce

I have some code in my WordPress functions.php file to do a redirect after a successful woocommerce login. It works great, but I'm wondering how I can disable that redirect from running on the checkout page?
If a user logs into their account while on the checkout page they are getting redirected off the checkout page just before they have the chance to fill in their credit card data which isn't a great experience.
According to https://docs.woocommerce.com/document/conditional-tags/ conditional query tags like is_checkout() won't work in the functions.php file because You can only use conditional query tags after the posts_selection action hook in WordPress. And, unfortunately, apparently the functions.php file gets run before that.
What's my best option to solve this?
Here's the code I currently have running (the one I want to disable on checkout page):
function woo_login_redirect( $redirect_to ) {
$redirect_to = get_permalink(70241);
return $redirect_to;
}
add_filter('woocommerce_login_redirect', 'woo_login_redirect');
This is me trying to avoid the redirect if on checkout page (it doesn't work. It just redirects to post id 70241):
function woo_login_redirect( $redirect_to ) {
if (is_checkout()){
$redirect_to = '#';
} else {
$redirect_to = get_permalink(70241);
}
return $redirect_to;
}
add_filter('woocommerce_login_redirect', 'woo_login_redirect');

Related

How to redirect user to another page if the user is logged in - WordPress Elementor Page Builder

I have created a registration form using Elementor Page Builder. Now, I want to redirect the user to a different page if he/she is trying to access that registration page after logging in.
Is there any Elementor hook available for that? I know the WordPress function called is_user_logged_in().
function my_logged_in_redirect() {
if ( is_user_logged_in() && is_page( 12 ) )
{
wp_redirect( get_permalink( 32 ) );
die;
}
}
add_action( 'template_redirect', 'my_logged_in_redirect' );
You should get the ids of the page where the form is and the id of the page you want to redirect the user to.
Code goes in your child theme functions.php file
Reference: here
The 'Content Area Not Found' error might appear on Elementor designed sites when you use that snippet and try to edit page of ID 12 (in your example) in certain cases.
To avoid this, add the following code before the if-statement of your snippet:
if ( \Elementor\Plugin::$instance->preview->is_preview_mode() ) {
return;
}

Unlogged user cannot see store and must go to login and successfully logged in users must be redirected to store

I am working on a Wordpress site with Woocommerce. I am using this function inside functions.php to redirect everyone who visits my store to login since it is a private area. Once the person logs in, they should be redirected to the store again. I have the following code implemented, which works for me to get all those trying to navigate my store to log in, I need to modify this code so that once logged in, it redirects me to the store page in woocommerce, in my case the page of the store is called "tienda".
To clarify: Users who are not registered or logged in, should not be able to see the store or cart or anything from woocommerce, I already implemented this with a wp-members plugin, if they try to navigate these woocommerce urls, they must go to login. When you have already logged in, you should go to the store, in my case you should not redirect to the previous link, but must inevitably go to the page called "tienda".
On the other hand, I would like to know if my function is missing something or is well built. Thank you!
function loggedoutuser_redirect() {
if (
! is_user_logged_in()
&& (is_woocommerce() || is_cart() || is_checkout())
) {
header('Location: ' . wp_login_url());
exit;
}
}
add_action('template_redirect', 'loggedoutuser_redirect');
function woocommerce_login_redirect_custom( $redirect, $user ) {
$redirect = wc_get_page_permalink( 'shop' );
return $redirect;
}
add_filter( 'woocommerce_login_redirect', 'woocommerce_login_redirect_custom', 10, 2 );
Paste this above snippet at the end of your active child theme functions.php file or else if you are good in plugin development then create a custom plugin.
Snippet Explanation:
Create a custom function to redirect after logged in
Fetch the shop URL using WooCommerce helper function wc_get_page_permalink( 'shop' ) and assign it to the variable $redirect
Then return the URL
Hook the custom function to the filter hook woocommerce_login_redirect
Note: You can check the user role inside our custom function then based on the role you can redirect it to the dashboard if he is administrator else to the shop page.

wordpress redirection loop after login

I have created a custom post taxonomy called portfolio so such posts have an url like this: mydomain.com/portfolio/post-name/. This is fine so far however i just found I am having a problem with my Wordpress login and i believe this is causing it. I have a login link on every post so if a user log in from there it is supposed to be redirected to the post page but instead i get a redirection loop error.
If you look at the URL which Wordpress is trying to redirect to you will see something like mydomain.com//post-name/ so it means the taxonomy name is missing from the URL and thats causing the redirection error.
How can i address this problem and have the redirection fixed?
Thank you.
Redirect to Previous Page After Login
Add the following code in your function.php it would help.
if ( (isset($_GET['action']) && $_GET['action'] != 'logout') || (isset($_POST['login_location']) && !empty($_POST['login_location'])) ) {
add_filter('login_redirect', 'my_login_redirect', 10, 3);
function my_login_redirect() {
$location = $_SERVER['HTTP_REFERER'];
wp_safe_redirect($location);
exit();
}
}

Only logged in users should be able to view product in WooCommerce

I want to restrict WooCommerce products, shop page and category pages to logged in users only.
I do not want to achieve it with any plugin.
Please let me know if anybody done it before with any hook/filter/action.
Or I have to make WooCommerce template pages and add condition over there.
If I was to do this I would hook into the init action that WordPress offers.
Then do something like this in your theme's functions.php file:
function woo_check_logged_in()
{
if ( (is_product() || is_shop() ) && is_user_logged_in() )
{
}
else
{
die("You must be logged in to view this page");
}
}
add_action('init', 'woo_check_logged_in');
I haven't tested this but I believe it should get you on the right path without having to use any plugins.

wordpress login redirect function

I am trying to create my own plugin in wordpress. Everything works great but I want to make a loginredirect check for users who want to access to the plugins page.
Here is my function in my functions.php:
function wpuf_auth_redirect_login() {
$user = wp_get_current_user();
if ( $user->id == 0 ) {
nocache_headers();
wp_redirect(get_option('siteurl') . '/wp-login.php?redirect_to=' . urlencode($_SERVER['REQUEST_URI']));
exit();
}
}
And this is from my myplugin.php:
function wpuf_user_edit_profile() {
wpuf_auth_redirect_login(); // if not logged in, redirect to login page
nocache_headers();
wpuf_post_form_style();
wpuf_user_edit_profile_form();
}
add_shortcode('wpuf_editprofile', 'wpuf_user_edit_profile');
It is not working. If I enter the plugins page as a guest in browser it does not redirect. It only shows the template uncomplete. Any help?
Where you say that it 'only shows the template uncomplete', this could indicate that there is a syntax error in your PHP code, and the server has stopped rendering the page at the point of that error.
I would suggest that you check for any errors by turning error reporting on, and seeing what happens from there.

Resources