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

Row*_*ouh 1 php wordpress field checkout woocommerce

在 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 );
}
Run Code Online (Sandbox Code Playgroud)

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

任何帮助都非常感谢。

Loi*_*tec 6

对于WC()->customer->billing_country_zone

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

因为它是关于 checkout fields,所以你应该使用$checkout参数,它是WC_Checkout对象的实例。然后有适当的方法get_value()用于它......

那是做什么用的?
一旦客户提交了至少一个订单,“ billing_country_zone”的选定值将显示在结帐页面上。

因此,您将不得不替换该行:

), WC()->customer->billing_country_zone );
Run Code Online (Sandbox Code Playgroud)

通过这个:

), $checkout->get_value('billing_country_zone') );
Run Code Online (Sandbox Code Playgroud)

如果$checkout未定义变量参数,您将使用WC()->checkout类似:

), WC()->checkout->get_value('billing_country_zone') );
Run Code Online (Sandbox Code Playgroud)

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

  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'] ) );
    }
}
Run Code Online (Sandbox Code Playgroud)

代码位于活动子主题(或活动主题)的 function.php 文件中。测试和工作。

  • @RowanMamdouh 欢迎...正如所解释的,只有当客户之前已经购买过某些东西时,该值才存在。因此,您可以使用 `get_user_meta( get_current_user_id(), 'billing_country_zone', true );` 从用户数据中获取此值 **(测试是否存在)** ... *或者在后端从现有的 `$order` 对象使用 ` $order-&gt;get_meta( '_billing_country_zone' );`* (2认同)