Wordpress教程 2023年05月29日
0 收藏 0 点赞 1,309 浏览 1059 个字

要在 WooCommerce 结账页面上只显示特定的国家/地区,您可以使用以下代码在函数文件(functions.php)中添加相应的过滤器。

保留的国家(美国 英国 加拿大 澳大利亚 法国 德国 意大利 )

add_filter('woocommerce_checkout_fields', 'custom_checkout_fields');

function custom_checkout_fields($fields) {
    $allowed_countries = array('US', 'GB', 'CA', 'AU', 'FR', 'DE', 'IT');
    
    // 检查账单地址字段
    if (isset($fields['billing']['billing_country'])) {
        $fields['billing']['billing_country']['type'] = 'select';
        $fields['billing']['billing_country']['options'] = array_intersect_key(
            WC()->countries->get_countries(),
            array_flip($allowed_countries)
        );
    }
    
    // 检查送货地址字段
    if (isset($fields['shipping']['shipping_country'])) {
        $fields['shipping']['shipping_country']['type'] = 'select';
        $fields['shipping']['shipping_country']['options'] = array_intersect_key(
            WC()->countries->get_countries(),
            array_flip($allowed_countries)
        );
    }
    
    return $fields;
}

在上面的代码中,我们首先定义了一个包含保留国家/地区代码的数组 $allowed_countries。然后,通过修改 $fields['billing']['billing_country']$fields['shipping']['shipping_country'] 数组元素,我们将账单地址和送货地址的国家字段设置为下拉选择框,并只保留了指定的国家/地区选项。

请确保将代码中的 $allowed_countries 数组替换为您所需的国家/地区代码列表。

保存并上传修改后的函数文件,然后在 WooCommerce 结账页面上只会显示指定的国家/地区作为选择项。

 

微信扫一扫

支付宝扫一扫

版权: 转载请注明出处:https://www.mizhanw.com/blog/2472.html

相关推荐
在WooCommerce中添加“立即购买”按钮直接跳转到结算页面
add_action( 'woocommerce_after_add_to_cart_button', 'add_content_after…
日期:2024-10-19 点赞:0 阅读:15
实现 WordPress 自动更新所有文章的发布日期为当天的日期
//二开自动更新文章日期为当天发布。 //设置宝塔面板中的定时任务,通过访问带有?update_posts=run的链接触发文章更新.如:c…
日期:2024-09-22 点赞:0 阅读:58
WooCommerce订单列表显示购买产品
// 为 Legacy CPT-based 订单添加自定义列 add_filter('manage_edit-shop_order_colu…
日期:2024-09-20 点赞:0 阅读:115
WooCommerce Order 类的所有Get方法,以面向对象的方法获取订单数据
在开发WooCommerce插件或者与第三方系统交互时,我们需要获取 WooCommerce 订单的数据,WooCommerce的 WC_O…
日期:2024-09-20 点赞:0 阅读:86
为WooCommerce后台订单列表添加按自定义字段功能
  // 添加自定义列到订单列表 add_filter('manage_woocommerce_page_wc-orders_co…
日期:2024-09-20 点赞:0 阅读:72
添加删除 WooCommerce 数据列表中的数据列
转载:添加删除 WooCommerce 数据列表中的数据列 _WordPress智库 (wpzhiku.com) 在 WooCommerce…
日期:2024-09-20 点赞:0 阅读:93
发表评论
暂无评论

还没有评论呢,快来抢沙发~