Site icon Hip-Hop Website Design and Development

How to add custom text field inputs for attributes in backend?

I wanted to have custom text input fields with the attribute name already defined for the products. Since this is not natively possible in Woocommerce I have added a custom text input field for one of my attributes “Forgaffel”. This information is to be shown under the “Additional information” tab on the product page a long with the other product attributes.

I have used the following code for this:

* Add custom text field
**/
function my_woo_custom_price_field() {

  $field = array(
    'id' => 'christmas_price',
    'label' => __( 'Forgaffel', 'textdomain' ),
    'data_type' => 'text' //Let WooCommerce formats our field as price field
  );

  woocommerce_wp_text_input( $field );
}
/**
* Save custom text field
**/
add_action( 'woocommerce_process_product_meta', 'save_custom_field' );
function save_custom_field( $post_id ) {

  $custom_field_value = isset( $_POST['my_custom_input'] ) ? $_POST['my_custom_input'] : '';

  $product = wc_get_product( $post_id );
  $product->update_meta_data( 'my_custom_input', $custom_field_value );
  $product->save();
}

However, how do I retrieve this information and display it under “Additional information” tab? I am using the WP Ocean theme.

And is there an easier way to add custom text input fields (with the attribute name predefined) for the information under “Additional information” tab?

Thanks in advance!