'Add custom field to product inventory tab and display value only on WooCommerce admin order edit page

I have this code added to functions.php and I would like the contents of my custom field to show on the orders page of WooCommerce backend.

Specifically each product that has this fields info filled in should show the special fields info to the admin user who is fulfilling the order. (each product needs its own in the area where the SKU and other data is listed)

This custom field appears on the "inventory" tab of each product in WooCommerce:

// Add Custom Field to woocommerce inventory tab for variable product

add_action( 'woocommerce_product_options_inventory_product_data', 'wc_custom_add_custom_fields' );

function wc_custom_add_custom_fields() {
  // Print a custom text field
  woocommerce_wp_text_input( array(
  'id' => '_custom_text_field',
  'label' => 'Custom Text Field',
  'description' => 'This is a custom field, you can write here anything you want.',
  'desc_tip' => 'true',
  'placeholder' => 'Custom text'
  ));
}
add_action( 'woocommerce_process_product_meta', 'wc_custom_save_custom_fields' );
function wc_custom_save_custom_fields( $post_id ) {
  if ( ! empty( $_POST['_custom_text_field'] ) ) {
  update_post_meta( $post_id, '_custom_text_field', esc_attr( $_POST['_custom_text_field'] ) );
  }
}

I don't want the user to ever see the info put in this field, only the admin users who will be completing the orders in WooCommerce orders backend section.

I have found the hook to get info into the order details section woocommerce_admin_order_data_after_order_details but I'm looking for the hook into the individual products. Any advice?



Solution 1:[1]

To display the meta data in WooCommerce admin order edit pages only, you can use the woocommerce_before_order_itemmeta hook

To save fields you can use the woocommerce_admin_process_product_object hook, opposite the outdated woocommerce_process_product_meta hook

So you get:

// Add custom field
function action_woocommerce_product_options_inventory_product_data() {
    woocommerce_wp_text_input( array(
        'id'            => '_custom_text_field',
        'label'         => __( 'Custom Text Field', 'woocommerce' ),
        'description'   => __( 'This is a custom field, you can write here anything you want.', 'woocommerce' ),
        'desc_tip'      => 'true',
        'placeholder'   => __( 'Custom text', 'woocommerce' )
    ) );
}
add_action( 'woocommerce_product_options_inventory_product_data', 'action_woocommerce_product_options_inventory_product_data' );

// Save custom field
function action_woocommerce_admin_process_product_object( $product ) {
    // Isset
    if ( isset( $_POST['_custom_text_field'] ) ) {        
        // Update
        $product->update_meta_data( '_custom_text_field', sanitize_text_field( $_POST['_custom_text_field'] ) );
    }
}
add_action( 'woocommerce_admin_process_product_object', 'action_woocommerce_admin_process_product_object', 10, 1 );

// Add before item meta
function action_woocommerce_before_order_itemmeta( $item_id, $item, $product ) { 
    // Only on backend order edit pages
    if ( ! is_admin() ) return; 

    // Targeting line items type only
    if ( $item->get_type() !== 'line_item' ) return;

    // Finds whether a variable is null
    $product = is_null( $product ) ? $item->get_product() : $product;

    // Get meta
    $value = $product->get_meta( '_custom_text_field' );

    // NOT empty
    if ( ! empty ( $value ) ) {
        echo '<p>' . $value . '</p>';
    }
}
add_action( 'woocommerce_before_order_itemmeta', 'action_woocommerce_before_order_itemmeta', 10, 3 );

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1