获取 woocommerce 3 中自定义结帐字段的值

Get the value of a custom checkout field in woocommerce 3

在 Woocommerce 结账中,我添加了一个自定义结账字段,这是我的代码:

add_action( 'woocommerce_before_order_notes', 'shipping_add_select_checkout_field' );
function shipping_add_select_checkout_field( WC_Checkout $checkout ) {
    $options = array_merge( [ '' => __( 'Nothing to select' ), ], city_zone() );
    woocommerce_form_field( 'billing_country_zone', array(
            'type'     => 'select',
            'class'    => array( 'form-row-wide', 'address-field', 'update_totals_on_change' ),
            'label'    => __( 'City zone' ),
            'required' => true,
            'options'  => $options
    ), WC()->customer->billing_country_zone );
}

现在我完全迷路了,因为我需要知道 WC()->customer->billing_country_zone 的用途以及如何检查它的价值……

非常感谢任何帮助。

WC()->customer->billing_country_zone:

  • 从 Woocommerce 3 开始,大多数 Woocommerce 实例对象都可以访问属性。
  • 并且“billing_country_zone”不是 WC_Customer 实例对象的默认 属性。

由于 关于结帐字段,您应该使用 $checkout 参数,它是 WC_Checkout 对象的实例。然后就是适当的method get_value()用在上面了...

What is that for?
Once the customer has submitted at least one order, the selected value for "billing_country_zone" will be displayed on checkout page.

因此您必须替换以下行:

), WC()->customer->billing_country_zone );

来自这个:

), $checkout->get_value('billing_country_zone') );

If $checkout variable argument is not defined, you will use WC()->checkout like:

), WC()->checkout->get_value('billing_country_zone') );

现在当您要保存此自定义结帐字段值时,您需要保存它:

  1. 作为订单元数据
  2. 并且作为用户元数据

所以这是完整的代码(评论):

// Display custom checkout field
add_action( 'woocommerce_before_order_notes', 'display_custom_checkout_field' );
function display_custom_checkout_field( $checkout ) {
    $options = array_merge( [ '' => __( 'Nothing to select' ), ], city_zone() );
    woocommerce_form_field( 'billing_country_zone', array(
            'type'     => 'select',
            'class'    => array( 'form-row-wide', 'address-field', 'update_totals_on_change' ),
            'label'    => __( 'City zone' ),
            'required' => true,
            'options'  => $options
    ), $checkout->get_value('billing_country_zone') );
}

// custom checkout field validation
add_action( 'woocommerce_checkout_process', 'custom_checkout_field_validation' );
function custom_checkout_field_validation() {
    if ( isset( $_POST['billing_country_zone'] ) && empty( $_POST['billing_country_zone'] ) )
        wc_add_notice( __( 'Please select a <strong>"City zone"</strong>.', 'woocommerce' ), 'error' );
}

// Save custom checkout field value as custom order meta data and user meta data too
add_action( 'woocommerce_checkout_create_order', 'custom_checkout_field_update_order_meta', 20, 2 );
function custom_checkout_field_update_order_meta( $order, $data ) {
    if ( isset( $_POST['billing_country_zone'] ) ) {
        // Save custom checkout field value
        $order->update_meta_data( '_billing_country_zone', esc_attr( $_POST['billing_country_zone'] ) );

        // Save the custom checkout field value as user meta data
        if( $order->get_customer_id() )
            update_user_meta( $order->get_customer_id(), 'billing_country_zone', esc_attr( $_POST['billing_country_zone'] ) );
    }
}

代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。