Support

Home Forums Event Espresso Premium sort country list

sort country list

Posted: June 3, 2017 at 10:25 am

Viewing 5 reply threads


AmritYoga2015

June 3, 2017 at 10:25 am

Hi, there is any way to sort the registration country list? I would like to place the bigger markets (United States and Canada) on top of the list!

ATM by default it is sorted name; this sends United States to the bottom of the input select.


Tony

  • Support Staff

June 6, 2017 at 8:49 am

Hi there,

Currently, it’s not possible to sort the country list through the admin, but you can do it through a filter we have available.

FHEE__EE_Country_Select_Input____construct__country_options

You’ll need to explicitly set the order you want to use at the top of the list (beginning of the array in the code) and then return the new order for the input to use.


AmritYoga2015

June 7, 2017 at 8:35 am

Thanks, Im posting here a code snippet in case somebody else need it:

/**
* Code snippets that extends the Event Espresso 4,
* this will move the most used counties to the top of the imput select.
*/
function sort_ee4_country_select($country_options, $this) {

$item_key = array_search(‘United States’, $country_options);
$country_options = moveElementInArray( $country_options, $item_key, 1 );

$item_key = array_search(‘Canada’, $country_options);
$country_options = moveElementInArray( $country_options, $item_key, 2 );

$item_key = array_search(‘India’, $country_options);
$country_options = moveElementInArray( $country_options, $item_key, 3 );

return $country_options;
}

function moveElementInArray($array, $toMove, $targetIndex) {
if (is_int($toMove)) {
$tmp = array_splice($array, $toMove, 1);
array_splice($array, $targetIndex, 0, $tmp);
$output = $array;
}
elseif (is_string($toMove)) {
$indexToMove = array_search($toMove, array_keys($array));
$itemToMove = $array[$toMove];
array_splice($array, $indexToMove, 1);
$i = 0;
$output = Array();
foreach($array as $key => $item) {
if ($i == $targetIndex) {
$output[$toMove] = $itemToMove;
}
$output[$key] = $item;
$i++;
}
}
return $output;
}

add_filter(‘FHEE__EE_Country_Select_Input____construct__country_options’, ‘sort_ee4_country_select’, 10, 2);


AmritYoga2015

June 7, 2017 at 8:37 am

/**
 * Code snippets that extends the Event Espresso 4,
 * this will move the most used counties to the top of the imput select.
 */
function sort_ee4_country_select($country_options, $this) {
	
	$item_key = array_search('United States', $country_options);
	$country_options = moveElementInArray( $country_options, $item_key, 1 );
	
	$item_key = array_search('Canada', $country_options);
	$country_options = moveElementInArray( $country_options, $item_key, 2 );
	
	$item_key = array_search('India', $country_options);
	$country_options = moveElementInArray( $country_options, $item_key, 3 );

	return $country_options;
}

function moveElementInArray($array, $toMove, $targetIndex) {
    if (is_int($toMove)) {
        $tmp = array_splice($array, $toMove, 1);
        array_splice($array, $targetIndex, 0, $tmp);
        $output = $array;
    }
    elseif (is_string($toMove)) {
        $indexToMove = array_search($toMove, array_keys($array));
        $itemToMove = $array[$toMove];
        array_splice($array, $indexToMove, 1);
        $i = 0;
        $output = Array();
        foreach($array as $key => $item) {
            if ($i == $targetIndex) {
                $output[$toMove] = $itemToMove;
            }
            $output[$key] = $item;
            $i++;
        }
    }
    return $output;
}

add_filter('FHEE__EE_Country_Select_Input____construct__country_options', 'sort_ee4_country_select', 10, 2);


Tony

  • Support Staff

June 7, 2017 at 9:22 am

Thank you for sharing your solution.

After I posted the above I had a quick look at using the hook I mentioned myself and came up with a different solution:

https://gist.github.com/Pebblo/aeae0b4fb66ee59e3bb8c41c3d1904eb

Either one should achieve similar results.


AmritYoga2015

June 7, 2017 at 10:36 am

code is more beauty in this last solution! thanks!

Viewing 5 reply threads

The support post ‘sort country list’ is closed to new replies.

Have a question about this support post? Create a new support post in our support forums and include a link to this existing support post so we can help you.

Event Espresso