Replies: 6 comments
-
No this appraoch to currency conversion won't work with a page cache.
2 alternatives:
- Either you convert this code into JS and use a JS currency converter
(as you suggest).
- Or, convert the prices into all available currencies in PHP, wrapping
them with conditional show/hide tags:
(I didn't test it, just to get you started ...)
<?php
$html = '|[geoip_detect2_show_if country="GB"]' . $price_in_gb .
'[/||geoip_detect2_show_if]';|
|$html .= '|[geoip_detect2_show_if continent="NA"]' . $price_in_us .
'[/||geoip_detect2_show_if]';||
echo do_shortcode($html);
|
Beta Was this translation helpful? Give feedback.
-
Many thanks for your help Benjamin! I've been trying to implement the shortcodes option but get an internal server error and message: syntax error, unexpected '|', expecting end of file in... The error points to the last pipe on the first $html variable. Here is the code I'm using: ` $price = get_field('price');
I wasn't sure if this was a formating issue and placed the double pipe at the end which returned error: On the second $html var, what does '.' before '=' relate to? Apologies for the questions. Just trying to understand how these operators works compared to my usual long winded php :) Many thanks again for your time |
Beta Was this translation helpful? Give feedback.
-
IMO there should be no || pipes at all ...
On 16.06.21 14:28, Mulegoat wrote:
Many thanks for your help Benjamin!
I've been trying to implement the shortcodes option but get an
internal server error and message:
syntax error, unexpected '|', expecting end of file in...
The error points to the last pipe on the first $html variable.
Here is the code I'm using:
` $price = get_field('price');
|$price_in_eu = get_conversion( 'number='. $price . '&from=gbp&to=eur'
); $price_in_us = get_conversion( 'number='. $price .
'&from=gbp&to=usd' ); $price_in_gb = $price; $html =
'|[geoip_detect2_show_if country="GB"]' . $price_in_gb .
'[/||geoip_detect2_show_if]';| // Should there be a break here? |$html
.= '|[geoip_detect2_show_if continent="NA"]' . $price_in_us .
'[/||geoip_detect2_show_if]';|| echo do_shortcode($html);` |
I wasn't sure if this was a formating issue and placed the double pipe
at the end which returned error:
syntax error, unexpected '||' (T_BOOLEAN_OR), expecting end of file in
the same spot.
On the second $html var, what does '.' before '=' relate to?
Apologies for the questions. Just trying to understand how these
operators works compared to my usual long winded php :)
Many thanks again for your time
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#134 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAB3WPR6AWAO3P4FXLE5Y7DTTCKHLANCNFSM46Y7GGIA>.
--
Mein Blog: https://benjaminpick.de
|
Beta Was this translation helpful? Give feedback.
-
Then perhaps they were added automatically in place of spaces (see your code above)? Anyway, I've removed these and got things working: ` $price_in_eu = get_conversion( 'number='. $price . '&from=gbp&to=eur' ); $html_GB = '[geoip_detect2_show_if continent="EU" operator="and" country="GB"]£' . $price_in_gb . '[/geoip_detect2_show_if]'; echo do_shortcode($html_GB); Is this what you meant by converting all currencies and displaying conditionally? The only issue I'm finding is that when loading content via ajax after the initial page load the show/hide conditions are no longer working. Here is an example of the html output: `<span class="js-geoip-detect-show-if" style="display: none !important" data-options="{"skip_cache":false,"lang":["en","en"],"default":","parsed":{"op":"and","not":0,"conditions":[{"p":"continent","v":"eu"},{"p":"country","v":"gb"}]}}">£6540 <span class="js-geoip-detect-show-if" style="display: none !important" data-options="{"skip_cache":false,"lang":["en","en"],"default":","parsed":{"op":"and","not":1,"conditions":[{"p":"continent","v":"eu"},{"p":"country","v":"gb"}]}}"> <span class="js-geoip-detect-show-if" style="display: none !important" data-options="{"skip_cache":false,"lang":["en","en"],"default":","parsed":{"op":"and","not":0,"conditions":[{"p":"continent","v":"eu"},{"p":"country","v":"gb","not":1}]}}">€7,620 <span class="js-geoip-detect-show-if" style="display: none !important" data-options="{"skip_cache":false,"lang":["en","en"],"default":","parsed":{"op":"and","not":1,"conditions":[{"p":"continent","v":"eu"},{"p":"country","v":"gb","not":1}]}}"> <span class="js-geoip-detect-show-if" style="display: none !important" data-options="{"skip_cache":false,"lang":["en","en"],"default":","parsed":{"op":"and","not":0,"conditions":[{"p":"continent","v":"na"}]}}">$9,077 <span class="js-geoip-detect-show-if" style="display: none !important" data-options="{"skip_cache":false,"lang":["en","en"],"default":","parsed":{"op":"and","not":1,"conditions":[{"p":"continent","v":"na"}]}}"> Is there something I'm missing here? Do I need to call a particular JS method in a callback once the additioanl ajaxed content has been loaded? |
Beta Was this translation helpful? Give feedback.
-
Do you see error messages in the JS console ?
You need to enqueue the frontend JS (either globally or just on that page).
On 18.06.21 12:04, Mulegoat wrote:
Then perhaps they were added automatically in place of spaces (see
your code above)? Anyway, I've removed these and got things working:
` $price_in_eu = get_conversion( 'number='. $price . '&from=gbp&to=eur' );
$price_in_us = get_conversion( 'number='. $price . '&from=gbp&to=usd' );
$price_in_gb = $price;
$html_GB = '[geoip_detect2_show_if continent="EU" operator="and"
country="GB"]£' . $price_in_gb . '[/geoip_detect2_show_if]';
$html_EU = '[geoip_detect2_show_if continent="EU" operator="and"
not_country="GB"]€' . $price_in_eu . '[/geoip_detect2_show_if]';
$html_US = '[geoip_detect2_show_if continent="NA"]$' . $price_in_us .
'[/geoip_detect2_show_if]';
echo do_shortcode($html_GB);
echo do_shortcode($html_EU);
echo do_shortcode($html_US);`
Is this what you meant by converting all currencies and displaying
conditionally?
The only issue I'm finding is that when loading content via ajax after
the initial page load the show/hide conditions are no longer working.
Here is an example of the html output:
`<span class="js-geoip-detect-show-if" style="display: none
!important"
data-options="{"skip_cache":false,"lang":["en","en"],"default":","parsed":{"op":"and","not":0,"conditions":[{"p":"continent","v":"eu"},{"p":"country","v":"gb"}]}}">£6540
<span class="js-geoip-detect-show-if" style="display: none !important"
data-options="{"skip_cache":false,"lang":["en","en"],"default":","parsed":{"op":"and","not":1,"conditions":[{"p":"continent","v":"eu"},{"p":"country","v":"gb"}]}}">
<span class="js-geoip-detect-show-if" style="display: none !important"
data-options="{"skip_cache":false,"lang":["en","en"],"default":","parsed":{"op":"and","not":0,"conditions":[{"p":"continent","v":"eu"},{"p":"country","v":"gb","not":1}]}}">€7,620
<span class="js-geoip-detect-show-if" style="display: none !important"
data-options="{"skip_cache":false,"lang":["en","en"],"default":","parsed":{"op":"and","not":1,"conditions":[{"p":"continent","v":"eu"},{"p":"country","v":"gb","not":1}]}}">
<span class="js-geoip-detect-show-if" style="display: none !important"
data-options="{"skip_cache":false,"lang":["en","en"],"default":","parsed":{"op":"and","not":0,"conditions":[{"p":"continent","v":"na"}]}}">$9,077
<span class="js-geoip-detect-show-if" style="display: none !important"
data-options="{"skip_cache":false,"lang":["en","en"],"default":","parsed":{"op":"and","not":1,"conditions":[{"p":"continent","v":"na"}]}}">
`
Is there something I'm missing here? Do I need to call a particular JS
method in a callback once the additioanl ajaxed content has been loaded?
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#134 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAB3WPTFTSOJCVAA7ARGOIDTTMK4JANCNFSM46Y7GGIA>.
--
Mein Blog: https://benjaminpick.de
|
Beta Was this translation helpful? Give feedback.
-
No errors. Frontend JS is being loaded. I've tested this with simple console logs: ` geoip_detect.get_info().then(function(record) {
` On first page load everything works perfectly, when I make an additional request to load content via ajax (this is done via the facetWP plugin) the currency conversion works but the geoip evaluation is evaulating everything to false. I tried adding ajax=1 to the shortcodes but same result. |
Beta Was this translation helpful? Give feedback.
-
Hi there,
Just looking for some clarification on how to use AJAX mode. I only need it to add country and continent classes to the body.
Plugin v 4.1.0 with WP 5.6.4 and W3 Total Cache using API type: Country as data source for the Maxmind Precision Web API.
In General options I have enabled AJAX endpoint to get current IP on cached pages, JS Helper functions enabled for all pages & Country specific classes to body tag.
In my php templates I'm using geoip_detect2_get_info_from_current_ip() in order to conditionally call a third party method 'get_conversion' for currency conversion like this:
`
$price = get_field('price');
`
My question is will this approach work on cached pages or do I need to rewrite the above code to use the JS API instead? The currency conversion tool I'm currently using does not have a JS API so it would mean using a different curency converter that uses the Open Currency Exchange API.
I've tested this locally and it seems to work but it's hard to check against cached pages without testing in production.
Many thanks!
Beta Was this translation helpful? Give feedback.
All reactions