更改WooCommerce结账国家字段默认选项的显示标签

3
如何编辑现有数组的默认值 - 我已经使用以下方法编辑了标签,但是无法使用此方法使选项属性起作用:
add_filter( 'woocommerce_checkout_fields', 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
    $fields['billing']['billing_country']['options value="default"'] = 'My new select prompt';
    $fields['billing']['billing_country']['label'] = 'Are you purchasing as a Company Y/N or from the UK?';
    
    return $fields;
}
1个回答

3

要在结账页面更改WooCommerce国家字段默认选项的显示值,您可以按照以下组合钩子的方式使用:

add_filter( 'woocommerce_form_field_country', 'filter_form_field_country', 10, 4 );
function filter_form_field_country( $field, $key, $args, $value ) {
    // Only in checkout page for "billing" city field
    if ( is_checkout() && 'billing_country' === $key ) {
        $search  = esc_html__( 'Select a country / region…', 'woocommerce' ); // String to search
        $replace = esc_html__( 'My new select prompt', 'woocommerce' ); // Replacement string
        $field   = str_replace( $search, $replace, $field );
    }
    return $field;
}

代码存放在当前使用的子主题(或主题)的functions.php文件中。已测试,可用。

然后您可以在您的问题代码中添加以下内容:

add_filter( 'woocommerce_checkout_fields', 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
    $fields['billing']['billing_country']['label'] = __('Are you purchasing as a Company Y/N or from the UK?', 'woocommerce');
    
    return $fields;
}

哇,非常感谢LoicTheAztec,这个方法太妙了:):):)使用旧式国家下拉字段来根据要求触发税收选项:如果来自英国-所有人增值税20%如果来自英国以外的地方并且购买公司-销售税为零如果来自英国以外的地方并且作为个人购买-销售税20%现在表现良好,无法找到执行此操作的插件。 - Peter Rossetti
1
会做 - 新来这里,不确定该怎么做。 - Peter Rossetti

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接