Show custom field on order in woocommerce

后端 未结 3 1158
醉话见心
醉话见心 2020-12-23 18:21

Im working on a webshop and follwoing this tutorial http://wcdocs.woothemes.com/snippets/tutorial-customising-checkout-fields-using-hooks-and-filters/ to add some customes f

3条回答
  •  庸人自扰
    2020-12-23 18:24

    After defining your custom field (that you did in your code mentioned above), add the code mentioned below to:

    1. Process your field

    2. Save it in the database as Order meta data

    3. Display it in the 'Order details' in the Woocommerce->Orders section

    Process your field:

    add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
    
    function my_custom_checkout_field_process() {
    
    if (!$_POST['billing']['billing_gls_name']) {
        wc_add_notice(__('Please tell us the Location Type that we are billing to.'), 'error');
    }
    
    }
    

    Save Field in the DB as Order Meta Data:

    add_action('woocommerce_checkout_update_order_meta','my_custom_checkout_field_update_order_meta');
    
    function my_custom_checkout_field_update_order_meta($order_id) {
    
      if (!empty($_POST['billing']['billing_gls_name'])) {
         update_post_meta($order_id, 'Billing Gls Name', esc_attr($_POST['billing']['billing_gls_name_type']));
         }
     }
    

    And finally display it in the Order details screen:

      add_action('woocommerce_admin_order_data_after_billing_address', 'my_custom_billing_fields_display_admin_order_meta', 10, 1);
    
      function my_custom_billing_fields_display_admin_order_meta($order) {
    echo '

    ' . __('Billing Gls Name') . ':
    ' . get_post_meta($order->id, '_billing_gls_name', true) . '

    '; }

提交回复
热议问题