根据国家/地区设置Woocommerce结账字段的排序优先级

3
在Woocommerce中,我需要设置结账页面上三个字段的默认排序优先级(sorting):
第一个更改取决于选择了“IT”国家代码:
邮政编码 - 'priority' => 91,
其他两个没有条件,我只需要设置默认优先级:
billing_email - 'priority' => 30,
billing_phone - 'priority' => 40,
通过直接编辑文件,如下所示编辑core文件class-wc-countries.php,我得到了我需要的结果。但是显然,在每次插件更新时,我都会失去这些更改。
现在我正在寻找一个钩子来以清洁的方式完成此操作。
class-wc-countries.php中(大约在第935行附近):
IT' => array(
    'postcode' => array(
        'priority' => 65, // <============= HERE
    ),
    'state'    => array(
    'required' => true,
    'label'    => __( 'Province', 'woocommerce' ),
    ),
),

我将“priority”值从65更改为91

IT' => array(
    'postcode' => array(
        'priority' => 91, // <============= HERE
    ),
    'state'    => array(
    'required' => true,
    'label'    => __( 'Province', 'woocommerce' ),
    ),
),

在大约1203行附近:

  // Add email and phone fields.
  if ( 'billing_' === $type ) {
      $address_fields['billing_phone'] = array(
          'label'        => __( 'Phone', 'woocommerce' ),
          'required'     => true,
          'type'         => 'tel',
          'class'        => array( 'form-row-wide' ),
          'validate'     => array( 'phone' ),
          'autocomplete' => 'tel',
          'priority'     => 100, // <============= HERE
      );
      $address_fields['billing_email'] = array(
          'label'        => __( 'Email address', 'woocommerce' ),
          'required'     => true,
          'type'         => 'email',
          'class'        => array( 'form-row-wide' ),
          'validate'     => array( 'email' ),
          'autocomplete'=>'no'===get_option
              ('woocommerce_registration_generate_username' )
              ? 'email' : 'email username',
          'priority'     => 110, // <============= HERE
       );
    }

我将“优先级”值更改为:
  // Add email and phone fields.
  if ( 'billing_' === $type ) {
      $address_fields['billing_phone'] = array(
          'label'        => __( 'Phone', 'woocommerce' ),
          'required'     => true,
          'type'         => 'tel',
          'class'        => array( 'form-row-wide' ),
          'validate'     => array( 'phone' ),
          'autocomplete' => 'tel',
          'priority'     => 40, // <============= HERE
      );
      $address_fields['billing_email'] = array(
          'label'        => __( 'Email address', 'woocommerce' ),
          'required'     => true,
          'type'         => 'email',
          'class'        => array( 'form-row-wide' ),
          'validate'     => array( 'email' ),
          'autocomplete'=>'no'===get_option
              ('woocommerce_registration_generate_username' )
              ? 'email' : 'email username',
          'priority'     => 30, // <============= HERE
       );
    }

非常感谢您的帮助。

1个回答

3
使用可用的过滤器钩子,您可以在不覆盖核心文件的情况下进行更改...
第一个更改是使用此钩子进行的(适用于“IT”国家代码):
add_filter('woocommerce_get_country_locale', 'custom__country_locale', 30, 1 );
function wps_select_order_meta_keys( $locale ) {
    $locale['IT']['postcode']['priority'] = 91;
    return $locale;
}

对于第二次修改,您还需要更改类别。这是与您其他问题答案中已有的相同代码,但不需要删除此行:

if( ! is_account_page() ) return $fields;

代码如下:
add_filter(  'woocommerce_billing_fields', 'custom_billing_fields', 20, 1 );
function custom_billing_fields( $fields ) {

    ## ---- 1.  Sort billing email and phone fields ---- ##

    $fields['billing_email']['priority'] = 30;
    $fields['billing_email']['class'] = array('form-row-first');
    $fields['billing_phone']['priority'] = 40;
    $fields['billing_phone']['class'] = array('form-row-last');

    return $fields;
}

如果您从我的其他答案中删除if( ! is_account_page() ) return $fields;它也将适用于结算页面(如已经指示的那样)……所以,您可以在账户和结算页面都使用以下代码:
add_filter(  'woocommerce_default_address_fields', 'custom_default_address_fields', 20, 1 );
function custom_default_address_fields( $fields ) {

    ## ---- 1.  Remove 'address_2' field ---- ##

    unset($fields['address_2']);

    ## ---- 2.  Sort Address fields ---- ##

    // Set the order (sorting fields) in the array below
    $sorted_fields = array('first_name','last_name','company','address_1','country','postcode','city','state');

    $new_fields = array();
    $priority = 0;

    // Reordering billing and shipping fields
    foreach($sorted_fields as $key_field){
        $priority += 10;

        if( $key_field == 'company' )
            $priority += 20; // keep space for email and phone fields

        $new_fields[$key_field] = $fields[$key_field];
        $new_fields[$key_field]['priority'] = $priority;
    }
    return $new_fields;
}

代码放在您的活动子主题(或活动主题)的function.php文件中。已测试并可用。


完美永远更好。 我已经有函数来重新排列结帐中的字段,现在与这些新添加的内容发生冲突。 所以我需要时间来更好地整合它们。 我会尽快通知您。 一如既往,提前感谢...... :) - Dom
1
@Dom,你也可以使用条件化的 WooCommerce 函数,如 is_checkout()is_account(),但如果你使用相同的钩子,请改变钩子的优先级,这是 add_action()add_filter() 的第三个参数(每次为相同的钩子使用不同的参数)。 - LoicTheAztec

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