24

How can I add custom fields to product category? I have added custom field to product but I can't find any extension which provide facility to add custom field to product category.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Vidhi
  • 2,026
  • 2
  • 20
  • 31

3 Answers3

45

You can add custom fields to WooCommerce product category by using following action :

  • product_cat_add_form_fields
  • product_cat_edit_form_fields
  • edited_product_cat
  • create_product_cat

UPDATED on 17-Feb-2017 ###For WP version 4.4 and above.
As of WordPress 4.4, update_term_meta() and get_term_meta() functions have been added. This means that now we don't have to save the data in wp_options table anymore, rather they are now stored in wp_termmeta table.

Here is my updated code.

//Product Cat Create page
function wh_taxonomy_add_new_meta_field() {
    ?>
        
    <div class="form-field">
        <label for="wh_meta_title"><?php _e('Meta Title', 'wh'); ?></label>
        <input type="text" name="wh_meta_title" id="wh_meta_title">
        <p class="description"><?php _e('Enter a meta title, <= 60 character', 'wh'); ?></p>
    </div>
    <div class="form-field">
        <label for="wh_meta_desc"><?php _e('Meta Description', 'wh'); ?></label>
        <textarea name="wh_meta_desc" id="wh_meta_desc"></textarea>
        <p class="description"><?php _e('Enter a meta description, <= 160 character', 'wh'); ?></p>
    </div>
    <?php
}

//Product Cat Edit page
function wh_taxonomy_edit_meta_field($term) {

    //getting term ID
    $term_id = $term->term_id;

    // retrieve the existing value(s) for this meta field.
    $wh_meta_title = get_term_meta($term_id, 'wh_meta_title', true);
    $wh_meta_desc = get_term_meta($term_id, 'wh_meta_desc', true);
    ?>
    <tr class="form-field">
        <th scope="row" valign="top"><label for="wh_meta_title"><?php _e('Meta Title', 'wh'); ?></label></th>
        <td>
            <input type="text" name="wh_meta_title" id="wh_meta_title" value="<?php echo esc_attr($wh_meta_title) ? esc_attr($wh_meta_title) : ''; ?>">
            <p class="description"><?php _e('Enter a meta title, <= 60 character', 'wh'); ?></p>
        </td>
    </tr>
    <tr class="form-field">
        <th scope="row" valign="top"><label for="wh_meta_desc"><?php _e('Meta Description', 'wh'); ?></label></th>
        <td>
            <textarea name="wh_meta_desc" id="wh_meta_desc"><?php echo esc_attr($wh_meta_desc) ? esc_attr($wh_meta_desc) : ''; ?></textarea>
            <p class="description"><?php _e('Enter a meta description', 'wh'); ?></p>
        </td>
    </tr>
    <?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_title = filter_input(INPUT_POST, 'wh_meta_title');
    $wh_meta_desc = filter_input(INPUT_POST, 'wh_meta_desc');

    update_term_meta($term_id, 'wh_meta_title', $wh_meta_title);
    update_term_meta($term_id, 'wh_meta_desc', $wh_meta_desc);
}

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

USAGE, to retrive data:

echo $productCatMetaTitle = get_term_meta($term_id, 'wh_meta_title', true);
echo $productCatMetaDesc = get_term_meta($term_id, 'wh_meta_desc', true);

###For WP version below `4.4`.

Here is the code

//Product Cat creation page
function text_domain_taxonomy_add_new_meta_field() {
    ?>
    <div class="form-field">
        <label for="term_meta[wh_meta_title]"><?php _e('Meta Title', 'text_domain'); ?></label>
        <input type="text" name="term_meta[wh_meta_title]" id="term_meta[wh_meta_title]">
        <p class="description"><?php _e('Enter a meta title, <= 60 character', 'text_domain'); ?></p>
    </div>
    <div class="form-field">
        <label for="term_meta[wh_meta_desc]"><?php _e('Meta Description', 'text_domain'); ?></label>
        <textarea name="term_meta[wh_meta_desc]" id="term_meta[wh_meta_desc]"></textarea>
        <p class="description"><?php _e('Enter a meta description, <= 160 character', 'text_domain'); ?></p>
    </div>
    <?php
}

add_action('product_cat_add_form_fields', 'text_domain_taxonomy_add_new_meta_field', 10, 2);

