WooCommerce-在现有结帐字段上覆盖帐单状态和邮政编码 [英] WooCommerce - Overriding billing state and post code on existing checkout fields

查看:118
本文介绍了WooCommerce-在现有结帐字段上覆盖帐单状态和邮政编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到ovveride帐单状态和邮政编码的方法.

I can't find the way to ovveride billing state and post code.

如何编辑现有帐单字段的其他部分,例如帐单状态和邮政编码?

How can I edit the other parts of existing billing fields like billing state and post code?

这是我子主题的 functions.php 文件中的内容(我已经包含影响计费部分的代码):

This is what I have in the functions.php file in my child theme (I have included the code affecting the billing part):

    <?php
function my_custom_checkout_field( $checkout ) {
  global $wpdb;
  $check_zone  = $wpdb->get_results("select area_name from brick_area where id='".$_SESSION['area']."'",ARRAY_A);

                if(!empty($check_zone)){
                    $check_zoneid = $check_zone['0'];
                }


    woocommerce_form_field( 'my_field_name', array(
        'type'          => 'text',
        'class'         => array('my-field-class form-row-wide'),
        'label'         => __('Delivery Area'),
        'placeholder'   => __('Area'),
        'readonly'      =>'readonly',
        'default'       => $check_zoneid['area_name']
        ), $checkout->get_value( 'my_field_name' ));


    woocommerce_form_field( 'my_expected_date', array(
        'type'          => 'text',
        'class'         => array('my-field-class form-row-wide'),
        'required'      => true,
        'label'         => __('Expected Delivery Date'),
        'placeholder'   => __('Enter expected delivery date.'),
        ), $checkout->get_value( 'my_expected_date' ));

   /*woocommerce_form_field( 'my_expected_time', array(
        'type'          => 'text',
        'class'         => array('my-field-class form-row-wide'),
        'required'      => true,
        'label'         => __('Expected Delivery Time'),
        'placeholder'   => __('Enter expected delivery time.'),
        ), $checkout->get_value( 'my_expected_time' ));*/

    woocommerce_form_field( 'site_contact_name', array(
        'type'          => 'text',
        'class'         => array('my-field-class form-row-wide'),
        'required'      => true,
        'label'         => __('Site Contact Person Name'),
        'placeholder'   => __('Enter site contact person name.'),
        ), $checkout->get_value( 'site_contact_name' ));    

    woocommerce_form_field( 'site_contact_phone', array(
        'type'          => 'tel',
        'class'         => array('my-field-class form-row-wide'),
        'required'      => true,
        'label'         => __('Site Contact Phone Number'),
        'placeholder'   => __('Enter site contact phone number.'),
        ), $checkout->get_value( 'site_contact_phone' ));       

}
        $fields['billing']['billing_city']['default'] = $_SESSION['cn'];
        add_filter( 'woocommerce_default_address_fields' , 'custom_override_default_address_fields' );
        function custom_override_default_address_fields( $address_fields ) {

            // we are changing here billing_state field to required
            $fields['billing']['billing_state']['required'] = true;

            return $address_fields;
        }
        /*$fields['billing']['my_field_name']['default'] = $check_zoneid['area_name'];
        $fields['billing']['my_field_name']['label'] = 'Area';*/

        return $fields;
        }
 ?>      

谢谢

推荐答案

这是计费状态和计费邮政编码覆盖的完整方法,使计费选择器保持选项不变.

This is the complete way for billing state and billing post code override, keeping the billing selector with options.

以下是功能齐全且经过测试的代码:

Here is the code the fully functional and tested code:

  1. 未设置帐单状态和邮政编码结帐字段

add_filter( 'woocommerce_checkout_fields' , 'partial_unsetting_checkout_fields' );
function partial_unsetting_checkout_fields( $fields ) {
     unset($fields['billing']['billing_state']);
     unset($fields['billing']['billing_postcode']);

     return $fields;
}

  1. 重新插入自定义计费状态和邮政编码结帐字段

add_filter( 'woocommerce_default_address_fields' , 'art_override_default_address_fields' );
function art_override_default_address_fields( $address_fields ) {

  // @ for state
    $address_fields['billing_state']['type'] = 'select';
    $address_fields['billing_state']['class'] = array('form-row-wide');
    $address_fields['billing_state']['required'] = true;
    $address_fields['billing_state']['label'] = __('State', 'my_theme_slug');
    $address_fields['billing_state']['placeholder'] = __('Enter state', 'my_theme_slug');
    $address_fields['billing_state']['default'] ='Choice 1';
    $address_fields['billing_state']['options'] = array(
        'option_1' => 'Choice 1',
        'option_2' => 'Choice 2',
        'option_3' => 'Choice 3'
    );

    // @ for postcode
    $address_fields['billing_postcode']['type'] = 'text';
    $address_fields['billing_postcode']['class'] = array('form-row-wide');
    $address_fields['billing_postcode']['required'] = true;
    $address_fields['billing_postcode']['label'] = __('Postcode', 'my_theme_slug');
    $address_fields['billing_postcode']['placeholder'] = __('Enter your postcode', 'my_theme_slug');


    return $address_fields;
}

自然地,这会继续运行您的活动子主题或主题的function.php文件

官方参考: WooThemes-自定义结帐使用操作和过滤器的字段

关于'class'属性的说明

Note concerning the 'class' property

有2种处理方式:

  • 该字段仅占一行(宽度100%),您可以使用: 'form-row-wide'
  • 在同一行上并排有2个字段,您可以使用:
    • 'form-row-first' (第一个字段)
    • 'form-row-last' 用于第二个字段
    • The field is alone in one line (width 100%), you use: 'form-row-wide'
    • There is 2 fields side by side on the same line, you use:
      • 'form-row-first' for the first field
      • 'form-row-last' for the second field

      这篇关于WooCommerce-在现有结帐字段上覆盖帐单状态和邮政编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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