Non logged in users Woocommerce, redirection - php

I'm doing a shop online with Woocommerce, where my client ask me he don't want people to be able to register, he will create a user and password for each one of his clients. That way he can control who buys on his shop.
So I went into Woocommerce and disable the registration at checkout and everywhere, and the option to allow guests to place orders. Everything works fine, except that when someone tries to place an order, when logged out, when he tries to go to the checkout page, it just shows an unformatted message saying "You must be logged in to place an order". Is there a way where I can redirect not logged in customers to login page, when trying to access checkout?

May be this code could be more compact, simple and convenient:
add_action('template_redirect','check_if_logged_in');
function check_if_logged_in() {
if(!is_user_logged_in() && is_checkout())
wp_redirect( get_permalink( get_option('woocommerce_myaccount_page_id') ) );
}
This code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and fully functional.
Reference: WooCommerce login redirect based on cart

Use the following code in functions.php
add_action( 'template_redirect', 'redirect_user_to_login_page' );
function redirect_user_to_login_page(){
// Make sure your checkout page slug is correct
if( is_page('checkout') ) {
if( !is_user_logged_in() ) {
// Make sure your login page slug is correct in below line
wp_redirect('/my-account/');
}
}
}

I manage to do it my self! But still if someone has a cleaner code to do so, I'm open to suggestions.
What I did was to paste the following code on the functions.php file of the child theme:
add_action('template_redirect','check_if_logged_in');
function check_if_logged_in()
{
if(!is_user_logged_in() && is_checkout())
{
$url = add_query_arg(
'redirect_to',
get_permalink($pagid),
site_url('/my-account/')
);
wp_redirect($url);
exit;
}
}
Thanks
kind regards

Related

Redirect not - logged in users to custom login page

I'm currently building my own website using wordpress.
I designed my own login - page and want users to be redirected to that page if they are not logged in.
There are several solutions to this problem, however, none of them seem to work.
I tried:
Adding code to the functions.php, such as:
<?php
function admin_redirect() {
if ( ! is_user_logged_in() ) {
wp_redirect( home_url( '/{custom page goes here}/' ) );
exit;
}
}
add_action( 'get_header', 'admin_redirect' );
It apparently didn't work - I also tried some other code like (to my header.php):
<?php
if ( ! is_user_logged_in() ) {
wp_redirect('http://somepagehere');
exit;
}
Use of plugins - I tried using "Force Login" and "WPS Hide Login".
First of all, installing "Force Login" didn't work when accessing the main page and only prohibited access when adding a slug to the main page (ex.: oho.nachhilfe.de/xyz). It also didn't redirect to the login page, but instead simply redirected to a 404 page (no matter what my login URL was).
Changing my login URL to a custom one using "WPS Hide Login" also didn't work, as this only replaced my custom login page with the default wordpress login page, therefore, my custom page simply got overwritten with the wordpress one.
Any ideas how to solve the issue?
Here's the page I need help with: https://www.oho-nachhilfe.de
First of all, let's all agree that the login customization handling in WordPress is awful.
Like Chris said, forward compatibility and messing with core don't go well together.
If it's just a permalink question you can always customize them instead of re-creating the whole login system.
Moving on. Both of your spinets are perfectly working, tho get_header is the wrong hook to use. Redirects needs to happen before determining which template to load. which is happening before get_header but after init and wp.
You can use, template_redirect do do just that.
Fires before determining which template to load.
<?php
add_action( 'template_redirect', function () {
if ( ! is_user_logged_in() && ! is_page( 'login' ) ) {
wp_safe_redirect( home_url( '/login/' ) );
exit;
};
} );
You can refer to the WP Plugin API/Action Reference to have a better understanding of the hook firing sequence.

Custom WooCommerce login and registration redirection for one page

