Site logo

[Support request] Woocommerce Category Custom Field & Dynamic Option

Home Forums Support [Support request] Woocommerce Category Custom Field & Dynamic Option

Home Forums Support Woocommerce Category Custom Field & Dynamic Option

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #1707340
    melvin

    Hi,

    Objective: I would like to show dynamic option using element block module (for page hero) on woocommerce product category.
    https://imgur.com/y5qIgo7

    I’ve tried a few setting but it doesnt work. Can you please help me to see how to set properly?

    Things i’ve done.

    Step 1: Insert code to create custom field on woocommerce category page

    //Product Cat creation page
    function from_price_taxonomy_add_new_meta_field() {
        ?>
        <div class="form-field">
            <label for="term_meta[wh_meta_price]"><?php _e('Meta Price', 'from_price'); ?></label>
            <input type="text" name="term_meta[wh_meta_price]" id="term_meta[wh_meta_price]">
            <p class="description"><?php _e('Package with lowest price in this category', 'from_price'); ?></p>
        </div>
        <div class="form-field">
            <label for="term_meta[wh_meta_desc]"><?php _e('Meta Description', 'from_price'); ?></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', 'from_price'); ?></p>
        </div>
        <?php
    }
    
    add_action('product_cat_add_form_fields', 'from_price_taxonomy_add_new_meta_field', 10, 2);
    
    //Product Cat Edit page
    function from_price_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_price]"><?php _e('Meta Price', 'from_price'); ?></label></th>
            <td>
                <input type="text" name="term_meta[wh_meta_price]" id="term_meta[wh_meta_price]" value="<?php echo esc_attr($term_meta['wh_meta_price']) ? esc_attr($term_meta['wh_meta_price']) : ''; ?>">
                <p class="description"><?php _e('package with lowest price in this category', 'from_price'); ?></p>
            </td>
        </tr>
        <tr class="form-field">
            <th scope="row" valign="top"><label for="term_meta[wh_meta_desc]"><?php _e('Meta Description', 'from_price'); ?></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_price']) : ''; ?></textarea>
                <p class="description"><?php _e('Enter a meta description', 'from_price'); ?></p>
            </td>
        </tr>
        <?php
    }
    
    add_action('product_cat_edit_form_fields', 'from_price_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);

    Step 2 : Set Dynamic Options
    https://imgur.com/ahgTUa5

    The meta price i set is not shown. Could you please check where have i set wrongly and guide me to show it correctly?

    TQ

    Melvin

    #1707751
    David
    Staff
    Customer Support

    Hi there,

    first thing to check is whether the term_meta key is correct.
    Create a temporary Hook Element and add this snippet replacing your_meta_key_name with the relevant meta key:

    <?php 
    echo 'test output - meta key value should be below';
    echo get_term_meta( get_queried_object_id(), 'your_meta_key_name', true); 
    ?>

    And hook this into the after_header

    Does the value get displayed?

    If it doesn’t then it means the meta_key is not correct.

    #1710144
    melvin

    Hi David,

    Thanks for your response. I’ve tested on this page but the value doesn’t display.

    This is the code i used

    <?php 
    echo 'test output - meta key value should be below';
    echo get_term_meta( get_queried_object_id(), 'wh_meta_price', true); 
    ?>

    This is the custom field code is used

    //Product Cat creation page
    function from_price_taxonomy_add_new_meta_field() {
        ?>
        <div class="form-field">
            <label for="term_meta[wh_meta_price]"><?php _e('Meta Price', 'from_price'); ?></label>
            <input type="text" name="term_meta[wh_meta_price]" id="term_meta[wh_meta_price]">
            <p class="description"><?php _e('Package with lowest price in this category', 'from_price'); ?></p>
        </div>
        <div class="form-field">
            <label for="term_meta[wh_meta_desc]"><?php _e('Meta Description', 'from_price'); ?></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', 'from_price'); ?></p>
        </div>
        <?php
    }
    
    add_action('product_cat_add_form_fields', 'from_price_taxonomy_add_new_meta_field', 10, 2);
    
    //Product Cat Edit page
    function from_price_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_price]"><?php _e('Meta Price', 'from_price'); ?></label></th>
            <td>
                <input type="text" name="term_meta[wh_meta_price]" id="term_meta[wh_meta_price]" value="<?php echo esc_attr($term_meta['wh_meta_price']) ? esc_attr($term_meta['wh_meta_price']) : ''; ?>">
                <p class="description"><?php _e('package with lowest price in this category', 'from_price'); ?></p>
            </td>
        </tr>
        <tr class="form-field">
            <th scope="row" valign="top"><label for="term_meta[wh_meta_desc]"><?php _e('Meta Description', 'from_price'); ?></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_price']) : ''; ?></textarea>
                <p class="description"><?php _e('Enter a meta description', 'from_price'); ?></p>
            </td>
        </tr>
        <?php
    }
    
    add_action('product_cat_edit_form_fields', 'from_price_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);

    Could you kindly help me to check where’s wrong with the code? or what maybe i used a wrong meta_key?

    Please help.

    Thanks

    #1711079
    Tom
    Lead Developer
    Lead Developer

    It could be that the key is wrong.

    What happens if you do this?:

    var_dump(get_term_meta( get_queried_object_id() ));

    Do you see your key/value in the output?

    #1713478
    melvin

    Hi Tom,

    Thanks for the advice. I’ve tested the code on the below page,

    <?php 
    echo 'test output - meta key value should be below';
    echo var_dump(get_term_meta( get_queried_object_id() ));
    ?>

    And i didnt see the key/value in the output. If you could kind enough to see what’s wrong with my code that i inserted to create a custom field for woocommerce category below? I just added the below code to child theme function.php file

    //Product Cat creation page
    function from_price_taxonomy_add_new_meta_field() {
        ?>
        <div class="form-field">
            <label for="term_meta[wh_meta_price]"><?php _e('Meta Price', 'from_price'); ?></label>
            <input type="text" name="term_meta[wh_meta_price]" id="term_meta[wh_meta_price]">
            <p class="description"><?php _e('Package with lowest price in this category', 'from_price'); ?></p>
        </div>
        <div class="form-field">
            <label for="term_meta[wh_meta_desc]"><?php _e('Meta Description', 'from_price'); ?></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', 'from_price'); ?></p>
        </div>
        <?php
    }
    
    add_action('product_cat_add_form_fields', 'from_price_taxonomy_add_new_meta_field', 10, 2);
    
    //Product Cat Edit page
    function from_price_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_price]"><?php _e('Meta Price', 'from_price'); ?></label></th>
            <td>
                <input type="text" name="term_meta[wh_meta_price]" id="term_meta[wh_meta_price]" value="<?php echo esc_attr($term_meta['wh_meta_price']) ? esc_attr($term_meta['wh_meta_price']) : ''; ?>">
                <p class="description"><?php _e('package with lowest price in this category', 'from_price'); ?></p>
            </td>
        </tr>
        <tr class="form-field">
            <th scope="row" valign="top"><label for="term_meta[wh_meta_desc]"><?php _e('Meta Description', 'from_price'); ?></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_price']) : ''; ?></textarea>
                <p class="description"><?php _e('Enter a meta description', 'from_price'); ?></p>
            </td>
        </tr>
        <?php
    }
    
    add_action('product_cat_edit_form_fields', 'from_price_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);

    Thanks TOM

    #1714352
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    Hard to tell without actually building something, but this might help: https://stackoverflow.com/questions/23469841/adding-custom-field-to-product-category

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.