【问题标题】:Sorting priority for specific Woocommerce checkout fields depending on country根据国家/地区对特定 Woocommerce 结帐字段的排序优先级
【发布时间】:2018-09-14 11:20:28
【问题描述】:

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

第一个变化取决于'IT'国家代码的选择:

  • 邮政编码 - '优先级' => 91,

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

  • billing_email - '优先级' => 30,
  • billing_phone - '优先级' => 40,

通过直接编辑文件,class-wc-countries.php核心文件如下图,我得到了我需要的。但很明显,每次更新插件时,我都会丢失更改。

现在我正在寻找一个钩子来以干净的方式完成这项工作。

class-wc-countries.php(第 935 行附近)来自:

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

我将“优先级”值从 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
       );
    }

感谢任何帮助。

【问题讨论】:

    标签: php wordpress woocommerce checkout hook-woocommerce


    【解决方案1】:

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

    第一个改变是用这个钩子(对于“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;
    }
    

    因此,如果您从 the code in my other answer 中删除 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 您也可以将conditional woocommerce functions 用作is_checkout()is_account(),但如果您使用相同的挂钩,请更改挂钩的优先级,即第三个参数add_action()add_filter()(每次都不同,只是为了相同的钩子)
    猜你喜欢
    • 2021-10-27
    • 2016-01-01
    • 2015-04-09
    • 2021-11-22
    • 2018-05-05
    • 2019-08-07
    • 1970-01-01
    • 2020-09-15
    • 1970-01-01
    相关资源
    最近更新 更多