• Resolved randomdev91

    (@randomdev91)


    Hi. 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?
    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!

Viewing 4 replies - 1 through 4 (of 4 total)
  • You can try this approach

    add_action( 'woocommerce_product_additional_information', 'paulc_custom_field_data', 40 );
    function paulc_custom_field_data( $product ) {
    	//* WRITE YOUR PHP CODE HERE
    }
    Thread Starter randomdev91

    (@randomdev91)

    Hi wprock.

    Thanks for the reply.

    What exactly would I post inside the //*WRITE YOUR PHP CODE HERE ? 🙂

    Do you think I should use “get_post_meta” or?

    Hi @randomdev91

    As I can see, @wprock has shared the hook which stores details about the product metadata. What you need to do, is to call the metadata related to the particular post and add it here.

    I’m not an expert here, but I would think that get_post_meta might work here.

    I hope that guides you in the right direction.

    Hey @randomdev91

    Just wanted to check-in on you: Were you able to get to the bottom of this?

    Since we haven’t heard from you in a while, I’m going to mark this topic as Resolved. Let us know if you keep running into troubles. You can either reply here to follow-up with the same problem, or create a new thread to report your troubles to us.

    All the best!

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Custom text field inputs for attributes’ is closed to new replies.