Advertisement
Barbareshet

Add custom fields to order

Sep 1st, 2020
1,085
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.34 KB | None | 0 0
  1. //* Add select field to the checkout page
  2. add_action('woocommerce_before_order_notes', 'barbareshet_add_select_checkout_field');
  3. function barbareshet_add_select_checkout_field( $checkout ) {
  4.     $current_user = wp_get_current_user();
  5.     if ( !user_can( $current_user, 'administrator')) return;
  6.     woocommerce_form_field( 'order_method', array(
  7.         'type'          => 'radio',
  8.         'class'         => array( 'barbareshet-drop' ),
  9.         'label'         => __( 'Order Method', 'oceanwp-child' ),
  10.         'options'       => array(
  11.             'online'    => __( 'online', 'oceanwp-child' ),
  12.             'phone' => __( 'by phone', 'oceanwp-child' ),
  13.         )
  14.     ),
  15.  
  16.         $checkout->get_value( 'order_method' ));
  17.  
  18. }
  19.  
  20. // save custom field values
  21.  
  22. function barbareshet_save_order_cf( $order_id ){
  23.  
  24.     if( !empty( $_POST['order_method'] ) )
  25.         update_post_meta( $order_id, 'order_method', sanitize_text_field( $_POST['order_method'] ) );
  26.  
  27. }
  28. // save fields to order meta
  29. add_action( 'woocommerce_checkout_update_order_meta', 'barbareshet_save_order_cf' );
  30.  
  31. function barbareshet_checkout_field_display_admin_order_meta($order){
  32.     echo '<p><strong>'.__('Order Method', 'oceanwp-child').':</strong> <br/>' . get_post_meta( $order->get_id(), 'order_method', true ) . '</p>';
  33.  
  34. }
  35. add_action( 'woocommerce_admin_order_data_after_billing_address', 'barbareshet_checkout_field_display_admin_order_meta', 10, 1 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement