3
votes

I'm developing my own theme Woocommerce , and now I need to do a redirect after logging in a particular page where I am using Woocommerce login form. To this I added the following function in functions.php

if (!is_account_page()) {
add_filter('woocommerce_login_redirect', 'redirect_after_login_cart');
   function redirect_after_login_cart(){
       wp_redirect( get_permalink( get_page_by_path(‘checkout’) ) );
       exit;
   }
}

I want only the login-cart page, do the redirect after login to the checkout page. The form of the my-account is still redirecting to the my-account page. The way it is , all my Woocommerce login forms are redirecting to the checkout page.  The conditional is not being respected.

Before going to the checkout page, I check if you are logged in or not. If you are logged in, redirect to checkout, but redirects to a page created with the login form. When logging on that page, instead of going to my account page, redirects to the checkout.

The default Woocommerce is every time you log in, go to my account, and I'm changing this form only if you are on page I created login-cart

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_page_by_path('login-cart') ) );
    exit;
   }
}

If anyone can help me , I will be very grateful .

3
I'm not too familiar with Woocommerce's filters, but the condition should either be inside the function, or around the add_filter() call - otherwise you risk calling a function that doesn't exist.Tim Malone
I've done that way too, and it did not work.Flávia Amaral

3 Answers

2
votes

I managed to solve my problem. Follow the code below:

add_action( 'woocommerce_login_form', 'redirect_user_to_checkout' );
function redirect_user_to_checkout() {
    $referer = get_permalink( get_page_by_path('finalizar-compra'));
if( $referer ) {
    if(!is_account_page()) { ?>
            <input type="hidden" name="redirect-user" value="<?php echo $referer; ?>"><?php
        }
    }
}



function custom_woocommerce_login_redirect_to_checkout_page( $redirect, $user ) {
    if( isset( $_POST['redirect-user'] ) ) {
        $redirect = esc_url( $_POST['redirect-user'] );
    }
    return $redirect;
}
add_filter( 'woocommerce_login_redirect', 'custom_woocommerce_login_redirect_to_checkout_page' );
2
votes

Based on 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:

  1. Customer goes to checkout
  2. If Logged In, continue to checkout. If not logged in, go to a page with login and register forms.
  3. Once in this page, if customer logs in or register, continue to Checkout.

First, i created a custom page with [woocommerce_my_account] shortcode (so now i have two different pages with this shortcode, the 'My Account' page, and this custom page). 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: user not logged in and try to go to checkout
    if ( !is_user_logged_in() && is_checkout() )
        wp_redirect( get_permalink( get_page_by_path('iniciar-sesion-registrarse') ) );

  // Caso 2: if user login or register in my custom login page, or has empty cart, i redirect back to checkout.
  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 login or register in this page then i redirect users who are logged in and are in this page to checkout (if i dont redirect them to checkout on this custom login page, my account page should be displayed in this page).

I also redirect them to checkout if someones access this custom page directly (without trying to checkout) 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 (Incorrect email, email does not exist, etc...), will display normally as if it was real 'my-account' page, but once logged in in this custom page, it will redirect to checkout (instead of showing my account page).

I hope someone can tell me if i have missed something in the process. Thx

EDIT: if u are using caché plugin, dont forget to never caché this custom login page

0
votes

I am not sure why use condition you can try this

add_filter( 'woocommerce_login_redirect', 'redirect_after_login_cart' );

function redirect_after_login_cart( $default_redirect ){
  if( function_exists('wc_get_checkout_url') )
  $redirect_url = wc_get_checkout_url();
  else  
  $redirect_url = get_permalink( get_page_by_path(‘checkout’) ) ; 
  return $redirect_url;
}