0

I want to make sure that all customers who register on my WordPress (with WooCommerce) site fill up their billing information before doing anything else.

Whenever customers login for the first time, filling up the billing information should be the first thing they do.

To do this, I added the following code in my themes's function.php:

add_action( 'wp', 'is_billing_address_set' );


function is_billing_address_set()
{

    $curr_url = $_SERVER['PHP_SELF'];

    if(is_user_logged_in ()){

        if(strstr($curr_url, 'my-account/edit-address/billing') == false){

            $current_user_id = get_current_user_id ();

            $billing_country = get_user_meta ($current_user_id, 'billing_country', true);

            if($billing_country == null || $billing_country == false || $billing_country == ""){
                wp_redirect( 'https://localhost/my-account/edit-address/billing/' );
            }

        }

    }

}
  1. I know this is a wrong approach since it will create an infinite redirect loop.
  2. In my wp_redirect function I am hard coding the absolute URL so I will have to change this code everytime my server location changes.

How can I solve this problem correctly?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Shuaib
  • 779
  • 2
  • 13
  • 47

2 Answers2

1

---- (update) : This is tested and work perferctly

Finally you don't need any hook to do what you want, you just need to paste this snippet in your function.php file. With this approach, you will prevent:

  • Infinite redirect loop
  • Hardcoding the absolute URL
  • Simplified condition with empty()
  • Url errors on multi language website

I have test it, and it works just perfectly:

if(is_user_logged_in ()){
    // Queried URL
    $curr_url = home_url(add_query_arg(array(),$wp->request)); 
    // localisable "Edit billing information" path
    $edit_billing_path = __('/my-account/edit-address/billing', 'woocommerce');
    // Target URL
    $targ_url = home_url($edit_billing_path);

    if ( !strstr($curr_url, $targ_url) ) {
        $user_id = get_current_user_id ();
        $billing_country = get_user_meta ($user_id, 'billing_country', true);
        if ( empty( $billing_country ) ) {
            wp_redirect( $targ_url );
        }
    }
}

With this little piece of code you can avoid, as you want, logged user to do anything until they have filled and completed their billing information.


You can also use multiple conditions based on different billing fields to redirect user. This way each field has to be filled and completed. This is just an example, and you can add, replace or remove any field to feet your needs:

if(is_user_logged_in ()){
    // Queried URL
    $curr_url = home_url(add_query_arg(array(),$wp->request)); 
    // localisable "Edit billing information" path
    $edit_billing_path = __('/my-account/edit-address/billing', 'woocommerce');
    // Target URL
    $targ_url = home_url($edit_billing_path);

    if ( !strstr($curr_url, $targ_url) ) {
        $user_id = get_current_user_id ();

        // Using multiple condition based on user multiple billing fields
        $first_name = get_user_meta ($user_id, 'billing_first_name', true);
        $last_name = get_user_meta ($user_id, 'billing_last_name', true);
        $address = get_user_meta ($user_id, 'billing_address_1', true);
        $city = get_user_meta ($user_id, 'billing_city', true);
        $country = get_user_meta ($user_id, 'billing_country', true);
        if ( empty( $first_name ) && empty( $last_name ) && empty( $address ) && empty( $city ) && empty( $country ) ) {
            wp_redirect( $targ_url );
        }
    }
}

All code is tested and working.

References:

Community
  • 1
  • 1
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • It does not work. Anyway I do not want to use login hook because I want to prevent them from doing other things unless they fillout the information – Shuaib Jul 03 '16 at 04:42
0

This is the solution for plain url permalink and for preserving admins to be redirected in backend... maybe useful for those who don't get this working

if( is_user_logged_in () &&  !is_admin() ){
    // interroghiamo l'url corrente
 global $wp;
 $curr_url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    //$curr_url = home_url(add_query_arg(array(),$wp->request)); 
    // path per la localizzazione
 $edit_billing_path = __('/myaccount/edit-address/fatturazione', 'woocommerce');
 
    // Target URL
    $targ_url = home_url($edit_billing_path);

    if ( !strstr($curr_url, $targ_url) ) {
        $user_id = get_current_user_id ();
        $billing_city = get_user_meta ($user_id, 'billing_city', true);
        if ( empty( $billing_city ) ) {
            wp_redirect($targ_url);
   //echo $targ_url;
   //echo $curr_url;
        }
    }
}
Miss-Take
  • 43
  • 6