Show custom field on order in woocommerce

后端 未结 3 1139
醉话见心
醉话见心 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 '<p><strong>' . __('Billing Gls Name') . ':</strong><br> ' . get_post_meta($order->id, '_billing_gls_name', true) . '</p>';
    }
    
    0 讨论(0)
  • 2020-12-23 18:43

    Adding the action woocommerce_admin_order_data_after_billing_address you can insert some data after billing info. Custom fields are under $order->order_custom_fields array.

    function display_rfc_in_order_metabox($order){
        echo "<p><strong>RFC:</strong> {$order->order_custom_fields['_billing_rfc'][0]}</p>";
    }
    
    add_action( 'woocommerce_admin_order_data_after_billing_address', 'rxm_details_to_order', 10, 1 );
    
    0 讨论(0)
  • 2020-12-23 18:44

    The first answer (Cesar) was CLOSE to being correct. In case anyone ever comes across this old post trying to find out the same thing, below is the code needed to insert into your functions.php file after the code given by the original poster, tailored to his/her variables as provided. Note that they use the field name "billing_gls_name" and that this is referenced in our new function as "_billing_gls_name". The extra "_" at the beginning is necessary. This works on Wordpress 3.5.1 running WooCommerce 2.0.3.

    function your_custom_field_function_name($order){
        echo "<p><strong>Name of pickup person:</strong> " . $order->order_custom_fields['_billing_gls_name'][0] . "</p>";
    }
    
    add_action( 'woocommerce_admin_order_data_after_billing_address', 'your_custom_field_function_name', 10, 1 );
    
    0 讨论(0)
提交回复
热议问题