Mastering WooCommerce Products Custom Fields

mastering-woocommerce-product-fields-featured

[edit] If you want to add custom fields to product variations, click here!

WooCommerce is without any doubt the more powerful e-commerce plugin for WordPress. And what i love with WooCommerce is that there’s an API for nearly everything. Today, i decided to show you how you can customize WooCommerce dynamically, and in particular how to add custom fields to any WooCommerce products. Here is the final result of what you’ll learn to do in this post:

mastering-woocommerce-product-fields

As you can on the screenshot above we will see how to add custom fields to the product edition page. To do that we will be working on the functions.php file of your theme’s folder only.

To begin we will add custom fields to the product general tab, we will see later how to add custom fields to the other tabs, and how to create your own tabs.

The right hooks

The first step is to hook an action to woocommerce_product_options_general_product_data. The function linked to this hook will be responsible of the new fields display. A second hook will be taken into account to save fields values: woocommerce_process_product_meta. Basically these two actions will be done using that code:

Adding New Fields

The snippet above link two custom functions to the right hooks. Now we need to create those functions. Let’s start bye the first one, woo_add_custom_general_fields(), that will create the fields. Here is how the function will look like

To create our fields we will mainly use WooCommerce builtin function like (All these functions are located in WooCommerce/Admin/WritePanels/writepanels-init.php.):

Text Field Type

To create a text field type, you will need to use that code:

Please note the use of desc_tip to display those little nice bubble to the right of the field instead of displaying the default field description. This attribute works for every field type.

Number Field Type

To create a number field type, you will have to use that code:

The main difference here is the type attribute set to number. You can also define custom attributes like step, and min, or even max. In the code above, step is the default one (1), and min is set to zero. Basically that means that we expect a positive value here (at least greater than zero).

Textarea Field Type

To create a textarea, here is the code to use:

Nothing really complex here…

Dropdown Select Field Type

To create a dropdown select, use the following code:

The options attributes defines available options within an array.

Checkbox Field Type

To create a checkbox, use this code below:

Hidden Field Type

You can also create hidden fields with the following code:

Products Select Field Type

There’s a really nice way to create a customized dropdown select for WooCommerce products with that code:

Custom Field Type

You can also create custom fields. In the example below, i created a “double field”. You can customize that code and use as many fields as you want that will be merged into one single array:

Custom fields can be pretty much everything, just make sure that you use the form-field class to make them pretty!

Saving Fields Values

Now that you created your WooCommerce product fields, you need to create a function to save their values once you edit the update or publish button. As we saw earlier, we will use a function called woo_add_custom_general_fields_save() hooked to woocommerce_process_product_meta. Basically the idea behind this function is pretty simple: we check if the field is empty and if not we create a post meta using update_post_meta(). Please note that we use esc_attr() and esc_html() to secure data just a bit. Here is the code to save each field type values:

Here is the result:

mastering-woocommerce-custom-fields-2

Retrieve Fields Values

Now that we successfully created our fields and saved their values, i guess you’d like to display those values on the frontend. In this case the best method would be to work with WooCommerce custom templates. Basically a custom template allows you to override WooCommerce default files and use your own custom files instead. Here is a quick tutorial that will explain you how to create your custom templates: http://docs.woothemes.com/document/template-structure/

To get those values we just need to use the popular get_post_meta() function. That’s pretty much all you need.

Example:

Create Custom Tabs

Finally here is a quick snippet to create a custom product tab:

I recommend you to style your tab using a bit of CSS (simply add a nice icon and you’re done!).

One More Thing

One more thing: if you want to add your fields to any other tab than the general one you simply need to modify the hook name you linked your woo_add_custom_general_fields() function to. Fo example use that hook woocommerce_product_options_shipping to add your fields to the shipping tab. You can find all the available hooks in  woocommerce/admin/post-types/writepanels/writepanel-product_data.php.

Found this post useful? Keep me awake with a coffee ;-)

155 thoughts on “Mastering WooCommerce Products Custom Fields

  1. Thanks so much for this. I have been looking to put a small text area at the bottom of the edit order page with some data I captured into MySQL. I think I found it!

  2. Actually, I just re-read the article.. Is there a way to do something similar to this on the Order Edit page? I just realized you are showing a demo for a Products page.
    Thanks,
    Keith

  3. Thanks for this great post, I know it’s not related to this post but do you know how we can count product attached to a specific attribute ?

  4. I’m trying this out and you say that we are only working in functions.php but when I put the text area code snippet in my functions.php file it broke. I guess I’m not understanding where to put the code?
    this is what I put in my functions.php file.
    // Textarea
    woocommerce_wp_textarea_input(
    array(
    ‘id’ => ‘_textarea’,
    ‘label’ => __( ‘My Textarea’, ‘woocommerce’ ),
    ‘placeholder’ => ”,
    ‘description’ => __( ‘Enter the custom value here.’, ‘woocommerce’ )
    )
    );

    Do you have a finished working example that we can see?
    Thanks for your help.

    • That is a finished and working sample, you have to add the add_action() otherwise it doesn’t work. And yes place the code in functions.php

      • okay. got it. I had to add that bit of code within…
        function woo_add_custom_general_fields() {

        global $woocommerce, $post;

        echo ”;
        //added that code here
        echo ”;
        }


        follow up question. I want to add these to Tabs instead of General or shipping but I cannot find the right hook like you mentioned. We do have “Woocommerce tab manager” plugin installed and I looked in the “writepanel-product_data-tabs.php”.

        Our ultimate goal is to be able to export.import our product data but we need the data to include the custom tabs we created.
        Thanks!

  5. Hi there,
    I tried adding the code to the functions.php in the theme folder but it alters the products general tab in the admin of WooCommerce.
    I would send you screenshots but it’s not possible here.

    The code I entered is as follow (without the comments /**/):
    // Display Fields
    add_action( ‘woocommerce_product_options_general_product_data’, ‘woo_add_custom_general_fields’ );

    // Save Fields
    add_action( ‘woocommerce_process_product_meta’, ‘woo_add_custom_general_fields_save’ );
    /*
    function woo_add_custom_general_fields() {

    global $woocommerce, $post;

    echo ”;

    // Text Field
    woocommerce_wp_textarea_input(
    array(
    ‘id’ => ‘_textarea’,
    ‘label’ => __( ‘My Textarea’, ‘woocommerce’ ),
    ‘placeholder’ => ”,
    ‘desc_tip’ => ‘true’,
    ‘description’ => __( ‘Enter the custom value here.’, ‘woocommerce’ )
    )
    );
    }
    function woo_add_custom_general_fields_save( $post_id ){

    // Textarea
    $woocommerce_textarea = $_POST['_textarea'];
    if( !empty( $woocommerce_textarea ) )
    update_post_meta( $post_id, ‘_textarea’, esc_html( $woocommerce_textarea ) );

    }

    */

  6. Hey, how to do that for VARIATIONS, not only for General Tab? I need some custom fields there :)

  7. Mahfuzur Rahman

    Hi,

    This is a great Tutorial. Could you please give an example of ‘my-field-slug’ for the get_post_meta() function?

    Thanks,

    Mahfuz

    • the field slug can be whatever you want, simply do not use special characters, spaces and you’re fine!

      • Thanks Remi for your reply. May be my question was not clear. As you have added some custom fields by the above code, what would be the code to retrieve the value of the first text field for example that you have added.

        I understand the slug part is required to locate the field and its syntax but where did you set it for the custom fields that you have added?

        Thanks,

        Mahfuz

        • Mahfuzur Rahman in this example slug is represented by id, for example slug for checkbox is “_checkbox” in the given array ‘id’ => ‘_checkbox’.

  8. Can a Date Picker function be added to the above custom fields?

    And how would these added functions get added to the Customer Emails and Order Edit pages?

  9. […] Mastering WooCommerce Products Custom Fields – Remi Corson […]

  10. Remi,

    Thanks very much for this – I’ve been trying to get this done all day!

    Two questions:

    1) Is it possible to sort a product list using a numerical field like this?

    2) I’d like to display the contents of a custom number field like this, along with some explanatory text (not in the field – which is what makes it complicated), only on certain product categories. Is there a way to use get_post_meta in combination with an if statement (if post is in category x) to do this? Example:

    PRODUCT NUMBER ONE
    Alpha Acid: 5%

    (I want the “5″ to be the field which I can also sort by, but I only want the text “alpha acid … %” to display on products in the “hops” category”

    Hope that makes sense!

    Andrew

  11. Hi,

    I’ve implemented this. Is it possible to get the info to display on the admin emails?

    I’ve tried adding the post meta data code. But nothing is showing.

    I need this to display an internal product code to ease product packing.

    Cheers for the great tutorial!

  12. Hi,

    I managed to implement the new field in backend. I want to add the new text field under SKU of my product page here: [removed]

  13. Hi,
    i use plugin WP User Frontend Pro to add products to Woocommerce from frontend.
    How i can add these fields to Wp User Frontend Pro form?
    Regards, Ari

  14. It took me a few tries, but now it works like a charm! I posted a tailgating blog about this at: http://ingoodcompanywebdesign.net/blog/project/custom-fields-for-woocommerce-tailgating-remi-corson/ , I hope thats okay – if not just let me know.

  15. Hi, great tutorial!

    I have just one question: I created a Textarea Field Type and when I put some text and hit Publish it’s displaying on my front-end. However when I remove the text to clean the front-end and click on Publish again it stays on the Textarea Field and on the front-end.

    Could you help me to solve that?

    Thanks!

    • I would say this is not link to that code, but i’ll give it a try

      • Hi there,
        I am having the same problem.

        In the functions.php I have:

        // Display Fields
        add_action( ‘woocommerce_product_options_general_product_data’, ‘woo_add_custom_general_fields’ );

        // Save Fields
        add_action( ‘woocommerce_process_product_meta’, ‘woo_add_custom_general_fields_save’ );

        function woo_add_custom_general_fields() {

        global $woocommerce, $post;

        echo ”;

        // Custom fields will be created here…

        // Text Field
        woocommerce_wp_text_input(
        array(
        ‘id’ => ‘_text_field_gold’,
        ‘label’ => __( ‘Gold Offer Text’, ‘woocommerce’ ),
        ‘placeholder’ => ‘Offer Text’,
        ‘desc_tip’ => ‘true’,
        ‘description’ => __( ‘If you would like to show a gold offer flash enter the text here.’, ‘woocommerce’ )
        )
        );

        // Text Field
        woocommerce_wp_text_input(
        array(
        ‘id’ => ‘_text_field_red’,
        ‘label’ => __( ‘Red Offer Text’, ‘woocommerce’ ),
        ‘placeholder’ => ‘Offer Text’,
        ‘desc_tip’ => ‘true’,
        ‘description’ => __( ‘If you would like to show a red offer flash enter the text here.’, ‘woocommerce’ )
        )
        );

        // Text Field
        woocommerce_wp_text_input(
        array(
        ‘id’ => ‘_text_field_blue’,
        ‘label’ => __( ‘Blue Offer Text’, ‘woocommerce’ ),
        ‘placeholder’ => ‘Offer Text’,
        ‘desc_tip’ => ‘true’,
        ‘description’ => __( ‘If you would like to show a blue offer flash eg for a 3for2 enter the text here.’, ‘woocommerce’ )
        )
        );

        echo ”;

        }

        function woo_add_custom_general_fields_save( $post_id ){

        // Text Field
        $woocommerce_text_field = $_POST['_text_field_gold'];
        if( !empty( $woocommerce_text_field ) )
        update_post_meta( $post_id, ‘_text_field_gold’, esc_attr( $woocommerce_text_field ) );

        $woocommerce_text_field = $_POST['_text_field_red'];
        if( !empty( $woocommerce_text_field ) )
        update_post_meta( $post_id, ‘_text_field_red’, esc_attr( $woocommerce_text_field ) );

        $woocommerce_text_field = $_POST['_text_field_blue'];
        if( !empty( $woocommerce_text_field ) )
        update_post_meta( $post_id, ‘_text_field_blue’, esc_attr( $woocommerce_text_field ) );

        }

        Many thanks in advance,
        Susan

  16. Hi man. I did everything as described in a couple different ways, but for the life of me I cannot get the custom fields to display on my product description page. This is my functions.php…

    // Display Fields
    add_action( ‘woocommerce_product_options_general_product_data’, ‘woo_add_custom_general_fields’ );

    // Save Fields
    add_action( ‘woocommerce_process_product_meta’, ‘woo_add_custom_general_fields_save’ );

    function woo_add_custom_general_fields() {

    global $woocommerce, $post;

    echo ”;

    // Text Field
    woocommerce_wp_text_input(
    array(
    ‘id’ => ‘_manufacturertext_field’,
    ‘label’ => __( ‘Manufacturer’, ‘woocommerce’ ),
    ‘placeholder’ => ”,
    ‘desc_tip’ => ‘true’,
    ‘description’ => __( ‘manufacturer’, ‘woocommerce’ )
    )
    );
    echo ”;

    }

    function woo_add_custom_general_fields_save( $post_id ){

    // Text Field
    $woocommerce_text_field = $_POST['_manufacturertext_field'];
    if( !empty( $woocommerce_text_field ) )
    update_post_meta( $post_id, ‘_manufacturertext_field’, esc_attr( $woocommerce_text_field ) );
    }

    and then this is my short-description…
    post_excerpt ) return;
    ?>

    post_excerpt ) ?>

      

    I have more fields but its pretty long. I got the fields to appear on the product edit page and can enter and save but no display. Please help. Been trying for two days now.

  17. Man i have. I thought i copied it to last message. It still isn’t showing. Maybe im doing it wrong. Could you please just use the above example and write it out for me? I made the custom template for single description and put it in my themes woocommerce folder. Nothing i do works, I’ve tried everyones suggestion to no avail. Please help!

  18. Hi Remi –

    Thanks so much for this awesome tutorial, much appreciated.

    I’m using this inside a conditional statement in a project where if the meta text-field created by your process above is empty, then neither it or the wrapping copy are displayed. Straight forward and works great.

    Challenge though, if there is a value given to the text field in WC, but then the user wants to clear it, it will not except a null value. If I delete the value in the backend and then update the page, it repopulates from the database. Obviously if I put a space to cheat, then the wrapper text I want to hide with my conditional is echoed with that space.

    How can I get the DB to “forget” the value I’ve entered? Is there something along the lines of the save code above for expunging values or another function I can research?

    Thanks in advance for any guidance! Looking forward to your next tutorial.

    • Are you saying that the placeholder’s value is stored in the database or the default value you defined for the field?

      • The value would be coming from the database. I am not using a default value or even placeholder info.

        It is a text field representing a suggested price value. If I go to a product and never touch the field it’s perfect (no data), or if I always want a value represented its perfect.

        However if I add a value today and then tomorrow I want it gone (data reverting to no data) then tough luck it seems. I clear the field so nothing is there (like it would have been prior to ever typing in it), update the product and the field repopulates with what I just removed.

        Hope that makes sense lol, it’s very early in the morning here.

    • Hey, I’m trying to do the same thing with, that if the text field is empty, is not shown on the description, can you pass me the code you used for that please?

  19. Hello,

    Just wanted to say thank you for this been looking for something like this. Also it’s weird that it’s not apart of woocommerce as it is essential to ecommerce web sites.

    I’m having some trouble to get this working properly I tried using your code and everything you said but for some reason it wasn’t working so then I read on the comments and I saw someone else having trouble and he did his own version of this as well which is http://ingoodcompanywebdesign.net/blog/project/custom-fields-for-woocommerce-tailgating-remi-corson/

    I followed what he said and I got everything working just fine with one glitch. It’s working fine for the products that were originally on the store. If I try to add a new product and I add the details for the custom fields it won’t show up on the Product Detail page. I also have the Woocommerce CSV Importer from woothemes and when I export I see the data and I tried importing new data and It wasn’t showing up on the Product detail page.

    Any help would be appreciated thanks!

  20. This is what I have in short-description.php to display…

      

     

  21. Hmm after recording a video to show you what my problem was I noticed why my code wasn’t working it was because I was putting the (get_post_meta) code in a if condition inside
    woocommerce > short-description.php

    ‘if ( ! $post->post_excerpt ) return; ‘

    I then realized what my problem was and simply put it on top of that if condition!

    I had another question regarding this but in the Product Variation Version
    Seen here: http://www.wpexplorer.com/best-woocommerce-snippets/

    I’m trying to get this information displayed in the front-end I’ve tried this but with no luck.
    echo get_post_meta( $variation->ID, ‘_my_custom_field’, true );

    Any suggestions?

  22. Hi Remi
    first, thanks for this great tutorial!
    I am working on a project based on a template called ‘Bazar’ that already have override woocommerce template.
    I have created a child theme and have put this code in my function.php but the new field is not add to the product admin:
    // Display Fields
    add_action( ‘woocommerce_product_options_general_product_data’, ‘woo_add_custom_general_fields’ );

    // Save Fields
    add_action( ‘woocommerce_process_product_meta’, ‘woo_add_custom_general_fields_save’ );

    function woo_add_custom_general_fields() {

    global $woocommerce, $post;

    echo ”;

    // Custom fields will be created here…

    echo ”;

    }
    // Text Field
    woocommerce_wp_text_input(
    array(
    ‘id’ => ‘_text_field’,
    ‘label’ => __( ‘My Text Field’, ‘woocommerce’ ),
    ‘placeholder’ => ‘http://’,
    ‘desc_tip’ => ‘true’,
    ‘description’ => __( ‘Enter the custom value here.’, ‘woocommerce’ )
    )
    );

    function woo_add_custom_general_fields_save( $post_id ){

    // Text Field
    $woocommerce_text_field = $_POST['_text_field'];
    if( !empty( $woocommerce_text_field ) )
    update_post_meta( $post_id, ‘_text_field’, esc_attr( $woocommerce_text_field ) );
    }
    am I doing something wrong? r maybe it’s a problem with overriding woocomerce twice?
    thanks

  23. Hi
    Please ignore my previous comment
    I mannaged too sort it out.
    I have another question: can the new custom fields I’ve created be sent to the shooping cart? so the customer can see what material for example he has chossen for the product without the need to go back to product page..
    I mean where is this new meta data being saved and for whom?
    Thanks

  24. Hello!

    I’m a starter in WordPress and looking help for custom fields for woocommerce products. For the project, I’m using a Child theme with Responsive Free (by cyberchimps) theme as parent.

    My Problem: To follow your steps, I created a functions.php file in child theme folder with php opening and closing tags and no whitespaces between them. The site is broken and displaying white dead screen… How can I fix the issue?

    Any help is appreciated.
    Thanks.

  25. Hello,

    Although solved the problem I was facing, got another query.

    How can I display all the custom fields created in the single product summary?

    Thanks

  26. Hello Again,

    I’m having some trouble again but this time its if I want to delete the custom field text. It won’t delete. If I update the custom text field it will update fine but If I delete the text it won’t delete.

    Here’s a video of what I’m talking about
    Video: http://maccam.tv/store/wp-content/video/custom-filed.mp4

    Is there something that I’m missing?

    Thanks!

    • Seems like I’ve figured this little bug out.

      You were updating the field when the value was not empty but not when it was empty.

      Here’s the code:

      function woo_add_custom_general_fields_save( $post_id ){
      // Text Field
      $woocommerce_text_field = $_POST['_text_field'];
      if( !empty( $woocommerce_text_field ) ) {
      update_post_meta( $post_id, ‘_text_field’, esc_attr( $woocommerce_text_field ) );
      }
      else {
      update_post_meta( $post_id, ‘_text_field’, esc_attr( $woocommerce_text_field ) );
      }

      }

  27. What if I want to add a custom field for the user to fill out. For example, I want the user to be able to order a customized product: a dog bowl with their pet’s name on it. He needs to type the name he wants on the bowl in a field that submits with his order. What kind of plugin can I use for this???

  28. Thanx buddy.. its was really helpful for me,….

  29. hello!
    This is a very thorough and instructive post.
    I’ve managed to do everything you proposed, … which is already very good :D

    But of course, if i post it’s because i need to go further, and maybe the answer are self explainatory or maybe not. :
    1) In the last part you give the code to add a custom tab, => works ok, but how would you add those extra fields in the right part fot that tab ?

    add_action( ‘woocommerce_product_write_panel_tabs’, ‘woo_add_custom_admin_product_tab’ );
    function woo_add_custom_admin_product_tab() {
    ?>

    <?php
    }

    do i have to "only" add an action like
    add_action( 'woocommerce_product_options_custom_tab_data', 'woo_add_custom_general_fields' );
    (this doesn't work)
    or should i add something in the core plugin in woocommerce/admin/post-types/writepanels/writepanel-product_data.php. ?

    2) You give example for textfields, textareas, select…, which are useful, but can i add image/file fields ?

    Thank you anyway for the quality of your tutorials
    Regards

  30. Great post, but wait – there’s no plugin for this? (hint hint ^^)

  31. Hi Remi, Happy New Year!
    I want to add some text input fields to the cart page and display the user’s inputs in the “order items” area in admin dashboard. Which hooks shall i link to?

    Thank you.

  32. I have a project that needs custom fields for variable products, any chance you will be diving into it soon? or could i hire you to?

    • I will post a blog post soon regarding custom fields for variables products.

      • Hi!

        Any possibility of writing that post on custom fields for variations? I’m just trying to achieve that. Is it so different to this one?

        Also, I haven’t been able to find the ‘woocommerce_product_options_general_product_data’ hook anywhere in Woocommerce’s documentation, except looking through the files on the plugin, under the post type write panels. I’ve been trying to do the same for variations without luck so far. It would be great if you could enlighten us a bit here ;).

        Am also looking at some of you plugins on codecanyon. Pretty interesting stuff!

        Thanks!

  33. Custom fields disappeared after adding product.

    I have a two custom fields, when i add information, the fields are gone for general_product_tab.

  34. Hi Remi,

    Thanks for the awesome tutorial, really helped me out. Just have one quick question with regards to the custom tab snippet you have provided. Have got this all working fine, but can’t seem to get the right function to add fields into the custom tab?

    Any help would be appreiated

    Thanks

    Rich

  35. Hi Remi,

    I’m looking forward to your next blog post about custom fields. I’m trying to finish up my site and this is one thing holding up the launch. Do your instructions above basically accomplish the same thing that the Personalized Product Plugin does? Thanks a million!

  36. Hi.

    Thanks for the great tutorial.
    I have a question on how to display the fields.
    Where do you set ‘my-field-slug’? Is my-field-slug the same as the name of the field?

    Thanks.

  37. Ok, the id is the same as the my-field-slug I found out :-)

    On the page http://www.mineoppskrifter.no/lyse-muffins/ I want to list ingredients in text fields. How do I make so that the text empty text fields are not displayed?

    Thanks again :-)

  38. And one more question please. How do I display ‘label’ in front end before the id name?

  39. Ok, I solved it, displaying like this:
    ID, ‘beskrivelse’, true))==”)) {
    //Not empty
    echo ‘Beskrivelse: ‘,get_post_meta( $post->ID, ‘beskrivelse’ , true);
    }
    ?>

    Now I have another question. When I delete the value in the custom field, the value will not be deleted when I save the product. The only way I can delete the value is to remove the value both in the product custom field and in the wordpress custom field at the same time. How can I avoid this and just delete it in the product custom field ?

    Thanks.

  40. Code broken in previous post, here it is:

    //Check if custom field is empty
    if (!((get_post_meta($post->ID, ‘beskrivelse’, true))==”)) {
    //Not empty
    echo ‘Beskrivelse: ‘,get_post_meta( $post->ID, ‘beskrivelse’ , true);
    }

  41. Hi Remy, can I use the checkbox field to give the product a CSS class?
    I have to replicate the OnSale badge to mark some product, but i have no idea how to do it. I have a little budget to develop this, can you contact me?

  42. hello remi,
    i am new on wordpress please help me i want to add drop down list of quantity on each page of product that quantty choose by customer like
    1-50 product price is 3 $
    51-100 product price is 4$
    101-500 product price is 5$
    501-so on product price is 6$
    how can i do it please help me.
    I will really thank full to you

  43. Hi Remi,

    Thanks for your great tutorial. I was also trying to add some custom fields to the “Cart Page” according to this, but failed. Can you pls advise how? I’ll be very appreciated for that.

  44. Hello Remi,
    First of all a hearty thanks for such a useful topic. A lot things are resolved in an individual page. So i wanted to know that i got stuck in Products Select Field Type.
    I worked exactly the way you instructed but i am getting an output like-
    array
    do i have to use some [index] fields or how it can be sorted.
    i am using- get_post_meta( $post->ID, ‘_product_field_type_ids’, true ); code
    Thanks Again.

  45. Remi, thank you for all the info, been searching for some time.

    Is there a way to call the (sale-price-schedule) to have HH:MM (hours and minutes)
    woocommerce/product/product-data/general/sale-price/schedule

    It reads YY-MM-DD, and I’d like to be able to set HH:MM in addition to the schedule as well.
    YY-MM-DD HH:MM
    Any ideas would be great!
    Thank you again

  46. Hello Remi. I like your work. I have a question for you about “Mastering WooCommerce Products Custom Fields”. Everything works great, but I would do so, select the field showed me the pictures. For example, if I select “Option 1″ is on the side to show me a picture 1.jpg, or if I choose “option 2″ it will show 2.jpg. Is this possible and if you can help me with this? I’ll be very grateful.
    ps. sorry for my english

  47. Awesome post Remi! One quick question though. Let’s say I wanted to build a plugin that allowed the user to link up Woocommerce products. how would I include the product select field type in the plugin meta box? I’ve tried and while the basic field shows up, the magic of making the ajax call etc. doesn’t seem to be working. I’m so close!

  48. hi
    This is Really Awsome Post!
    i need to add user search field for custom field value
    i am trying to use woocomerce ajax chosen for my case but it’s not help!!
    can you give me some hint about my problem?

    i need a search box for custom field that admin can search for users
    but i want to return results by ajax!
    thanks again

  49. Hi. Awesome feature. It made my life easier. I added a custom field called _qty_number_field. I need to show it in the Quick Edit dropdown on products list page in admin panel. Any idea on how to code it? TIA

  50. Hello I’m a new user and i need help.
    I recently imported 2K .csv product records to Woocommerce. The products show fine. I also imported custom fields for each record which when i go into the database i can see in the wp_postmeta table no problem. On the template i want to display those fields and I use the:
    “echo get_post_meta();”
    code but only one of the custom fields displays. Is there something you could suggest? I was wondering if the underscore had to preced the field name.

    The 2 fields that work are:
    “echo get_post_meta( $post->ID, ‘_sku’, true );”
    “echo get_post_meta( $post->ID, ‘title’, true );”

    Everything else won’t display.
    Please can you help?
    Manish

  51. Ok, so I am having a weird problem. I have gotten everything to work for the idea I am trying. I created the drop down selector, for the admin area for a heat level for the products. And that all works fine. I also added an if/else statement to so the the custom field when its populated. But the issue that is happening, is when the product has the custom field shown, the description tab below the product meta info is closed. Here are some examples:

    Showing the custom field with the product description closed:
    http://lavalipsindy.com/product/crazy-good-fruits-fire-ghost-pepper-sauce/

    No custom field info:
    http://lavalipsindy.com/product/blairs-heat-habanero-mango-exotic-hot-sauce/
    (ignore the heat level info on this. Its the sku meta info changed to create this)

  52. Hi Remi

    Thanks for this awesome post. It has helped me a lot :-)

    is it also possible to create a custom text field for a ‘Grouped product’ ? I really need this or an alternative.

    Thanks
    Stoan

  53. Hi Remi,

    this is a great tutorial, but for some reason I’m not doing it right I guess. I did manage to add all to functions.php file, and I can see my custom text field in product settings. I can save new label as well, but I need that label to show adjacent to product price on frontend page.

    I see you stated to use custom template files, to place in following peace of code:
    ID, ‘my-field-slug’, true );

    // You can also use
    echo get_post_meta( get_the_ID(), ‘my-field-slug’, true );
    ?>

    But that’s jus beyond my wordpress knowledge. I just need to know where to place that peace of code in order to see saved values on frontend page?

    Thanks in advance.

  54. Ok, I managed this. Thanks’ a bunch great guide.

  55. Hi Remi,

    I have one more issue. I’m using three checkboxes on my product edit page, and the problem is, when I check those boxes, values are saved, but on refreshed page boxes are not checked? So how do I ensure that one or all boxes stay checked after save?

  56. Nope:

    // Checkbox
    woocommerce_wp_checkbox(
    array(
    ‘id’ => ‘_akcija’,
    ‘wrapper_class’ => ”,
    ‘label’ => __(‘Proizvod na akciji’, ‘woocommerce’ ),
    ‘description’ => __( ‘Check me!’, ‘woocommerce’ )
    )
    );

    // Checkbox
    woocommerce_wp_checkbox(
    array(
    ‘id’ => ‘_novo’,
    ‘wrapper_class’ => ”,
    ‘label’ => __(‘Novi proizvod’, ‘woocommerce’ ),
    ‘description’ => __( ‘Check me!’, ‘woocommerce’ )
    )
    );

    // Checkbox
    woocommerce_wp_checkbox(
    array(
    ‘id’ => ‘_srpskiProizvod’,
    ‘wrapper_class’ => ”,
    ‘label’ => __(‘Proizvedeno u SR’, ‘woocommerce’ ),
    ‘description’ => __( ‘Check me!’, ‘woocommerce’ )
    )
    );

    and function as follows:

    function woo_add_custom_general_fields_save( $post_id ){

    // Checkbox
    $woocommerce_checkbox = isset( $_POST['_akcija'] ) ? ‘‘ : ”;
    update_post_meta( $post_id, ‘_akcija’, $woocommerce_checkbox );

    // Checkbox
    $woocommerce_checkbox = isset( $_POST['_novo'] ) ? ‘‘ : ”;
    update_post_meta( $post_id, ‘_novo’, $woocommerce_checkbox );

    // Checkbox
    $woocommerce_checkbox = isset( $_POST['_srpskiProizvod'] ) ? ‘
    ‘ : ”;
    update_post_meta( $post_id, ‘_srpskiProizvod’, $woocommerce_checkbox );

    }

    • it’s because you use 3 times the same variable name $woocommerce_checkbox, use instead $woocommerce_checkbox_1, $woocommerce_checkbox_2 and $woocommerce_checkbox_3 at the end of your script

  57. No luck when I use code you provided on that link I have same result, no checkbox is selected after save, even thou value is saved? So I can save, I do have as a result what I want for all three checkboxes, but non of three checkboxes aren’t checked after save.

    I’m sending whole code so someone could help. Sorry but I’m not good with php.

    http://d.pr/f/mCas

  58. Hi,
    Thank you so much for great tutorial!
    Could help me, please?
    I’m newbie and have some problems :(
    Could you tell me, please, how to add ‘My Textarea’ field (Bijango is my custom subtitle for one of these products) under product title on main shop page?
    I have it in , but not in ; here’s how it looks in firebug:

    Bijango


    My code in functions.php:

    // Display Fields
    add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );

    // Save Fields
    add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );

    function woo_add_custom_general_fields() {

    global $woocommerce, $post;

    // Text Field
    woocommerce_wp_textarea_input(
    array(
    'id' => '_subtitle',
    'label' => __( 'Subtitle', 'woocommerce' ),
    'placeholder' => '',
    'desc_tip' => 'true',
    'description' => __( 'Enter the custom value here.', 'woocommerce' )
    )
    );
    }
    function woo_add_custom_general_fields_save( $post_id ){

    // Textarea
    $woocommerce_textarea = $_POST['_subtitle'];
    if( !empty( $woocommerce_textarea ) )
    update_post_meta( $post_id, '_subtitle', esc_html( $woocommerce_textarea ) );

    }

    Thank you kindly for any help with that issue!

  59. I modify my simple.php of my woo with this code:
    ID, ‘_product_field_type_ids’, true );

    // You can also use
    echo get_post_meta( get_the_ID(), ‘_product_field_type_ids’, true );
    ?>
    And the result in the front page is only the word “Array”.
    Nothing more… pls tell me what is going wrong with my code?

  60. When i write an URL in the text field in the front end it does not appear like a link. What should i do?

  61. First of all sorry for my english…
    Can I create more sales dates and “activate” them automatically based on the day of purchase?
    For example:
    the regular price is € 500 for purchases made at least 7 days prior to May 10;
    if customer purchases 30 days in advance the sales price is 450
    if customer purchases 60 days in advance the sales price is 400

    Therefore, if the customer purchases the April 31, the price displayed will be € 450 and not € 500 (which is the regular price).
    Is that clear? :)

  62. I seem to can’t find WooCommerce/Admin/WritePanels/writepanels-init.php

  63. Hey,

    Im trying to make two text fields, this is how my functions.php looks like

    /* woocommerce */
    // Display Fields
    add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );

    // Save Fields
    add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );

    function woo_add_custom_general_fields() {

    global $woocommerce, $post;
    echo '';
    // Text Field
    woocommerce_wp_text_input(
    array(
    'id' => 'marka_field',
    'label' => __( 'Marka', 'woocommerce' ),
    'placeholder' => 'Marka',
    'desc_tip' => 'true',
    'description' => __( 'Enter the custom value here.', 'woocommerce' )
    )
    );
    // Text Field
    woocommerce_wp_text_input(
    array(
    'id' => 'dostepnosc_label',
    'label' => __( 'Dostępność', 'woocommerce' ),
    'placeholder' => 'Dostępny',
    'desc_tip' => 'true',
    'description' => __( 'Enter the custom value here.', 'woocommerce' )
    )
    );
    function woo_add_custom_general_fields_save( $post_id ){
    // Text Field
    $woocommerce_marka_field = $_POST['marka_field'];
    if( !empty( $woocommerce_marka_field ) )
    update_post_meta( $post_id, 'marka_field', esc_attr( $woocommerce_marka_field) );
    $woocommerce_marka_field = $_POST['marka_field'];
    if( !empty( $woocommerce_marka_field ) )
    update_post_meta( $post_id, 'marka_field', esc_attr( $woocommerce_marka_field) );
    }
    echo '';
    }

    /* end woocoommerce */

    I think is good, but after publishing product im receiving something error like that


    Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'woo_add_custom_general_fields_save' not found or invalid function name in /sklepogrodniczy/wp-includes/plugin.php on line 429

    Warning: Cannot modify header information - headers already sent by (output started at /sklepogrodniczy/wp-includes/plugin.php:429) in /sklepogrodniczy/wp-admin/post.php on line 233

    Warning: Cannot modify header information - headers already sent by (output started at /sklepogrodniczy/wp-includes/plugin.php:429) in /sklepogrodniczy/wp-includes/pluggable.php on line 896

    Did i do something wrong, or whats the problem :) ?

  64. Hi Remi,

    I still have no luck with those checkboxes. Even when I use your code from tutorial and even when I use different (clean) wordpress website, it’s not working properly. So yet again, values are saved when checkbox is selected and product is saved, but checkbox is not selected after that. So if I choose to update anything on that product and I don’t check that checkbox again it’s going to overwrite previous save.

    What is the problem here?

    again my function.php with your chunk of code:

    http://pastebin.com/mSbiCQMY

  65. Hi Remi,

    I followed your guide and was able to get a field to show up in dashboard.
    Then added a line to short-description.php and it displayed on front end.

    However, its not possible to clear the field after something was added to it.
    I can change whats there, but not remove. Selecting whats there and clicking delete, then pressing Update and what was there – stays.

    Code:

    // Display Fields
    add_action( ‘woocommerce_product_options_general_product_data’, ‘woo_add_custom_general_fields’ );

    // Save Fields
    add_action( ‘woocommerce_process_product_meta’, ‘woo_add_custom_general_fields_save’ );

    function woo_add_custom_general_fields() {

    global $woocommerce, $post;

    // Text Field
    woocommerce_wp_text_input(
    array(
    ‘id’ => ‘_sampletextarea’,
    ‘label’ => __( ‘Sample Text Area’, ‘woocommerce’ ),
    ‘placeholder’ => ‘something here’,
    ‘desc_tip’ => ‘true’,
    ‘description’ => __( ‘Enter the custom value here.’, ‘woocommerce’ )
    )
    );
    }
    function woo_add_custom_general_fields_save( $post_id ){

    // Text Field
    $woocommerce_text_field = $_POST['_sampletextarea'];
    if( !empty( $woocommerce_text_field ) )
    update_post_meta( $post_id, ‘_sampletextarea’, esc_attr( $woocommerce_text_field ) );
    }

    And this line added to .php file

    Sample:

  66. Hi, great tutorial Gerhard! I was lookin for something like this several time ago, but what if we want to put the values of the new custom fields in [cart.php, mini-cart.php or form-checkout.php and mail notifications]? You now, in order to show the values in the same way that show up variables, in my case i want to print in the order this little reference… do you have any advice? :)

  67. Hi,
    this is great and works fine. But not on cart page. When I try to get and display value on this page, nothing happens.

    In CART.PHP:

    Custom Field “latinsky” exists and its value displays on other pages. I believe the problem might be in getting the right ID?

    Any suggestions?

    Thanks a lot.
    Martin

  68. Hi Remi,

    Can you please provide a fix for data not being cleared on save, when fields data was removed? Thats the most common problem in this discussion..
    Your reply will be highly appreciated and rewarded with coffee :)

  69. Hi – love the code – got it to work on the site – only problem I have is then importing a large product database and mapping to the new fields.

    I can manually enter values into the new fields I created, but when I import (25K+ products) the import tool I use (dgrundel woo product import) it simply creates new custom fields and places them on the additional information tab. I really need the info to display on the main product pages as people who shop this product add multiple items at a time and use those fields to find the right product.

    Any ideas on how to bulk upload to the new fields?

  70. Hi!
    I’ve set a custom field and put a value to test your code.
    Now I would like to put none value for a product.
    It seems that I can’t do that. The field requires a value.
    How to get the possibility to set none value for a field?
    Regards.

  71. […] simple solution to adding custom fields to the WooCommerce Product page. You can checkout his blog here for adding not only text fields but drop downs, checkboxes, select buttons, and more. You would […]

  72. Thank you for the helpful article! This worked out perfectly as I was trying to use the wordpress custom fields but I kinda wanted to keep that area hidden in the screen options. This way seems a little more UI friendly if that makes sense…

    Hey, I got this working great and I know how to insert this into a template itself but I’d rather place it in a hook via the functions file. I’d like to have this as a conditional so that IF the text field has anything input then display it. Otherwise if the field is blank I’d like it ignored by the template. I’m trying to hook the “woocommerce_product_meta_start” to have the field display there if applicable.

    Could you possibly provide an example of such a filter/conditional for the functions file? I’m new to the idea of filters and have managed to figure out quite a few… but can’t seem to wrap my head around this one. I was thinking others might benefit from the example as well.

    If you can’t I totally understand and really appreciate the tutorial you put so much effort into. I’ll just keep trial/error’ing it but if you can help out that would be awesome!

    Thanks!

  73. Here is an example of what I have in the template itself for example until I figure out how to translate it into a hook for the “woocommerce_product_meta_start”:

    ID, '_text_field_age_restriction', true) ) { ?>

    CHOKING HAZARD - Small parts. Not for children under ID, "_text_field_age_restriction", $single = true); ?> yrs.

  74. Sorry, I think I forgot to put the closing code tag or something and it won’t let me edit my comment. Trying this again…

    ID, '_text_field_age_restriction', true) ) { ?>

    CHOKING HAZARD - Small parts. Not for children under ID, "_text_field_age_restriction", $single = true); ?> yrs.

  75. Ok, sorry… not sure why it won’t let my code display but hopefully you can view it all still. Feel free to edit or delete any of my posts here. My apologies for whatever I’m doing wrong…

  76. Hi! Thank you very much for your article. It’s very useful.
    I have followed your tutorial setp by step, but I’m facing the following error:

    Fatal error: Call to undefined function woocommerce_wp_text_input() in [path to my functions.php] on line 30

    Could you help me?

    Thank you very much!

  77. hi Remi can i print custom field content on my woocommerce single-product.php file — how it is possible …

    • you can use get_post_meta() function.

      • actually i have use your function in my woocommerce page …
        What should I do to print it on my php script file ..
        please help me bro………

        i have to use this function …….
        // Display Fields
        add_action( ‘woocommerce_product_options_general_product_data’, ‘woo_add_custom_general_fields’ );

        // Save Fields
        add_action( ‘woocommerce_process_product_meta’, ‘woo_add_custom_general_fields_save’ );

        function woo_add_custom_general_fields() {

        global $woocommerce, $post;

        // Text Field
        woocommerce_wp_textarea_input(
        array(
        ‘id’ => ‘_textarea’,
        ‘label’ => __( ‘My Textarea’, ‘woocommerce’ ),
        ‘placeholder’ => ”,
        ‘desc_tip’ => ‘true’,
        ‘description’ => __( ‘Enter the custom value here.’, ‘woocommerce’ )
        )
        );
        }
        function woo_add_custom_general_fields_save( $post_id ){

        // Textarea
        $woocommerce_textarea = $_POST['_textarea'];
        if( !empty( $woocommerce_textarea ) )
        update_post_meta( $post_id, ‘_textarea’, esc_html( $woocommerce_textarea ) );

        }

      • I would love to see an Extension for WooCommerce to add “custom form fields” functionality to the products pages. In some cases extra information needs to be captured along with the product or service you are selling.

        i want print custom field information…

  78. Hi dear Remi………….
    I want uplode image section in Simple product area….
    How do I upload… Please give me function structure ….
    help me…..

  79. Hi Remi – Thank you for this. It is very insightful and I really appreciate it. I do have one question. We used this to create fields that need populated for many products. They are only visible on the back end so they didn’t have to be written to the user interface. I added some code to allow these fields to be exported, however, I would like to populate my spreadsheet and import those products because there are a LOT. I am unable to figure out how to get the product features to write to the new fields I created. So, I hope I am making sense but I can add the fields, get them to export, but I can’t import and write to the fields…can you help?

  80. Hi Remi,

    Thank you so much for this lesson, I am actually surprised this is not part of the core of WordPress already.

    I have been able to display the 5 fields and one text area using this code

    // Display Fields
    add_action( ‘woocommerce_product_options_general_product_data’, ‘woo_add_custom_general_fields’ );

    // Save Fields
    add_action( ‘woocommerce_process_product_meta’, ‘woo_add_custom_general_fields_save’ );

    // Function to add fields
    function woo_add_custom_general_fields() {

    global $woocommerce, $post;

    echo ”;

    // Custom fields will be created here…// Text Field
    woocommerce_wp_text_input(
    array(
    ‘id’ => ‘_text_field’,
    ‘label’ => __( ‘Fabric Colour’, ‘woocommerce’ ),
    ‘placeholder’ => ‘Enter Colour’,
    ‘desc_tip’ => ‘true’,
    ‘description’ => __( ‘Enter the custom value here.’, ‘woocommerce’ )
    )
    );
    // Text Field
    woocommerce_wp_text_input(
    array(
    ‘id’ => ‘_text_field02′,
    ‘label’ => __( ‘Fabric Contents’, ‘woocommerce’ ),
    ‘placeholder’ => ‘Enter Contents’,
    ‘desc_tip’ => ‘true’,
    ‘description’ => __( ‘Enter the custom value here.’, ‘woocommerce’ )
    )
    );
    woocommerce_wp_text_input(
    array(
    ‘id’ => ‘_text_field03′,
    ‘label’ => __( ‘Country of origin’, ‘woocommerce’ ),
    ‘placeholder’ => ‘Enter Country’,
    ‘desc_tip’ => ‘true’,
    ‘description’ => __( ‘Enter the custom value here.’, ‘woocommerce’ )
    )
    );
    woocommerce_wp_text_input(
    array(
    ‘id’ => ‘_text_field04′,
    ‘label’ => __( ‘Fabric Width or Roll height’, ‘woocommerce’ ),
    ‘placeholder’ => ‘Enter Width’,
    ‘desc_tip’ => ‘true’,
    ‘description’ => __( ‘Enter the custom value here.’, ‘woocommerce’ )
    )
    );
    woocommerce_wp_text_input(
    array(
    ‘id’ => ‘_text_field05′,
    ‘label’ => __( ‘Fabric Pattern.’, ‘woocommerce’ ),
    ‘placeholder’ => ‘Enter Measurement’,
    ‘desc_tip’ => ‘true’,
    ‘description’ => __( ‘Enter the custom value here.’, ‘woocommerce’ )
    )
    );
    // Textarea
    woocommerce_wp_textarea_input(
    array(
    ‘id’ => ‘_textarea’,
    ‘label’ => __( ‘Notes about Fabric.’, ‘woocommerce’ ),
    ‘placeholder’ => ‘Enter Notes’,
    ‘desc_tip’ => ‘true’,
    ‘description’ => __( ‘Enter the custom value here.’, ‘woocommerce’ )
    )
    );
    function woo_add_custom_general_fields_save( $post_id ){

    // Text Field
    $woocommerce_text_field = $_POST['_text_field'];
    if( !empty( $woocommerce_text_field ) )
    update_post_meta( $post_id, ‘_text_field’, esc_attr( $woocommerce_text_field ) );
    // Textarea
    $woocommerce_textarea = $_POST['_textarea'];
    if( !empty( $woocommerce_textarea ) )
    update_post_meta( $post_id, ‘_textarea’, esc_html( $woocommerce_textarea ) );
    }
    echo ”;

    }

    But when I Update a product I get the follow errors

    Warning: call_user_func_array() expects parameter 1 to be a valid callback, function ‘woo_add_custom_general_fields_save’ not found or invalid function name in /home/queentex/public_html/wp-includes/plugin.php on line 429

    Warning: Cannot modify header information – headers already sent by (output started at /home/queentex/public_html/wp-includes/plugin.php:429) in /home/queentex/public_html/wp-admin/post.php on line 233

    Warning: Cannot modify header information – headers already sent by (output started at /home/queentex/public_html/wp-includes/plugin.php:429) in /home/queentex/public_html/wp-includes/pluggable.php on line 896

    I hope if you have some time and if you have any thoughts or suggestions about what I am doing wrong, you could let me know.

    Thank you kindly sir.

    ENB..//

    • Ok so I found a little bit of awesome help and found a missing bracket with one of the good chaps over at stake exchange…

      here is the new code that is saving to the database in the product, but is still not showing on the front end and has given me a new error..

      o first a link to the web page on stake exchange..

      http://wordpress.stackexchange.com/questions/140858/custom-fields-for-woocommerce/140902?iemail=1&noredirect=1#140902

      then the code that saves to the dbase..

      // Display Fields
      add_action( ‘woocommerce_product_options_general_product_data’, ‘woo_add_custom_general_fields’ );

      // Save Fields
      add_action( ‘woocommerce_process_product_meta’, ‘woo_add_custom_general_fields_save’ );

      // Function to add fields
      function woo_add_custom_general_fields() {

      global $woocommerce, $post;

      echo ”;

      // Custom fields will be created here…// Text Field
      woocommerce_wp_text_input(
      array(
      ‘id’ => ‘_text_field_fabric_colour’,
      ‘label’ => __( ‘Fabric Colour’, ‘woocommerce’ ),
      ‘placeholder’ => ‘Enter Colour’,
      ‘desc_tip’ => ‘true’,
      ‘description’ => __( ‘Enter the custom value here.’, ‘woocommerce’ )
      )
      );
      // Text Field
      woocommerce_wp_text_input(
      array(
      ‘id’ => ‘_text_field_fabric_contents’,
      ‘label’ => __( ‘Fabric Contents’, ‘woocommerce’ ),
      ‘placeholder’ => ‘Enter Contents’,
      ‘desc_tip’ => ‘true’,
      ‘description’ => __( ‘Enter the custom value here.’, ‘woocommerce’ )
      )
      );
      woocommerce_wp_text_input(
      array(
      ‘id’ => ‘_text_field_country_origin’,
      ‘label’ => __( ‘Country of origin’, ‘woocommerce’ ),
      ‘placeholder’ => ‘Enter Country’,
      ‘desc_tip’ => ‘true’,
      ‘description’ => __( ‘Enter the custom value here.’, ‘woocommerce’ )
      )
      );
      woocommerce_wp_text_input(
      array(
      ‘id’ => ‘_text_field_fabric_width’,
      ‘label’ => __( ‘Fabric Width or Roll height’, ‘woocommerce’ ),
      ‘placeholder’ => ‘Enter Width’,
      ‘desc_tip’ => ‘true’,
      ‘description’ => __( ‘Enter the custom value here.’, ‘woocommerce’ )
      )
      );
      woocommerce_wp_text_input(
      array(
      ‘id’ => ‘_text_field_fabric_pattern’,
      ‘label’ => __( ‘Fabric Pattern.’, ‘woocommerce’ ),
      ‘placeholder’ => ‘Enter Measurement’,
      ‘desc_tip’ => ‘true’,
      ‘description’ => __( ‘Enter the custom value here.’, ‘woocommerce’ )
      )
      );
      // Textarea
      woocommerce_wp_textarea_input(
      array(
      ‘id’ => ‘_textarea_fabric_notes’,
      ‘label’ => __( ‘Notes about Fabric.’, ‘woocommerce’ ),
      ‘placeholder’ => ‘Enter Notes’,
      ‘desc_tip’ => ‘true’,
      ‘description’ => __( ‘Enter the custom value here.’, ‘woocommerce’ )
      )
      );
      }
      function woo_add_custom_general_fields_save( $post_id ){

      // Text Field
      $woocommerce_text_field = $_POST['_text_field_fabric_colour'];
      if( !empty( $woocommerce_text_field ) )
      update_post_meta( $post_id, ‘_text_field_fabric_colour’, esc_attr( $woocommerce_text_field ) );
      $woocommerce_text_field = $_POST['_text_field_fabric_contents'];
      if( !empty( $woocommerce_text_field ) )
      update_post_meta( $post_id, ‘_text_field_fabric_contents’, esc_attr( $woocommerce_text_field ) );
      $woocommerce_text_field = $_POST['_text_field_country_origin'];
      if( !empty( $woocommerce_text_field ) )
      update_post_meta( $post_id, ‘_text_field_country_origin’, esc_attr( $woocommerce_text_field ) );
      $woocommerce_text_field = $_POST['_text_field_fabric_width'];
      if( !empty( $woocommerce_text_field ) )
      update_post_meta( $post_id, ‘_text_field_fabric_width’, esc_attr( $woocommerce_text_field ) );
      $woocommerce_text_field = $_POST['_text_field_fabric_pattern'];
      if( !empty( $woocommerce_text_field ) )
      update_post_meta( $post_id, ‘_text_field_fabric_pattern’, esc_attr( $woocommerce_text_field ) );
      // Textarea
      $woocommerce_textarea = $_POST['_textarea_fabric_notes'];
      if( !empty( $woocommerce_textarea ) )
      update_post_meta( $post_id, ‘_textarea_fabric_notes’, esc_html( $woocommerce_textarea ) );

      echo get_post_meta( get_the_ID(), ‘_text_field_fabric_colour’, true );
      echo get_post_meta( get_the_ID(), ‘_text_field_fabric_contents’, true );
      echo get_post_meta( get_the_ID(), ‘_text_field_country_origin’, true );
      echo get_post_meta( get_the_ID(), ‘_text_field_fabric_width’, true );
      echo get_post_meta( get_the_ID(), ‘_text_field_fabric_pattern’, true );
      echo get_post_meta( get_the_ID(), ‘_textarea_fabric_notes’, true );
      ”;
      }

      and now my new error when I hit save..

      red80% poly 20% cottonCanada90″8″ 900″ o ya lots of inchesthis is an awesome that rocks.
      Warning: Cannot modify header information – headers already sent by (output started at /home/queentex/public_html/wp-content/themes/enfold/functions.php:566) in /home/queentex/public_html/wp-admin/post.php on line 233

      Warning: Cannot modify header information – headers already sent by (output started at /home/queentex/public_html/wp-content/themes/enfold/functions.php:566) in /home/queentex/public_html/wp-includes/pluggable.php on line 896

      I am using the enfold theme from Kriesi.. they rock, it rocks, from theme forest.. it is here http://themeforest.net/collections/4283887-best-themes-in-the-world

      I also checked into the guy who tailgated this post.. http://ingoodcompanywebdesign.net/blog/2014/03/17/custom-fields-for-woocommerce-tailgating-remi-corson/ and checked out this too.. http://gerhardpotgieter.com/2013/09/17/woocommerce-custom-product-fields/ and I think my brain is melting now..

      I’ve taken to using attributes, but I think this product would work better if I could master custom fields on the general tab… http://queentextiles.ca/shop/uncategorized/scottish-tartan-plaid/

      Any thoughts you may have would be awesome Remi..

      cheers

      ENB..//

  81. Hey Remi,

    Great code snippet, very helpful. I got it to work great.

    The text field saves and displays when the product is updated.

    But if i go in and delete what i have input in the text field and update the product, the text remains saved.

    Any ideas on how to save the field when the input text has been deleted

  82. HenkvandenBerg

    Hi Remi,
    I just bought the product-add-ons from Woothemes.
    I would like to have the custom tekst field together with product field to be saved as a unique product. e.g. customtext.productname or myname.domainname.xx
    Do you think that is possible?

    Thanks in advance for your help.

  83. derek.foster94

    Hey,

    I followed your instructions, step by step, and I was able to get the custom fields added into the product page editor in my dashboard, and the fields are saving after I ‘update’ the page, but I’m not seeing any results on the live site. I made sure to include the code that ‘gets’ the new meta data to display it, but I’m still seeing nothing. Any thoughts?

  84. Hi Remi:
    Thank you so much for your post has been very helpful to me. I am a rookie in wordpress and all this things. I followed all the steps to create the custom fields and all looks good but know I wanna show it in the page, I attached an screenshot where you can see where I want the fields, please help me with that because I really don’t know how to do that, sorry for my dumb question and my poor English I am not a English native speaker.
    Thank you…

    http://d.pr/i/VPsf

  85. Hi again Remi:
    I figured out how to display the value of the field that I want but its show it w/o the label. how can I display the label of the field?

    Thanks

  86. i want add my custom text box in my post and page section …
    hi dear please tell me.. how is it;

  87. Hello Remi and thank you for your WooCommerce articles and snippets. However, i am trying to find a way to do the exact opposite of this – hide a few of the settings fields to simplify the user experience. What i wish to achieve is a way to define a few of the settings myself and leave only a chosen few to be defined by the user. Is this possible, and how?
    Thank you.

  88. Hello Remi,

    is there any way i could assign custom class or id to checkbox, so i could have that class/id on single product page? I did used “echo get_post_meta( get_the_ID(), ‘my-field-slug’, true );” and i do get “yes” as value in between span tags, but i would like to stylize that span somehow. So i tried to wrap this span inside of a div but div is not displayed on page at all. So any help from anybody here would be much appreciated. Just simple solution how to add custom class or id to this checkbox span “yes” value?

Leave a Reply