WooCommerce uses select2 in the shipping and billing address field for state and country. I want to make it so that when you search for ka
the top result is Kansas
not Alaska
.
I checked the documentation for select2 and found this:
https://select2.org/searching#customizing-how-results-are-matched
But that solution is for when there are children. I don't know how to edit the documented example to make it work with WooCommerce. Another SO article suggests this:
function matchCustom(term, text) {
console.log('matcher is running');
if (text.toUpperCase().indexOf(term.toUpperCase()) == 0) {
return true;
}
}
jQuery( document ).ready(function() {
$(".state_select").select2({
matcher: matchCustom
});
});
With the above code, when I search for ka
the top result is Alaska
not Kansas
.
I looked at the following SO questions:
jquery select2 plugin how to match only beginning of word
jquery select2 plugin how to get only the result 'myString%'
select2 search - match only words that start with search term