dowb58485 2018-09-11 00:39
浏览 124
已采纳

让我们首先在Woocommerce结帐国家选择字段

I already have US selected as my default country in the woocommerce checkout. In addition to that, I was asked to move 'US' to the very top of the country list in the checkout form.

I created a new filter and hooked into 'woocommerce_countries' hook like this:

function change_country_order_in_checkout_form($countries)
{
    $countries = array('US' => $countries['US']) + $countries;

    return $countries;
}

add_filter( 'woocommerce_countries', 'change_country_order_in_checkout_form' );

My list of countries gets modified correctly, but then something in WooCommerce sorts the countries alphabetically and I want to avoid that. I tried adding:

remove_filter('woocommerce_sort_countries', 'wpautop');

but that did not seem to make any difference. Any help is appreciated.

  • 写回答

1条回答 默认 最新

  • dsfds2353 2018-09-11 01:03
    关注

    To avoid ordering, you need to use woocommerce_sort_countries filter hook this way:

    add_filter('woocommerce_sort_countries', '__return_false');
    

    And to set "US" first, try this instead:

    add_filter( 'woocommerce_countries', 'change_country_order_in_checkout_form' );
    function change_country_order_in_checkout_form($countries)
    {
        $usa = $countries['US']; // Store the data for "US" key
        unset($countries["US"]); // Remove "US" entry from the array
    
        // Return "US" first in the countries array
        return array('US' => $usa ) + $countries;
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested and works.

    enter image description here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?