Добавление 2 настраиваемых полей в категорию товаров и тегов в WooCommerce, но код, дающий уведомление - PullRequest
0 голосов
/ 21 января 2020

со ссылкой на этот вопрос. Добавление настраиваемого поля в категорию товаров в WooCommerce

Я пытаюсь использовать этот код, но он уведомляет меня.

Примечание: неопределенная переменная: term_id в /home/userdir/public_html/wp-content/themes/theme-child/functions.php в строке 61

Вот код, который я пытаюсь с небольшой модификацией. Кто-нибудь, пожалуйста, дайте мне знать, что я делаю здесь не так?

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>
        <textarea name="term_meta[wh_meta_title]" id="term_meta[wh_meta_title]"></textarea>
        <p class="description"><?php _e('Enter a Text at Bottom, <= 160 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 Text at Top, <= 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('Text at Bottom', 'text_domain'); ?></label></th>
        <td>
            <textarea name="term_meta[wh_meta_title]" id="term_meta[wh_meta_title]"><?php echo esc_attr($term_meta['wh_meta_title']) ? esc_attr($term_meta['wh_meta_title']) : ''; ?></textarea>
            <p class="description"><?php _e('Text at Bottom, <= 160 character', 'text_domain'); ?></p>
        </td>
    </tr>
    <tr class="form-field">
        <th scope="row" valign="top"><label for="term_meta[wh_meta_desc]"><?php _e('Text at Top', '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_desc']) : ''; ?></textarea>
            <p class="description"><?php _e('Text at Top', '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);

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

//Product Tag creation page
function tag_taxonomy_add_new_meta_field() {
    ?>
    <div class="form-field">
        <label for="term_meta[tg_meta_title]"><?php _e('Text at Bottom', 'text_domain'); ?></label>
        <textarea  name="term_meta[tg_meta_title]" id="term_meta[tg_meta_title]"></textarea>
        <p class="description"><?php _e('Text at Bottom, <= 160 character', 'text_domain'); ?></p>
    </div>
    <div class="form-field">
        <label for="term_meta[tg_meta_desc]"><?php _e('Text at Top', 'text_domain'); ?></label>
        <textarea name="term_meta[tg_meta_desc]" id="term_meta[tg_meta_desc]"></textarea>
        <p class="description"><?php _e('Text at bottom, <= 160 character', 'text_domain'); ?></p>
    </div>
    <?php
}
add_action('product_tag_add_form_fields', 'tag_taxonomy_add_new_meta_field', 10, 2);
//Product tag Edit page
function tag_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[tg_meta_title]"><?php _e('Text at Bottom', 'text_domain'); ?></label></th>
        <td>
            <textarea name="term_meta[tg_meta_title]" id="term_meta[tg_meta_title]"><?php echo esc_attr($term_meta['tg_meta_title']) ? esc_attr($term_meta['tg_meta_title']) : ''; ?></textarea>
            <p class="description"><?php _e('Text will show in bottom, <= 260 character', 'text_domain'); ?></p>
        </td>
    </tr>
    <tr class="form-field">
        <th scope="row" valign="top"><label for="term_meta[tg_meta_desc]"><?php _e('Text at Top', 'text_domain'); ?></label></th>
        <td>
            <textarea name="term_meta[tg_meta_desc]" id="term_meta[tg_meta_desc]"><?php echo esc_attr($term_meta['tg_meta_desc']) ? esc_attr($term_meta['tg_meta_desc']) : ''; ?></textarea>
            <p class="description"><?php _e('Text will show at Top', 'text_domain'); ?></p>
        </td>
    </tr>
<?php
}
add_action('product_tag_edit_form_fields', 'tag_taxonomy_edit_meta_field', 10, 2);
// Save extra taxonomy fields callback function.
function tag_save_taxonomy_custom_meta($term_id) {
    if (isset($_POST['term_meta'])) {
        $term_meta = get_option("taxonomy_" . $term_id);
        $tag_keys = array_keys($_POST['term_meta']);
        foreach ($tag_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_tag', 'tag_save_taxonomy_custom_meta', 10, 2);
add_action('create_product_tag', 'tag_save_taxonomy_custom_meta', 10, 2);

$metaArray = get_option('taxonomy_' . $term_id);
echo $productTagMetaTitle = $metaArray['tg_meta_title'];
echo $productTagMetaDesc = $metaArray['tg_meta_desc'];

1 Ответ

1 голос
/ 21 января 2020

Удалить этот код

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

Из двух мест в вашем коде

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...