【问题标题】:Woocommerce overwriting Shipping City Select FieldWoocommerce 覆盖航运城市选择字段
【发布时间】:2016-10-09 17:51:45
【问题描述】:

我们正在尝试使用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

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

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

但是,问题是由于我们最初在 Shipping Method Class 中包含 $field 过滤器引起的,该类与 shipping_class_init 挂钩

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

【问题讨论】:

    标签: php wordpress woocommerce checkout shipping


    【解决方案1】:

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

    参考文献:

    【讨论】:

    • 感谢@LoicTheAztec - 初步测试看起来很有希望...... ajax 非常适合,但我们将运行一些变体,看看我们是否可以解决这个问题。
    【解决方案2】:

    将此代码放入您的子主题 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' );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-19
      • 1970-01-01
      • 1970-01-01
      • 2018-01-30
      • 1970-01-01
      • 2017-02-03
      • 1970-01-01
      相关资源
      最近更新 更多