Adding Custom Fields to WooCommerce Product Category

Adding Custom Fields to WooCommerce Product Category

custom-product-category-meta

WooCommerce comes in with a whole lot of features and options but there are times when we need to customized a little further, just for example if we need to add an option to have our custom Meta Title and Meta Description or even an icon class or icon image URL. Now the question arises how to achieve this.
WordPress provides us with the 4 action which comes handy in this.

  • {taxonomy_name}_add_form_fields => to add fields in taxonomy create form
  • {taxonomy_name}_edit_form_fields => to add fields in taxonomy edit form
  • edited_{taxonomy_name} => to process edit form fields
  • create_{taxonomy_name} => to process create form fields
  • manage_edit-{taxonomy_name}_columns => to register custom column for admin screen
  • manage_{taxonomy_name}_custom_column => to populate content of custom column in admin screen

What is {taxonomy_name}?

In WordPress, a taxonomy is a means of organizing (or categorizing) something. WordPress posts are configured by default with two taxonomies: “categories” and “tags”. The values you create within those categories are known as its “terms”.  So {taxonomy_name} stands for the slug that is associated with the taxonomy, for example: Post Categories => category, Post Tag => post_tag, Woo Product Categories => product_cat, Woo Product Tags => product_tag, etc..

Now let us get’s our hands dirty.

Now that we have added form we need to handle the form request.

All the above code can be added into the active theme or child theme functions.php file or also in any of our active plugin PHP files.

The above code will produce an output like this on the edit page.

product-category-edit-page

 

How to retrive custom fields values which we have just created ?

Now that we have learned as how to add this fields in Product Category, so if we what to add this in Post Category fields then we just have to replace product_cat with category from all the four mentioned action so action will look like some this like this. product_cat_add_form_fields => category _add_form_fields and so on.

How to show custom fields values in Product Category Admin Screen listing?

Share this Post:

About Raunak Gupta

I'm an expert Laravel, WooCommerce, WordPress theme and plugin developer, over the time I have developed several sites and applications. I like to help the upcoming developer, So I share my experience through this blog.  

Related Posts:

12 thoughts on “Adding Custom Fields to WooCommerce Product Category

  1. adytzul89@gmail.com'

    Added functions in file functions.php by template, but where add code:
    $productCatMetaTitle = get_term_meta($term_id, ‘wh_meta_title’, true);
    $productCatMetaDesc = get_term_meta($term_id, ‘wh_meta_desc’, true);
    ?

    Thank..

    Reply
  2. jose.kaspar@email.cz'

    Hello,

    i am trying to send data. But forms are empty after saving edits.

    Why? Thank you.

    Reply
  3. mickiemeekin@gmail.com'

    Highly energetic article, I liked that a lot.

    Will there be a part 2?

    Reply
  4. joesphmolnar@web.de'

    Normally I don’t learn post on blogs, however I wish to say that this write-up very forced me to try
    and do it! Your writing style has been surprised me.

    Thank you, quite nice post.

    Reply
  5. hjorthammar@gmail.com'

    Hi Raunak,

    I am trying to use your code for adding a few radio buttons. I want to use different templates for different different category pages.

    I am able to add the radio buttons to the Cat Create page and Cat Edit page using the following (your code updated) :

    //Product Cat Create page
    function wh_taxonomy_add_new_meta_field() {
    ?>

    Standard
    Sprängskissmall

    term_id;

    // retrieve the existing value(s) for this meta field.
    $wh_meta_category = get_term_meta($term_id, ‘wh_meta_category’, true);
    // $wh_meta_desc = get_term_meta($term_id, ‘wh_meta_desc’, true);
    ?>

    Standard
    Sprängskissmall

    <?php
    }

    add_action('product_cat_add_form_fields', 'wh_taxonomy_add_new_meta_field', 10, 1);
    add_action('product_cat_edit_form_fields', 'wh_taxonomy_edit_meta_field', 10, 1);

    // Save extra taxonomy fields callback function.
    function wh_save_taxonomy_custom_meta($term_id) {

    $wh_meta_category = filter_input(INPUT_POST, 'wh_meta_category');

    update_term_meta($term_id, 'wh_meta_category', $wh_meta_category);
    }

    add_action('edited_product_cat', 'wh_save_taxonomy_custom_meta', 10, 1);
    add_action('create_product_cat', 'wh_save_taxonomy_custom_meta', 10, 1);

    However, the value is not saved when I check it.

    Reply
    • Hi Peter,

      Is your radio filed name is set to name='wh_meta_category' and what you get in wh_save_taxonomy_custom_meta() method if you print the variable print_r($wh_meta_category);?

      Reply
  6. aukro.planet@gmail.com'

    Great, thanks!
    But the only little thing helped me to retrieve the meta_title on the front is get_queried_object_id() instead of your $term_id:
    echo get_term_meta(get_queried_object_id(), 'wh_meta_title', true);
    instead of
    echo get_term_meta($term_id, 'wh_meta_title', true);

    Reply
  7. emkamakeoff@gmail.com'

    I added all your code, expecting (How to show custom fields values in Product Category Admin Screen listing?) part to functions.php.
    How can I add displaying code into functions.php like HOOK?
    Thank you.

    Reply
    • Sorry didn’t get you on this. Do you want to know how to show the get the value and show it in frontend?

      If so then you can use it like this
      You can use this $productCatMetaTitle = get_term_meta($term_id, 'wh_meta_title', true); to retrive the value now you can display it were ever you want.

      Reply

Leave a Comment

Your email address will not be published. Required fields are marked *


*