Last active
June 1, 2016 13:17
-
-
Save timersys/f3f2253c104fe49f469c6b76d5c05a60 to your computer and use it in GitHub Desktop.
Custom dropdown widget for geotargeting plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Geotargeting dropdown | |
| * @link https://timersys.com/geotargeting/ | |
| * | |
| * @wordpress-plugin | |
| * Plugin Name: GeoTargeting Pro Custom Redirects | |
| * Plugin URI: https://timersys.com/geotargeting/ | |
| * Description: Geo Targeting custom dropdown widget values | |
| * Version: 1.0.0 | |
| * Author: Timersys | |
| * Author URI: https://timersys.com/geotargeting/ | |
| */ | |
| add_filter( 'geot/dropdown_widget/countries', 'custom_geo_dropdown_values' ); | |
| function custom_geo_dropdown_values( $countries ) { | |
| if( empty($countries) ) | |
| return $countries; | |
| foreach ($countries as &$country ) { | |
| switch ( $country->iso_code ) { | |
| case 'US': | |
| $country->country = 'USD'; | |
| break; | |
| case 'CA': | |
| $country->country = 'CAD'; | |
| break; | |
| case 'NZ': | |
| $country->country = 'NZD'; | |
| break; | |
| case 'AU': | |
| $country->country = 'AUS'; | |
| break; | |
| case 'IN': | |
| $country->country = 'INR'; | |
| break; | |
| default: | |
| // if non of the above then is a european country | |
| $country->country = 'EUR'; | |
| break; | |
| } | |
| } | |
| return $countries; | |
| } | |
| /** | |
| * Detect real user regions and set the default country so the dropdown shows correct currency | |
| */ | |
| add_filter( 'geot/user_data/calculated_data' , 'default_by_region'); | |
| function default_by_region( $data ) { | |
| global $region_calc; | |
| $country_code = $data['country']->isoCode; | |
| $regions = apply_filters('geot/get_regions', array() ); | |
| if( empty( $regions ) || ! is_array( $regions ) || empty( $country_code ) ) | |
| return $data; | |
| $user_region = 'USD'; | |
| foreach( $regions as $region ) { | |
| if( in_array( $country_code, $region['countries'] ) ) | |
| $user_region = $region['name']; | |
| } | |
| switch ( $user_region ) { | |
| case 'USD': | |
| $data['country']->isoCode = 'US'; | |
| $data['country']->name = 'United States'; | |
| break; | |
| case 'AU': | |
| $data['country']->isoCode = 'AU'; | |
| $data['country']->name = 'Australia'; | |
| break; | |
| case 'Rs. / INR': | |
| $data['country']->isoCode = 'IN'; | |
| $data['country']->name = 'India'; | |
| break; | |
| case 'NZD': | |
| $data['country']->isoCode = 'NZ'; | |
| $data['country']->name = 'New Zealand'; | |
| break; | |
| case 'CAD': | |
| $data['country']->isoCode = 'CA'; | |
| $data['country']->name = 'Canada'; | |
| break; | |
| case 'EUR': | |
| $data['country']->isoCode = 'UK'; | |
| $data['country']->name = 'United Kingdom'; | |
| break; | |
| default: | |
| $data['country']->isoCode = 'US'; | |
| $data['country']->name = 'United States'; | |
| break; | |
| break; | |
| } | |
| return $data; | |
| } | |
| // remove original user country from dropdown | |
| add_filter( 'geot/dropdown_widget/original_country_in_dropdown', '__return_false' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ok so i have tried this. Changed Default to India as the client wanted that. Fine, it works now.
_However, EUR is still an issue. So this is what my version looks like.
I changed line 56 from USD to INR
I changed line 92 from US to IN and 93 from United States to India.
From the widgets point of view.
I have kept the "Dropdown" region and added the countries
United States, India, Australia, New Zealand, Canada and Europe
The widget then populates around 30 odd EUD versions and dismissed the rest USD, AUD, INR etc
If i change Europe in the "Dropdown" region
United States, India, Australia, New Zealand, Canada and United Kingdom is does the same.
Either way if i use Europe or United Kingdom...it wont detect my my region if i browse from a European Country.._
`<?php
/**
Geotargeting dropdown
@link https://timersys.com/geotargeting/
*
@Wordpress-Plugin
Plugin Name: GeoTargeting Pro Custom Redirects
Plugin URI: https://timersys.com/geotargeting/
Description: Geo Targeting custom dropdown widget values
Version: 1.0.0
Author: Timersys
Author URI: https://timersys.com/geotargeting/
*/
add_filter( 'geot/dropdown_widget/countries', 'custom_geo_dropdown_values' );
function custom_geo_dropdown_values( $countries ) {
if( empty($countries) )
return $countries;
foreach ($countries as &$country ) {
}
return $countries;
}
/**
Detect real user regions and set the default country so the dropdown shows correct currency
*/
add_filter( 'geot/user_data/calculated_data' , 'default_by_region');
function default_by_region( $data ) {
global $region_calc;
$country_code = $data['country']->isoCode;
$regions = apply_filters('geot/get_regions', array() );
if( empty( $regions ) || ! is_array( $regions ) || empty( $country_code ) )
return $data;
$user_region = 'INR';
foreach( $regions as $region ) {
if( in_array( $country_code, $region['countries'] ) )
$user_region = $region['name'];
}
switch ( $user_region ) {
case 'USD':
$data['country']->isoCode = 'US';
$data['country']->name = 'United States';
break;
case 'AU':
$data['country']->isoCode = 'AU';
$data['country']->name = 'Australia';
break;
}
return $data;
}
// remove original user country from dropdown
add_filter( 'geot/dropdown_widget/original_country_in_dropdown', '__return_false' );`