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/' );
}
}
}
}
- I know this is a wrong approach since it will create an infinite redirect loop.
- 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?