Woocommerce 覆盖 Shipping City Select 字段

Woocommerce overwriting Shipping City Select Field

我们正在尝试使用 docs.woothemes 上记录的方法修改默认的 Woocommerce Checkout Shipping City 字段,但 运行 遇到了问题。

我们用 select 下拉菜单替换了 shipping_city 文本字段。

在页面加载时,select 下拉菜单被替换为默认文本字段,如果可用,会自动填充用户之前的递送目的地。

但是,如果页面是 reloaded/refreshed,则文本字段将替换为新的且非常受欢迎的 select 下拉菜单。

我们使用几个 WordPress add_filter 函数过滤了该字段,并向上和向下更改了 priority(-999 到 999)。

我们有 运行 filter 我们的运输方式 Class

我们甚至禁用了浏览器自动完成表单,因为我们……好吧 运行 出于其他想法……

select 字段工作时……它工作得很好。运费得到更新,数据得到返回、存储和通过电子邮件发送。

使用的filters是:

add_filter( 'woocommerce_checkout_fields', array( $this, 'fn_name' ) );

add_filter( 'woocommerce_default_address_fields', array( $this, 'fn_name' ) );

并且 $field 数组看起来像:

$fields[ 'shipping' ][ 'shipping_city' ] = array(
                    'label' => __( 'Suburb/City', 'woocommerce' ),
                    'required' => FALSE,
                    'clear' => TRUE,
                    'type' => 'select',
                    'options' => $options_array,
                    'class' => array( 'update_totals_on_change' )
                );

                return $fields;

奇怪的是,当我们 运行 在同一字段上使用两个过滤器时; sendond 的标签被第一个覆盖了……去看看……哎呀,我希望我知道 Ajax……我认为它是 Ajax 但如果我知道 AJAX 我就会知道它是不是Ajax…

WordPress 版本 4.5.2 && WooCommerce 版本 2.5.5

关于最终接受的解决方案的附录:

@LoicTheAztec 建议和提供的代码运行良好,已包含在我们的实施中。

但是,问题是由于我们最初将 $field 过滤器包含在我们的运输方法 Class 中,该过滤器已连接到 shipping_class_init

为了解决这个问题,我们将新的 woocommerce_form_field_args 过滤器移到了一个单独的文件中,并在新的运输方式 Class 发挥其魔力后检索了我们的选项数组。

这应该与 woocommerce_form_field_args 钩子一起工作,这样:

add_filter( 'woocommerce_form_field_args', 'custom_form_field_args', 10, 3 );
function custom_form_field_args( $args, $key, $value ) { 
    if ( $args['id'] == 'billing_city' ) {
     $args = array(
            'label' => __( 'Suburb/City', 'woocommerce' ),
            'required' => FALSE,
            'clear' => TRUE,
            'type' => 'select',
            'options' => $options_array,
            'class' => array( 'update_totals_on_change' )
        );
    } // elseif … and go on 

    return $args;
};

这是默认的 $args 参数值:

$defaults = array(
    'type'              => 'text',
    'label'             => '',
    'description'       => '',
    'placeholder'       => '',
    'maxlength'         => false,
    'required'          => false,
    'autocomplete'      => false,
    'id'                => $key,
    'class'             => array(),
    'label_class'       => array(),
    'input_class'       => array(),
    'return'            => false,
    'options'           => array(),
    'custom_attributes' => array(),
    'validate'          => array(),
    'default'           => '',
);

参考文献:

  • Add custom css class to WooCommerce checkout fields

将此代码放入您的子主题中 function.php 应该可以完成工作

$city_args = wp_parse_args( array(
    'type' => 'select',
    'options' => array(
        'city1' => 'Amsterdam',
        'city2' => 'Rotterdam',
        'city3'  => 'Den Haag', 
    ),
), $fields['shipping']['shipping_city'] );

$fields['shipping']['shipping_city'] = $city_args;
$fields['billing']['billing_city'] = $city_args; // Also change for billing field

return $fields;

}
add_filter( 'woocommerce_checkout_fields', 'jeroen_sormani_change_city_to_dropdown' );