There is plugin WooCommerce Product Price Based on Countries, which may do what you need.
‘woocommerce_init’ is action, not a shortcut. The latter will not work work for you. Try to troubleshoot the error message you get, as it should work.
The only problem might be timing. qtranx_setCountry must be executed after language is detected in action ‘plugins_loaded’. I am not sure when ‘woocommerce_init’ is executed – investigate it.
Hi John and thanks for your reply 🙂
I am already using this plugin and using the previous mentioned code from the plugin author as he wrote it in this thread here.
As far as I can see the qtranx_setCountry function is executed after the language is detected as it is at the bottom of my function.php file. The frontend of my website seems to be working fine when I use the add_action code block but the backend is broken an throwing a HTTP 500 error.
Any other ideas as to why this is and how I can fix it?
P.s. I am using the Oxygen theme by Laborator if that makes any difference
Hi, try this:
if ( ! is_admin() ) {
function qtranx_setCountry(){
$lang = qtranxf_getLanguage();
if ( $lang == 'en'){
WC()->customer->set_country('US');
} else{
WC()->customer->set_country('IS');
}
}
add_action( 'woocommerce_init', 'qtranx_setCountry' );
}
This code should probably be executed at front end only? Position of code within functions.php should not make difference in that regard, but filter/action within which it gets executed matters.
500 errors are normally easy to debug, since there is a clear error message in error_log, which you should lookup before bringing it into the discussion. It probably crashes because WC()->customer is not defined at admin side.
You have to find a right filter/action to execute this code within. Did you investigate when ‘woocommerce_init’ is firing? It probably gets executed in the very beginning at both front and admin, but you need front only.
Maybe ask Woocommerce which filter they would recommend for such purpose. Or read their docs, I am sure it is somewhere there. Or simply investigate it yourselves, or hire a developer with PHP skills to do it, if you do not wish to spend time on this.
I guess, I got on this thread by accident, mixing it up with other thread, sorry for the intrusion. Good luck to you to figure it out rather sooner!
P.S. In fact, I have just realized that making country to depend on language is probably not a good idea in general, since people speak any language in any country. You cannot assume that if a person speaks English, then they live in England 🙂 I would use a different method to set the country. Those are my two cents 🙂
Thank you both for your input 🙂
The code worked oscargare, everything is working as it should now. Thanks again!