编辑 WooCommerce 结帐页面账单地址字段

Editing WooCommerce checkout page billing address fields

我想编辑 WooCommerce 结账页面的账单地址。我想编辑结账页面的账单状态。我尝试通过首先在我的子主题中执行此操作来对其进行编辑。

然后我尝试编辑 class-wc-checkout.php 文件:

// Billing address
$billing_address = array();
    if ( $this->checkout_fields['billing'] ) {
        foreach ( array_keys( $this->checkout_fields['billing'] ) as $field ) {
            $field_name = str_replace( 'billing_', '', $field );
            $billing_address[ $field_name ] = $this->get_posted_address_data( $field_name );
        }
    }

没有成功。我该怎么做?

谢谢。

Important advice: Never touch WooCommerce plugin core files avoiding:

  • Important errors
  • Losing the changes you have done when plugin is updated

To customize WooCommerce you can:


编辑/创建/删除/重新排序结帐字段,我们可以使用这 2 个过滤器挂钩:

  • 'woocommerce_checkout_fields'

或者在特定情况下您需要使用

  • 'woocommerce_default_address_fields' (适用于以下所有账单和送货默认字段)

这里是账单和运费默认字段列表:

country
first_name
last_name
company
address_1
address_2
city
state
postcode

例如,要使 'billing_state' 字段成为必需字段:

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
    $address_fields['billing']['billing_state']['required'] = true;

    return $address_fields;
}

每个字段都包含一组您可以编辑的属性:

type – type of field (text, textarea, password, select)
label – label for the input field
placeholder – placeholder for the input
class – class for the input
required – true or false, whether or not the field is require
clear – true or false, applies a clear fix to the field/label
label_class – class for the label element
options – for select boxes, array of options (key => value pairs)

结帐字段分为 4 组:

  • 送货字段
  • 账单字段
  • 帐户字段
  • 订单字段(备注、评论)

参考文献: