Search
Other Conditional Content questions
Forum

Assign conditon ouput to php variable

Marc Pradel's Avatar Marc Pradel
Hi.

I wonder if it might be possible to use the output of this condition in a php variable in a php frontend template file?!

{show countries="Germany"}
19
{show-else countries="Austria"}
20
{show-else}
18
{/show}

Regards,
Marc
Peter van Westen's Avatar Peter van Westen ADMIN
No, but you can use the tags inside the html you output with the php file (if rendered through Joomla).
Please post a rating at the Joomla! Extensions Directory
Marc Pradel's Avatar Marc Pradel
Yes, this works. But I need the geolocation condition output to run a php calculation on this.
This is a 3rd party template file (j2store), where I have to manipulate the product price bases on geolocation.

Any Idea how I could achieve this, would be much appreciated. 🙂
Peter van Westen's Avatar Peter van Westen ADMIN
You can try something like:
$geo = (new RegularLabs\Library\GeoIp\GeoIp)->get();

$isGermany = $geo->countryCode == 'DE';
Please post a rating at the Joomla! Extensions Directory
Marc Pradel's Avatar Marc Pradel
Works fine! 🙂

THX. And have a nice weekend...
Peter van Westen's Avatar Peter van Westen ADMIN
You should probably do some type checking, in case the GeoIP object can't be created. If data for the IP is not found, or something else is not ok, the $geo can be false.

So:
$geo = (new RegularLabs\Library\GeoIp\GeoIp)->get();

$isGermany = $geo && isset($geo->countryCode) && $geo->countryCode == 'DE';

Or if you are running on PHP 8:
$geo = (new RegularLabs\Library\GeoIp\GeoIp)->get();

$isGermany = ($geo->countryCode ?? '') == 'DE';
Please post a rating at the Joomla! Extensions Directory
Marc Pradel's Avatar Marc Pradel
Okay. Here is my (newbie) syntax... 😉

$countryCode = $geo->countryCode ?? '';
if ($countryCode == 'AT') {
$rate = 20;
} elseif ($countryCode == 'DE') {
$rate = 19;
} else {
$rate = 25;
}
Peter van Westen's Avatar Peter van Westen ADMIN
If it works, then 👍

You could do:
$rate = match ($geo->countryCode ?? '') {
   'AT'    => 20,
   'DE'    => 19,
   default => 25,
};
Please post a rating at the Joomla! Extensions Directory
Marc Pradel's Avatar Marc Pradel
Nice! Top.

THX!!!
You can only post on the extension support forum if you have an active subscription and you log in

Buy a Pro subscription