Woocommerce guest checkout not redirecting to order-received page - wordpress

I have created a flow using a function that when someone try to make an order without login he/she is redirected to a custom login-register pages where they choose to either login/register or continue as a guest - see function code below, now when someone makes an order he/she gets redirected to custom login-register page instead of order-received page. What's wrong here?
function ace_redirect_pre_checkout() {
if ( class_exists( 'woocommerce' ))
$redirect_page_id = 104549;
if (!is_user_logged_in() && is_page(17)) {
wp_redirect(get_permalink($redirect_page_id));
} elseif (is_user_logged_in() && is_page($redirect_page_id)) {
wp_redirect(get_permalink(wc_get_page_id('checkout')));
}
}
add_action('template_redirect', 'ace_redirect_pre_checkout');

Related

Want to make a wordpress accessible to view only thorugh redirection from a particular page and cannot be viewed through its own page url

I want to create a function on my wordpress site that a particular page 'x' can be viewed or acessible only through redirection from a particular page 'y' and connot be viewed through its own url (only redirecton should be the way to view that page 'x')
I have tried this but no result what is wrong with it please check:
is_page(776) {
$referer = wp_get_referer(); if ( 'https://xnd.vtgcindia.online/' !== $referer ) { // User arrived from another source so send them away! wp_redirect( 'http://xnd.vtgcindia.onlne/' ); exit; }
}
(Note: Page "X" represent page to be viewed and Page "Y" represent page through which user get redirection or can visit. I use this for easy explanation of the situation)
Hope you answer and help!
is_page(776) {
$referer = wp_get_referer(); if ( 'https://xnd.vtgcindia.online/' !== $referer ) { // User arrived from another source so send them away! wp_redirect( 'http://xnd.vtgcindia.onlne/' ); exit; }
}

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.

WooCommerce check and redirect to login before checkout

The default Woocommerce checkout behavior breaks a lot of web conventions by showing the "Create Account" boxes on the checkout page if it detects the user is not logged in. New users may not know what to do without any added instructions.
My desired sequence would be:
User Checkout > Check login >
Proceed to checkout if logged in.
Redirect to login/register page if not logged in > proceed to the checkout page.
This is EXACTLY the case in WooCommerce login redirect based on cart
However, what I feel uncomfortable is that, in the above case, once the user logged in, he/she will be redirected to the checkout page if the cart is not empty. If the user's cart is not empty, he/she will not be able to go to MyAccount at all even though he/she does not want to checkout yet.
Any idea on this one?
Try this no need to change core code
add_action('template_redirect','check_if_logged_in');
function check_if_logged_in()
{
$pageid = get_option( 'woocommerce_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 account url
);
wp_redirect($url);
exit;
}
if(is_user_logged_in())
{
if(is_page(get_option( 'woocommerce_myaccount_page_id' )))
{
$redirect = $_GET['redirect_to'];
if (isset($redirect)) {
echo '<script>window.location.href = "'.$redirect.'";</script>';
}
}
}
}
In the second answer there is a problem, after loging in the user always redirects checkout page, it does not stand on my-account page until the cart become empty!!
This code redirects the user to my-account page for login instead of checkout if the user is not logged in, then after loging in it automatically redirects to checkout page.
STEP 1:
Add this code in function.php
add_action('template_redirect','check_if_logged_in');
function check_if_logged_in()
{
$pageid = get_option( 'woocommerce_checkout_page_id' );// your checkout page id
if(!is_user_logged_in() && is_page($pageid))
{
$url = add_query_arg(
'redirect_to',
get_permalink($pageid),
site_url('/my-account/') // your my acount url
);
wp_redirect($url);
exit;
}
}
STEP 2:
Add this code at the end of /wp-content/plugins/woocommerce/templates/myaccount/my-account.php
<?php
$quark_web_solution_redirect = $_GET['redirect_to'];
if (isset($quark_web_solution_redirect)) {
echo '<script>
window.location.href = "'.$quark_web_solution_redirect.'";
</script>';
}
?>
after reading this post and others in Stackoverflow, i came up with a simple solution (more than i thought at first) that is working for me... Maybe theres something i have missed up in the process, but i have tested several combinations and it works for me. So we want to:
Customer goes to checkout
If Logged In, continue to checkout. If not logged in, go to a page with login and register forms.
Once in this page, if customer logs in or register, continue to Checkout.
First, i created a custom page with [woocommerce_my_account] shortcode. Here i customized the texts etc... in the forms, so its a little bit different that my account page. URL path of this page is -> /iniciar-sesion-registrarse.
Then, i put this code in functions.php of my child theme:
function custom_woocommerce_login_redirect_to_checkout_page() {
// Caso 1: Usuario no identificado intenta acceder a Finalizar Compra
if ( !is_user_logged_in() && is_checkout() )
wp_redirect( get_permalink( get_page_by_path('iniciar-sesion-registrarse') ) );
// Caso 2: Si en la página de identificarse, el cliente ha accedido o se ha registrado, o tiene el carrito vacío, continuar a Finalizar Compra.
if ( is_page('iniciar-sesion-registrarse') ) {
if( is_user_logged_in() || WC()->cart->is_empty() ) {
wp_redirect( get_permalink( get_page_by_path('finalizar-comprar') ) );
}
}
}
add_action( 'template_redirect', 'custom_woocommerce_login_redirect_to_checkout_page' );
So in Case 1: If user is not logged in and tries to go to checkout, it redirects to my custom login page 'iniciar-sesion-registrarse'.
Then in Case 2: If user loggin or register in this page, normal my account page would be displayed in this page, then i redirect users who are logged in and are in this page to checkout. I also redirect them to checkout if someones access this custom page directly and has nothing in their carts (so woocommerce message: u cant checkout without items in ur cart is displayed).
This way, while loggin in or registering in this custom page, error messages will display normally as if it was real 'my-account' page, but once logged in, it will redirect to checkout.
I hope someone can tell me if i have missed something in the process. Thx
Actually had this problem today & now have a solution.
Context: our site has a separate user registration system than WooCommerce, so we didn't want a duplicative process prompting users to signup twice.
First I'll give the brief solution then list the full steps for my situation.
Brief solution: enable guest checkout, (trust me) enable account creation during checkout, automatically generate username and password (most important). Then disable in email settings the "New Account" message. Now "users" can checkout with account details auto-generated, but they never know about it and that information is essentially dumped.
Screenshot of my Accounts & Privacy settings. Please note I also disabled the "New Account" message triggered in the Email settings for WC.
Add this code in function.php
add_action('template_redirect','check_if_logged_in');
function check_if_logged_in()
{
$pageid = get_option( 'woocommerce_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;
}
}

change homepage for logged-in users in wordpress

In wordpress i want a different homepage for logged-in users and those who are logged-out (simple changing the redirect url when somebody clicks on the title of my website). Is there a way of doing this by adding a code snippet in my themes functions.php? I tried:
add_filter('get_the_permalink','my_permalink_redirect');
function my_permalink_redirect($permalink) {
if( is_user_logged_in() ) {
global $post;
if ($post->ID == 1) {
$permalink = 'http://localhost/homepage-for-logged-users';
}
}
return $permalink;
}
unfortunately it doesn't work and it would be unhandy to change the function from localhost to my domain name when i upload the site to a live hosting. can somebody give me some advice? Thanks!
edit:I got it working with the pluging "login with ajax" where I could define redirects after logins and I putted this snippet in my functions.php.
// Redirect users who arent logged in and on a 404 to the home page for logged out users
function login_redirect() {
// Check to see if user in not logged in and on the home page for logged users
if(!is_user_logged_in() && is_404()) {
// If user is, Redirect to home page for logged out users.
wp_redirect(home_url('home-logged-out'));
exit;
}
}
// add the block of code above to the WordPress template
add_action( 'wp', 'login_redirect' );
*I made the pages for logged-in users private so logged-out users will see 404. Not a very clean method, but it works for now...
Set your default home page to the page logged in users should see.
Add this code to functions.php:
add_action('init', 'redirect_user');
// for users not logged in
function redirect_user(){
if( !is_user_logged_in() ) {
wp_redirect( 'http://www.YourSite.com/SomePage');
exit;
}
}
Logged in users will see what they should see, and those not logged in will be redirected.

Resources