php - Change WooCommerce default currency based on user role

624

I need to change the default currency in WooCommerce based on user roles in WordPress.

For the user role named "customer", the default price needs to be in SEK and for all others the price must be in DKK

I've tried a lot of different solutions but can't seem to find one that is working.

global $current_user;
 if (in_array('customer', $current_user->roles)) {

Don't know what to put here.. :D

 }

Right now i have no way of moving forward, can't seem to find anything on Google or StackOverflow which helps my situation.

I can't find a function that can change default currency programmaticly

640

Answer

Solution:

You can usewoocommerce_currency filter

add_filter('woocommerce_currency', 'set_role_currency', 200);
function set_role_currency($currency){
 global $current_user;
 if (in_array('customer', $current_user->roles)) { return 'SEK'; }
return $currency; //this will return your woocommerce default currency
}
780

Answer

Solution:

Hi please check below code.

add_filter('woocommerce_currency','ji_woocommerce_currency',10);
function ji_woocommerce_currency( $currency ){     
$user_info = get_userdata(get_current_user_id());
if ( $user_info->roles[0]=="administrator" ) { 
    return 'USD'; 
} elseif ( $user_info->roles[0]=="subscriber" ) { 
    return 'GBP'; 
} else {
    return 'EUR';
} 

}

People are also looking for solutions to the problem: mysql - PHP Populate Dropdown with Unique SQL Values Quickly

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.