//Product Cat Edit page
function text_domain_taxonomy_edit_meta_field($term) {

    //getting term ID
    $term_id = $term->term_id;

    // retrieve the existing value(s) for this meta field. This returns an array
    $term_meta = get_option("taxonomy_" . $term_id);
    ?>
    <tr class="form-field">
        <th scope="row" valign="top"><label for="term_meta[wh_meta_title]"><?php _e('Meta Title', 'text_domain'); ?></label></th>
        <td>
            <input type="text" name="term_meta[wh_meta_title]" id="term_meta[wh_meta_title]" value="<?php echo esc_attr($term_meta['wh_meta_title']) ? esc_attr($term_meta['wh_meta_title']) : ''; ?>">
            <p class="description"><?php _e('Enter a meta title, <= 60 character', 'text_domain'); ?></p>
        </td>
    </tr>
    <tr class="form-field">
        <th scope="row" valign="top"><label for="term_meta[wh_meta_desc]"><?php _e('Meta Description', 'text_domain'); ?></label></th>
        <td>
            <textarea name="term_meta[wh_meta_desc]" id="term_meta[wh_meta_desc]"><?php echo esc_attr($term_meta['wh_meta_desc']) ? esc_attr($term_meta['wh_meta_title']) : ''; ?></textarea>
            <p class="description"><?php _e('Enter a meta description', 'text_domain'); ?></p>
        </td>
    </tr>
    <?php
}

add_action('product_cat_edit_form_fields', 'text_domain_taxonomy_edit_meta_field', 10, 2);

// Save extra taxonomy fields callback function.
function save_taxonomy_custom_meta($term_id) {
    if (isset($_POST['term_meta'])) {
        $term_meta = get_option("taxonomy_" . $term_id);
        $cat_keys = array_keys($_POST['term_meta']);
        foreach ($cat_keys as $key) {
            if (isset($_POST['term_meta'][$key])) {
                $term_meta[$key] = $_POST['term_meta'][$key];
            }
        }
        // Save the option array.
        update_option("taxonomy_" . $term_id, $term_meta);
    }
}

add_action('edited_product_cat', 'save_taxonomy_custom_meta', 10, 2);
add_action('create_product_cat', 'save_taxonomy_custom_meta', 10, 2);

USAGE, to retrive data:

$metaArray = get_option('taxonomy_' . $term_id);
echo $productCatMetaTitle = $metaArray['wh_meta_title'];
echo $productCatMetaDesc = $metaArray['wh_meta_desc'];
    

This code goes in function.php file of your active child theme (or theme) or also in any plugin file.

All code is tested, and fully functional.


Reference

Official Doc:

Raunak Gupta
  • 10,412
  • 3
  • 58
  • 97
  • Thanks for the update, hadn't heard they add a termmeta table. – DarkNeuron Feb 23 '17 at 09:42
  • How do you get it to show up as a column/screen option when looking at product categories? – Ray Jan 31 '18 at 17:16
  • @Ray: Good point but it is not related to this question directly, but you can post a question and share me the link. In a mean time I'll update my tutorial link. – Raunak Gupta Feb 01 '18 at 04:48
1

Starting Wordpress 4.4 you should consider waiting for an upgrade of Woocommerce or starting feeding manually the new wp_termmeta table.

https://core.trac.wordpress.org/ticket/10142

snowflake
  • 1,750
  • 2
  • 17
  • 40
0

There is an issue on code for WP version 4.4 and above.

When you change anything (f.e. url slug or name) on category list admin page the custom field value changes to empty string.

I added saving function for editing which does not execute on category edit admin page:

// Save extra taxonomy fields callback function - create.
function wh_save_taxonomy_custom_meta_create($term_id) {

$wh_meta_title = filter_input(INPUT_POST, 'wh_meta_title');
$wh_meta_desc = filter_input(INPUT_POST, 'wh_meta_desc');

update_term_meta($term_id, 'wh_meta_title', $wh_meta_title);
update_term_meta($term_id, 'wh_meta_desc', $wh_meta_desc);
}

// Save extra taxonomy fields callback function - edit.
function wh_save_taxonomy_custom_meta($term_id) {
    $screen = get_current_screen();
    if ( $screen->id != 'edit-product_cat' )
            return; // exit if incorrect screen id

$wh_meta_title = filter_input(INPUT_POST, 'wh_meta_title');
$wh_meta_desc = filter_input(INPUT_POST, 'wh_meta_desc');

update_term_meta($term_id, 'wh_meta_title', $wh_meta_title);
update_term_meta($term_id, 'wh_meta_desc', $wh_meta_desc);
}

add_action('edited_product_cat', 'wh_save_taxonomy_custom_meta', 10, 1);
add_action('create_product_cat', 'wh_save_taxonomy_custom_meta_create', 10, 1);
jerri69
  • 25
  • 3