• Resolved ivancazorla

    (@ivancazorla)


    Hi there. I wanted to have an aditional even longer description at the bottom page of each product category. For that I’ve figured a way creating a function hook using if/else variables, like this:

    ————————————————————————
    add_action( ‘woocommerce_after_main_content’, ‘my_custom_text’, 10 );

    function my_custom_text() {
    if ( is_product_category() ) {
    if ( is_product_category( ‘shirts’ ) ) {
    echo ‘Hi! Take a look at our sweet tshirts below.’;
    } else {
    echo ‘Hi! Check our our products below.’;
    }
    }
    }
    ————————————————————————

    The problem is that I would have to manually introduce all the text I want inside here, and it can be not as intuitive for other people to use. Is it possible with that function to create a custom field in each one of the categories I say, so that I can later go to that category and have another field to enter my own text?

    Similar to the field we have like name of category, description, etc, the purpose will be to add another field.

    And that custom field should be displaying at the bottom of the category page, after showing all the products.

    Thank you in advance.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support mouli a11n

    (@mouli)

    Hi there,
    There are a couple of useful hooks you can use to add fields to Categories as you are discussing.
    product_cat_add_form_fields
    product_cat_edit_form_fields
    which used in conjunction with the following hooks:
    edited_product_cat
    create_product_cat
    will do what you want to achieve.

    A good example of this is to be found here:
    https://stackoverflow.com/questions/23469841/adding-custom-field-to-product-category-in-woocommerce
    and this is a code snippet from that article to get you started

    
    //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);

    Good luck with it ??

    • This reply was modified 6 years, 4 months ago by mouli a11n.
    Thread Starter ivancazorla

    (@ivancazorla)

    Hi there, I’ve been playing with the code you gave me but as for what I’ve seen that is made to create the field for meta text inside teh category.

    What I would love to achieve is mix my code for an extra long description at the end of the category page, including the custom field that appears in the category editor to introduce the text as you can with the meta example.

    I’m kinda new in all this stuff.

    Thanks,
    Ivan

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Creating custom field in WooCommerce categories’ is closed to new replies.