Here’s a fixed version of your code. “Equal” comparisons require two or three equal signs depending on the comparison. In your case you only need two. In addition, your code should use “else if” on the second and third comparisons since there is no need to do the comparison if one above it was successful.
function wc_change_billing_country($order) {
if ($order->billing_country == 'DE') {
$order->billing_country = "Germany";
}
else if ($order->billing_country == 'CH') {
$order->billing_country = "Hurliburli";
}
else if ($order->billing_country == 'A') {
$order->billing_country = "Austria";
}
return $order->billing_country;
}
I also changed the layout of the function. If the billing country was something other than the three in your comparisons, your original function would return nothing, and that might break the code which called the function.
You could also use a switch statement instead of the multiple elseif checks…
function wc_change_billing_country($order) {
switch($order) {
case "DE":
$order->billing_country = "Germany";
break;
case "CH":
$order->billing_country = "Hurliburli";
break;
case "A":
$order->billing_country = "Austria";
break;
}
return $order->billing_country;
}
Thank you so much guys for your input.
@diondesigns your code works perfect thanks for the hint.
@wspencer your code did not work, it seems like it is changing nothing as the result is DE or CH or A
Sorry, I typed too quickly. You’re passing in the $order object to the wc_change_billing_country() function. The switch statement should run on the billing_country property of that object…not the object itself. I’ve changed the code below, if you’re interested.
function wc_change_billing_country($order) {
switch($order->billing_country) {
case "DE":
$order->billing_country = "Germany";
break;
case "CH":
$order->billing_country = "Hurliburli";
break;
case "A":
$order->billing_country = "Austria";
break;
}
return $order->billing_country;
}