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

by randomdev91   Last Updated August 01, 2020 11:08 AM

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!

Tags : woocommerce


Answers 1


I managed to achieve this using ACF and editing the "product-attributes.php" file in the woocommerce plugin.

randomdev91
randomdev91
October 26, 2019 23:46 PM

Related Questions


How can add and preview product Frontend

Updated March 26, 2015 12:02 PM


How to add "Reply-to" in Woocommerce

Updated April 04, 2015 15:03 PM


WooCommerce epdq gateway Change order status

Updated April 06, 2015 19:03 PM