doulifang5554 2018-08-08 23:30
浏览 107
已采纳

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

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 );
}

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.

  • 写回答

1条回答 默认 最新

  • dourang20110122 2018-08-09 01:24
    关注

    For theWC()->customer->billing_country_zone:

    • 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.

    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...

    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 );
    

    by this one:

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

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

    1. As order meta data
    2. And also as User meta data

    So Here is the complete code (commented):

    // 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'] ) );
        }
    }
    

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