woocommerce checkout redirect based on conditions - php

I am trying for solution for woocommerce to check if the user is logged in when they click on the checkout button and not take to checkout page untill logedin or register . If the user is not logged in, open login register form of xootix plugin using its class .xoo-el-login-tgr, which shows a login and registration form. If the user logs in with existing credentials and there are products in the cart, they will be redirected to the checkout page. If there are no products in the cart, they will remain on the same page after login. If the user registers as a new user and there are products in the cart, they will be redirected to the checkout page. If there are no products in the cart, they will remain on the same page after registration.
I am using custmized code to achive this but it is not working when i click on checout button it is taking into checkout page as the code not at all applies.Looking for help.
I am trying below cusmized code to achive this
add_action( 'wp_footer', 'redirect_to_login_on_checkout' );
function redirect_to_login_on_checkout() {
if ( ! is_user_logged_in() && is_checkout() ) {
?>
<script>
jQuery(document).ready(function($) {
$('.checkout-button').on('click', function(e) {
e.preventDefault();
$('.xoo-el-login-tgr').trigger('click');
});
});
</script>
<?php
}
}
add_action( 'wp_login', 'redirect_to_checkout_on_login', 10, 2 );
function redirect_to_checkout_on_login( $user_login, $user ) {
if ( is_checkout() && ! WC()->cart->is_empty() ) {
wp_safe_redirect( wc_get_checkout_url() );
exit;
}
}
add_action( 'user_register', 'redirect_to_checkout_on_registration' );
function redirect_to_checkout_on_registration( $user_id ) {
if ( is_checkout() && ! WC()->cart->is_empty() ) {
wp_safe_redirect( wc_get_checkout_url() );
exit;
}
}
The solution i am looking is if some one add products to cart checkout button appera(cart page disabled)on clickng on checkout check used loged in or not ,if he login take to the checkout page if not loged in present login registrion form using XOOTIX plugin class to allow user to login or register.If new user rgister adn if there are products in the cart take to checkout page.

Related

Auto redirection after login except on WooCommerce checkout page

I have a problem with WooCommerce. As default WooCommerce redirects users to My Account page after login.
I have set up a custom URL for users to be redirected after login by using this code:
// Woocommerce redirect to panel klienta after woocommerce redirect
add_filter('woocommerce_login_redirect', 'wc_login_redirect');
function wc_login_redirect( $redirect_to ) {
$redirect_to = 'https://example.com';
return $redirect_to;
}
This works but the problem is that WooCommerce also auto redirects users when they log in on the checkout page.
This is bad because they've just added a product and want to pay for it.
How do I keep my custom redirect URL after login for users except for the checkout page where they should just login, not be redirected anywhere and just finish their order?
You can use:
wc_get_checkout_url() – Gets the url to the checkout page
Then you can compare it with $redirect_to, and if it is equal, return the url of $redirect_to instead of the modified url
So you get:
function filter_woocommerce_login_redirect( $redirect_to ) {
// Gets the url to the checkout page
if ( $redirect_to == wc_get_checkout_url() ) return $redirect_to;
// My url
$redirect_to = home_url( '/myurl/' );
return $redirect_to;
}
add_filter( 'woocommerce_login_redirect', 'filter_woocommerce_login_redirect', 10, 1 );

Check and redirect to log in before checkout redirecting on wrong URL

