Sorting the country select in Shopp

Sorting the country select in Shopp

This came up recently while working on a site that had a bunch of target markets selected in Shopp. The country dropdown list on the Checkout page was in a somewhat random order, and I realized that it was displaying them in the order they were selected or saved. When I checked 3 new countries, they showed up at the bottom of the list. So, knowing that, we decided it would be better for everyone if we sorted the list alphabetically. The code is below:

/* Sort Countries in dropdown alphabetically */
add_filter( 'shopp_countries', 'my_country_sort' );
function my_country_sort( $_ ) {

	$countries = array_msort($_, array( 'name' => SORT_ASC ) );
	// Åland Islands (EUR) sits at the bottom, so move it to the top
	$countries = array('AX' => $countries['AX']) + $countries;
	return $countries;

}

You’ll note the comment about Åland Islands; ideally we want it to be listed with the other A countries, but I don’t know if there’s a way to sort special characters as their originating character in PHP. So, we pop it off the end of the array and push it back to the top.

Leave a Reply

Your email address will not be published. Required fields are marked *