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

admin2025-01-07  3

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!

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!

Share Improve this question asked Oct 26, 2019 at 13:29 randomdev91randomdev91 11 silver badge1 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

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

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736258482a516.html

最新回复(0)