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 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥15 Windows 系统cmd后提示“加载用户设置时遇到错误”
  • ¥50 vue router 动态路由问题
  • ¥15 关于#.net#的问题:End Function
  • ¥15 无法import pycausal
  • ¥15 VS2022创建MVC framework提示:预安装的程序包具有对缺少的注册表值的引用
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义
  • ¥15 嵌入式--定时器使用