I would like my WooCommerce login and registration form to redirect to a specific page (after login/registration), but only when the user is on a specific login page.
I have added the following code in my functions.php file:
if (is_page(6432)) {
add_filter('woocommerce_login_redirect', 'wc_login_redirect');
function wc_login_redirect( $redirect_to ) {
$redirect_to = 'http://www.eliv.com.tw/product/tripsignup';
return $redirect_to;
}
}
The function works without the if (is_page(6432)) on all pages. But I Would like to make this function only work on page 6432.
Also I want to adapt it for registration but so far I couldn't get this to work.
Any help would be very welcome.
You should try to add the condition inside your function, this way (without any guaranty):
add_filter('woocommerce_login_redirect', 'wc_login_redirect');
function wc_login_redirect( $redirect_to ) {
if (is_page(6432))
$redirect_to = home_url('/product/tripsignup/');
return $redirect_to;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

WooCommerce - Skip cart page redirecting to checkout page

Our website is kohsamuitour.net. I have added custom code to skip the cart page on checkout, which works for all sales. This code:
function wc_empty_cart_redirect_url() {
return 'https://www.kohsamuitour.net/all-tours/';
}
add_filter( 'woocommerce_return_to_shop_redirect', 'wc_empty_cart_redirect_url' );
Now that does the job, but we also have a possibility to check booking availability. That can be found on the pages of private charters, i.e. this one: https://www.kohsamuitour.net/tours/kia-ora-catamaran/ .
Here the customer is being redirected to the cart, where I don't want that to happen as this is not a sale.
How can I make sure the 'Check booking availability' is also redirected to the checkout straight away?
You can skip cart definitively, redirecting customers to checkout page when cart url is called.
To achieve this use this code snippet, that should do the trick:
// Function that skip cart redirecting to checkout
function skip_cart_page_redirection_to_checkout() {
// If is cart page, redirect checkout.
if( is_cart() )
wp_redirect( WC()->cart->get_checkout_url() );
}
add_action('template_redirect', 'skip_cart_page_redirection_to_checkout');
This code goes in function.php file of your active child theme (or theme) or also in any plugin file.
The code is tested and fully functional.
Edit: Since WooCommerce 3 replace wp_redirect( WC()->cart->get_checkout_url() ); by:
wp_redirect( wc_get_checkout_url() );

wordpress redirect to login form

I know that are some answers to my question but i don't know if my problem exists in the other persons who asked for help.
I have this function that helps me to redirect users to login before access any contetnt of my website:
function restrict_access_if_logged_out(){
if (!is_user_logged_in() && !is_home()){
wp_safe_redirect(wp_login_url(get_permalink()));
}
}
add_action( 'wp', 'restrict_access_if_logged_out', 3 );
That function works fine but i have a problem. When somebody is navigated to my home page and click to a link that goes here: 'example.com/about' then my function works great. When somebody he trying to go direct to 'example.com/about' by entering this into browser url then my function doesn't work and he is able to access my website. What i have do wrong? I have write my function in my templates function.php file.
I just created a page template called restriction_page.php:
<?php
/*
Template Name: My Restricted Page
*/
?>
<h1>Hello There</h1>
<?php
function restrict_access_if_logged_out(){
if (!is_user_logged_in() && !is_home()){
wp_safe_redirect(wp_login_url());
}
}
add_action( 'wp', 'restrict_access_if_logged_out', 3 );
?>
i set this template as the fefault template in my about page. No solution.
Any ideas?
You can use the built in auth_redirect() function for this:
function restrict_access_if_logged_out(){
if ( !is_user_logged_in() ){
auth_redirect();
}
}
add_action( 'wp', 'restrict_access_if_logged_out', 3 );
Note: This should go in your themes functions.php file
Method :1
you can add it to the top of page.php
If you want to add more pages, just add the ||(or) operator like so:
<?php
$pageTitle == get_the_title();
if($pageTitle == 'Submit' || $pageTitle =='NewPage') {
if (!is_user_logged_in()) {
redirect code
} else {
page code
}
} else {
page code
}
?>
Method:2
Write this into a plugin:
add_action( 'template_redirect', 'auth_redirect' );
This will force all visitors login if they aren’t already.
In some cases, this is asking for a log-in every time. This might work better:
add_action( 'template_redirect', function() {
is_user_logged_in() || auth_redirect();
});
You should create a template page for restricted page like we create full width page tamplate, right sidebar page template, left sidebar template, etc. And in restricted page template call your function. When going to create pages, choose restricted template from the listed of the page template in right side.
If it doesn't help you, we'll be able to help you, when you paste your more code with more detail.

Is there a way to only authorise logged user to see my whole WordPress site without plugin?

is there a way to only authorise logged user to see my whole WordPress site without plugin?
My goal is to see the login page if your are not logged-in or registered.
I found that on another post:
<?php if(!is_user_logged_in()){wp_redirect( 'http://www.your-blog.com/wp-login' ); exit;}?>
Whit this solution I have to put on top from all my pages. Is there a way to put just some line of code in the functions.php to get the same results?
Adding this to the end of my functions.php doesn't do the trick:
add_action( 'muplugins_loaded', 'my_plugin_override' );
function my_plugin_override() {
if(!is_user_logged_in()){
wp_redirect( wp_login_url() );
exit;
}
}
Don't know why, any Idea?
I have found a plugin who made the trick, but I want to try to understand how to get it work without plugin with a simple solution.
Thank you
Put below code into your current theme's functions.php file.
add_action( 'muplugins_loaded', 'my_plugin_override' );
function my_plugin_override() {
if(!is_user_logged_in()){
wp_redirect( 'http://www.your-blog.com/wp-login' );
exit;
}
}
muplugins_loaded is the earliest action hook.
For more dfetails check - https://wordpress.stackexchange.com/questions/162862/how-to-get-wordpress-hook-run-sequence

Categories