I am using Woocommerce wordpress plugin in my site. When I am in the Cart page and click "proceed to checkout" I am now going to checkout page. I want to change this flow as below.
If the user is not logged in, he should be taken to a different url. If user is logged in he will go to checkout page as usual.
Any help is appreciated.
First you create a page where you want to redirect the guest user, for example register
then write this code in your functions.php
function restrict_user() {
if (! is_user_logged_in() && (is_checkout())) {
wp_redirect("http://your site url.com/register/");
exit;
}
}
add_action('template_redirect', 'restrict_user');
Hope this will help you...
Related
I'm trying to redirect this login button to a different page upon log in success. How do I do that without a plugin?
I tried checking the Woo Commerce settings and LearnDash settings and it does not change. I tried looking in the PHP file but am not really sure where to start.
picture of login modal
You can use the filter "woocommerce_login_redirect", which will redirect the user after successful login.
Follow the code below:
add_filter( 'woocommerce_login_redirect', 'my_login_redirect' );
function my_login_redirect( $redirect_to ) {
$redirect_to = 'http://www.example.com/my-account/';
return $redirect_to;
}
Hope this will help!
At the moment if the user navigates to the default logout page it looks like this:
This is not consistent with the theme of my website so I would like this content inside my own custom page. Can this be done?
I have a plugin installed My Theme Plugin which is designed to let me specify a logout page but I do not know how to construct it.
Since this may help others, I'm adding my comments as an answer.
You need to initially use the logout_url filter - https://developer.wordpress.org/reference/hooks/logout_url/
This will allow you to set up a page when a user clicks on the logout link. Next, you simply create the page however you need to (basic WordPress page, special template, etc.).
On that page you would use wp_logout_url() to set the link for the Are you sure you want to logout text. e.g.
Logout
This would redirect the user to the home page after they've logged out.
Edit: shortcode to add to content:
function wpso58817718_add_logout_link( $atts ){
return 'Logout';
}
add_shortcode( 'logout_link', 'wpso58817718_add_logout_link' );
Then you can do [logout_link]
You'll have to update the end URL wp_logout_url( home_url() ) if you don't want it to go to the home page.
You can use below code to redirect user to your specific url after logout
add_action('wp_logout','ps_redirect_after_logout');
function ps_redirect_after_logout(){
wp_redirect( 'your url here' );
exit();
}
i installed dokan multi vendor plugin and revo theme on wordpress, whenever i use the multi-vendor account to login using the login widget, it takes to me the customer dashboard.
but on the regular login page ,it takes me to the correct url
I tried placing this code in themes/functions.php but didn't help, Pls I need assistance
add_filter( 'woocommerce_login_redirect', 'ckc_login_redirect', 10, 2 );
function ckc_login_redirect( $redirect_url, $user ) {
// Change this to the url to Updates page
if( $user->roles[0] == 'seller' ) {
return dokan_get_navigation_url(‘dashboard’);
}
return $redirect_url;
}
As you are not using the WooCommerce default my-account page for that reason, your vendor is not redirecting to the Dashboard page. I beleive that your theme login widget is using the default WordPress login function. So, I will suggest you to follow this function reference to redirect vendor to dashboard-https://codex.wordpress.org/Plugin_API/Filter_Reference/login_redirect
Thanks :)
I am using Woocommerce and am having problems disabling the guest checkout.
I have made sure 'Enable Guest Checkout' is unticked in the settings page but people are still able to checkout without creating an account or logging in.
Is there somewhere else I should be setting this?
Go to WooCommerce > Accounts and untick Enable registration on the "Checkout" page.
This should do the job!
Alternatively, you can add this code to your theme functions.php
add_action( 'template_redirect', 'woo_restirct_checkout' );
function woo_restirct_checkout() {
if ( !is_user_logged_in() && is_checkout() ) {
$my_account_url = get_permalink( get_option('woocommerce_myaccount_page_id') );
wp_redirect( $my_account_url );
exit;
}
}
Cheers,
Francesco
Also need to unticked the option from Woocommerce -> Accounts tab.
Where it says Enable registration on the "Checkout" page.
Try looking at the site in a different browser from the one you're developing on. I found that being logged in as a user in wordpress-admin, also made me an active, logged in user for the checkout.
I want ONLY users who are logged in to view my WordPress site and the code I'm using is:
add_action('template_redirect', 'admin_redirect');
function admin_redirect() {
if ( !is_user_logged_in()) {
auth_redirect();
}
}
PROMLEM: if I send a hyperlink every time the user clicks it they are asked to sign in again.
Can you not just put
auth_redirect();
into your functions.php file?
This should redirect users who aren't logged in to the log in page but remember which page they were trying to access. Then when they log in it should redirect them to their destination page.
Is this not working?
From my understanding of auth_redirect(), you shouldn't have to place that check around it. The function actually handles that check itself as shown in the Codex
Also, I'd recommend moving this function into your header. By simply calling auth_redirect() in your header (which should be called on each and every page anyways), you can check if the user is logged in. If not, they should be bounced to the wp-login page.
Edit:
add_action( 'template_redirect', function() {
is_user_logged_in() || auth_redirect();
});
The folks over at the WordPress Exchange are much better with this sort of stuff. Found this link here.