获取woocommerce 3中的自定义结帐字段的值 [英] Get the value of a custom checkout field in woocommerce 3

查看:41
本文介绍了获取woocommerce 3中的自定义结帐字段的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In Woocommerce checkout, I am adding a custom checkout field and here is my code:

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 的含义以及如何检查其值……

Now I am totally lost as I need to know what is WC()->customer->billing_country_zone for and how can I check it's value…

我们非常感谢您的帮助.

Any help is really appreciated.

推荐答案

对于 WC()-> customer-> billing_country_zone :

  • 自Woocommerce 3以来,首先,可以在大多数所有Woocommerce实例对象上访问属性.
  • 和" billing_country_zone "不是 WC_Customer 实例对象的默认属性.
  • First since Woocommerce 3, properties can be accessed on most all Woocommerce instances objects.
  • And "billing_country_zone" is no a default property of WC_Customer instance object.

因为它关于结帐字段 ,因此您应该使用 $ checkout 参数,该参数是 WC_Checkout 对象的实例.然后是适当的方法 get_value() 可以在上面使用...

As it's about checkout fields, instead you should use $checkout argument which is the instance of the WC_Checkout Object. Then there is the appropriated method get_value() to be used on it...

这是做什么用的?
客户提交了至少一份订单后,便会为" billing_country_zone "选择的值显示在屏幕上.将显示在结帐页面上.

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.

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

So you will have to replace the line:

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

通过这个:

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

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

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


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


Now when you will save this custom checkout field value, you will need to save it:

  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文件中.经过测试并可以正常工作.

Code goes in function.php file of your active child theme (or active theme). tested and works.

这篇关于获取woocommerce 3中的自定义结帐字段的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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