根据国家/地区对特定Woocommerce结帐字段的排序优先级 [英] Sorting priority for specific Woocommerce checkout fields depending on country

查看:79
本文介绍了根据国家/地区对特定Woocommerce结帐字段的排序优先级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Woocommerce中,我需要在结帐页面上设置三个字段的默认排序优先级(排序):

In Woocommerce, I need to set the default ordering priority (sorting) of three fields on the checkout page:

第一个更改取决于 'IT' 国家/地区代码:

The first change depends on the choice of the 'IT' country code:


  • 邮政编码-' priority'=> 91,

另外两个没有条件,我只需要设置默认优先级即可:

The other two are free of conditions, I just need to set the default priority:


  • billing_email-'priority'=> 30,

  • billing_phone-'priority'=> 40,

通过直接编辑文件, class-wc-countries.php 核心文件,如下所示,我可以找到所需的文件。但是显然在每次更新插件时,我都会丢失更改。

By editing the file directly, class-wc-countries.php core file as shown below, I get what I need. But clearly at each update of the plugin I lose the changes.

现在,我正在寻找一个挂钩来以一种干净的方式完成此操作。

Now I'm looking for a hook to get this done in a clean way.

类中-wc-countries.php (第935行)来自:

In class-wc-countries.php (around line 935) from:

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

我将优先级值从 65 91

I change the 'priority' value from 65 to 91:

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

和以下环绕线1203:

And arround line 1203 in:

  // 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
       );
    }

感谢您的帮助。

推荐答案

使用可用的过滤器挂钩,您可以在不覆盖核心文件的情况下进行更改……

Using available filter hook you can make that change without overriding core files…

第一个更改是与此相关的挂钩(用于 IT国家/地区代码)

The first change is maid with this hook (for "IT" country code):

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;
}

第二次更改,您还需要更改班级。这是您在其他问题的答案中已经具有的相同代码,但是没有删除此行:

For the second change you need also to change the class. It's the same code that you already have in the answer to your other question, but without this line to be removed:

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())返回$ fields; 来自 我其他答案中的代码 它也将在结帐页面上运行(如前所述)…

So if you remove if( ! is_account_page() ) return $fields; from the code in my other answer, it will work also on checkout page (as as already indicated)…

因此您可以使用(对于帐户页面和结帐页面):

So you can use (for both account and checkout pages):

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文件)。经过测试,可以正常工作。

Code goes in function.php file of your active child theme (or active theme). Tested and works.

这篇关于根据国家/地区对特定Woocommerce结帐字段的排序优先级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