Woocommerce 将中文版国家列表修改为按拼音排序

// 国家排序
add_filter( 'woocommerce_sort_countries', '__return_false' );
add_filter( 'woocommerce_countries',  'wc_custom_countries_order', 10, 1 );
function wc_custom_countries_order( $countries ) {

    if (ICL_LANGUAGE_CODE == 'zh-hans') {

        unset($countries['UM']);
        $countries = ['UM' => '美国本土外小岛屿'] + $countries;

        foreach ($countries as $key=>$value) {
            $new_array[$key] = iconv('UTF-8', 'GBK', $value);
        }
        asort($new_array);
        $new_countries = [];
        foreach ($new_array as $key=>$value) {
            $new_countries[$key] = iconv('GBK', 'UTF-8', $value);
        }

        unset($new_countries['CN']);
        $countries = ['CN' => '中国'] + $new_countries;
    }

    return $countries;
}

112 total views, 2 views today

Revisions

Comments are closed.