I am using woocommerce and I have created a login and register page. I am not using any plugin for this.
My issue is, I have to check and redirect to log in before checkout I refer to this code(WooCommerce check and redirect to login before checkout)
I am using the below code
add_action('template_redirect', 'check_if_logged_in');
function check_if_logged_in() {
$pageid = 10; // your checkout page id
if (!is_user_logged_in() && is_page($pageid)) {
$url = add_query_arg(
'redirect_to',
get_permalink($pagid),
site_url('/my-account/') // your my acount url
);
wp_redirect($url);
exit;
}
}
but when I click Proceed to checkout then I am getting the URL like
http://examle.com/my-account/?redirect_to=http://example.com/index.php/checkout/
You could do it in two steps.
If the user goes to checkout and is not logged in, he is redirected to the my-account page for login or registration.
// if the user is not logged in, return him to login before checkout
add_action('template_redirect', 'check_if_logged_in');
function check_if_logged_in() {
if ( ! is_user_logged_in() && is_checkout() ) {
$url = site_url('/my-account/');
wp_redirect( $url );
exit;
}
}
After login or registration, if the cart is not empty, the user is redirected to checkout.
// redirect to checkout after login (or registration)
add_filter( 'woocommerce_registration_redirect', 'redirect_after_login_or_registration_to_checkout_page' );
add_filter( 'woocommerce_login_redirect', 'redirect_after_login_or_registration_to_checkout_page' );
function redirect_after_login_or_registration_to_checkout_page() {
// only if the cart is not empty
if ( ! WC()->cart->is_empty() ) {
return wc_get_page_permalink( 'checkout' );
} else {
return home_url();
}
}
The code has been tested and works. Add it to your theme's functions.php.

When Logged Out how to change the link of my account menu Woocommerce

Can someone suggest me if someone logged out from WordPress Woocommerce then click on my account how to change the link?
I want instead of going to the login register page to the other page but only when the user logged out if user login it is fine
Thanks
Use below code will work :
function iconic_account_menu_items( $items ) {
if ( !is_user_logged_in() ) {
$items['information'] = __( 'your_page_name', 'page' );
}
return $items;
}
add_filter( 'woocommerce_account_menu_items', 'iconic_account_menu_items', 10, 1 );

Allow access to more than one page in PHP/Wordpress

I want to allow access to two areas. Signup and Signin within Woocommerce. If there user is not logged in they are redirected to the accounts page to select to either signup or login. But i want the signup on a seperate page or link within myaccount/signup
add_action( 'template_redirect', function() {
if( ( !is_page('accounts', 'signup') ) ) {
if (!is_user_logged_in() ) {
wp_redirect( site_url( '/accounts' ) ); // redirect all...
exit();
}
}
});
Is there something i am missing?
I am not sure if this the correct way to do this, but it worked for me. I am no expert so take this example with a grain of salt.
If will force a login if the user goes to any other page than signup or accounts/login.
// Redirect to accounts login page, if user is not logged in.
add_action( 'template_redirect', function() {
if( ( !is_page('accounts') && !is_page('signup') ) ) {
if (!is_user_logged_in() ) {
wp_redirect( site_url( '/accounts' ) ); // redirect all...
exit();
}
}
});

Disable checkout page for not logged in users

In WooCommerce, I'm trying to find a way to disable the woocommerce checkout page for non logged users, OR when they try to checkout they get redirected to the log in page.
So they should log in first to be able to continue their checkout.
Is that possible?
Thanks
Is possible to redirect non logged customers that trying to access to checkout with this code:
add_action( 'template_redirect', 'checkout_redirect_non_logged_to_login_access');
function checkout_redirect_non_logged_to_login_access() {
// Here the conditions (woocommerce checkout page and unlogged user)
if( is_checkout() && !is_user_logged_in()){
// Redirecting to your custom login area
wp_redirect( get_permalink( get_option('woocommerce_myaccount_page_id') ) );
// always use exit after wp_redirect() function.
exit;
}
}
Then you can display a custom notice in cart page with a button linked to login area, to avoid customer frustration. Is better to warn customer before, than after.
// Displaying a message on cart page for non logged users (Optional)
add_action( 'woocommerce_before_cart', 'customer_redirected_displaying_message');
function customer_redirected_displaying_message() {
if( !is_user_logged_in() ){
// HERE Type your displayed message and text button
$message = __('To access checkout, you need first to be logged in', 'woocommerce');
$button_text = __('Login area', 'woocommerce');
$cart_link = get_permalink( get_option('woocommerce_myaccount_page_id') );
wc_add_notice( $message . '' . $button_text . '', 'notice' );
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
The code is tested and works.

Categories