首页 文章

Woocommerce覆盖航运城市选择领域

提问于
浏览
1

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

我们已将 shipping_city 文本字段替换为 select 下拉菜单 .

在页面加载时, select 下拉菜单将替换为默认文本字段,如果可用,则会自动填充用户以前的交付目标 .

但是,如果页面被重新加载/刷新,则文本字段将被替换为新的和非常期望的 select 下拉菜单 .

我们使用几个WordPress add_filter 函数过滤了该字段,并且上下都改变了 priority (-999到999) .

我们在运输方法 Class 内外运行了 filter

我们甚至已经禁用了浏览器自动格式,因为我们......其他想法已经用光了......

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

1 回答

  • 2

    这应该与 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'           => '',
    );
    

    参考文献:

相关问题