修改WooCommerce结算字段

WooCommerce结算页面,会要求用户填写一些默认字段,如下图——

如果你的业务特点,不需要用户填写这么多的字段,那么应该如何修改呢?

下面将详细向你介绍修改方法!

首先,先来弄清楚,结算页面woocommerce到底准备了哪些默认的字段,其数据结构(格式)是怎么样的?

<?php
  array(
            // 名
            'first_name' => array(   
                'label'        => __( 'First name', 'woocommerce' ),
                'required'     => true,
                'class'        => array( 'form-row-first' ),
                'autocomplete' => 'given-name',
                'autofocus'    => true,
                'priority'     => 10,
            ),

            // 姓
            'last_name'  => array(
                'label'        => __( 'Last name', 'woocommerce' ),
                'required'     => true,
                'class'        => array( 'form-row-last' ),
                'autocomplete' => 'family-name',
                'priority'     => 20,
            ),
            // 公司
            'company'    => array(
                'label'        => __( 'Company name', 'woocommerce' ),
                'class'        => array( 'form-row-wide' ),
                'autocomplete' => 'organization',
                'priority'     => 30,
            ),
            // 国家
            'country'    => array(
                'type'         => 'country',
                'label'        => __( 'Country', 'woocommerce' ),
                'required'     => true,
                'class'        => array( 'form-row-wide', 'address-field', 'update_totals_on_change' ),
                'autocomplete' => 'country',
                'priority'     => 40,
            ),
            // 地址1,如街道
            'address_1'  => array(
                'label'        => __( 'Street address', 'woocommerce' ),
                /* translators: use local order of street name and house number. */
                'placeholder'  => esc_attr__( 'House number and street name', 'woocommerce' ),
                'required'     => true,
                'class'        => array( 'form-row-wide', 'address-field' ),
                'autocomplete' => 'address-line1',
                'priority'     => 50,
            ),
            // 地址2,如小区
            'address_2'  => array(
                'placeholder'  => esc_attr__( 'Apartment, suite, unit etc. (optional)', 'woocommerce' ),
                'class'        => array( 'form-row-wide', 'address-field' ),
                'required'     => false,
                'autocomplete' => 'address-line2',
                'priority'     => 60,
            ),
            // 城市
            'city'       => array(
                'label'        => __( 'Town / City', 'woocommerce' ),
                'required'     => true,
                'class'        => array( 'form-row-wide', 'address-field' ),
                'autocomplete' => 'address-level2',
                'priority'     => 70,
            ),
            // 省或州
            'state'      => array(
                'type'         => 'state',
                'label'        => __( 'State / County', 'woocommerce' ),
                'required'     => true,
                'class'        => array( 'form-row-wide', 'address-field' ),
                'validate'     => array( 'state' ),
                'autocomplete' => 'address-level1',
                'priority'     => 80,
            ),
            // 邮编
            'postcode'   => array(
                'label'        => __( 'Postcode / ZIP', 'woocommerce' ),
                'required'     => true,
                'class'        => array( 'form-row-wide', 'address-field' ),
                'validate'     => array( 'postcode' ),
                'autocomplete' => 'postal-code',
                'priority'     => 90,
            ),
 );

然后,如果你不想要那么多的字段,你可以通过woocommerce提供的钩子,去调整字段,例如——

[auth]

<?php
/**
 * 调整结算默认字段
 * 包括删除默认字段,修改字段,以及添加字段
 */
add_filter( 'woocommerce_default_address_fields', 'wordpresskt_mod_default_checout_fields' );
function wordpresskt_mod_default_checout_fields($fields) {
    
    // 删除默认字段
    unset(
            $fields['last_name'],      // 删除默认字段中的last_name成员
            $fields['company'], 
            $fields['country'], 
            $fields['address_1'], 
            $fields['address_2'], 
            $fields['city'], 
            $fields['state'],
            $fields['postcode']
        );

    
    // 修改字段
    $fields['first_name'] = array(
        'label'        => '姓名',                   // 字段标记,会在字段前面展示
        'required'     => true,
        'class'        => array( 'form-row-wide' ), // 宽字段,会占满整行
        'autocomplete' => 'given-name',
        'autofocus'    => true,
        'priority'     => 10,
    );

    // 添加字段
    $fields['qq'] = array(
        'label'        => 'QQ',
        'required'     => true,     // 一个必填字段
        'class'        => array( 'form-row-wide' ),
        'autocomplete' => 'given-qq',
        'autofocus'    => true,
        'priority'     => 5,
    );
    return $fields;  // 修改字段后一定要返回
}

[/auth]

上面这段代码,可以放在插件文件中,也可以放到你当前主题的functions.php文件中。

 

看下修改后的效果——

 

凌风

该用户很懒,还没有介绍自己。