将自定义字段添加到产品类别 [英] Adding custom field to product category

查看:84
本文介绍了将自定义字段添加到产品类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将自定义字段添加到产品类别?我已经将自定义字段添加到产品中,但是找不到任何扩展功能,可以提供将自定义字段添加到产品类别的功能.

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.

推荐答案

您可以使用以下操作将自定义字段添加到WooCommerce产品类别:

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
    • product_cat_add_form_fields
    • product_cat_edit_form_fields
    • edited_product_cat
    • create_product_cat
    • 更新于2017年2月17日

      UPDATED on 17-Feb-2017

      从WordPress 4.4开始,添加了update_term_meta()get_term_meta()函数.这意味着现在我们不必再将数据保存在wp_options表中,而是将它们存储在wp_termmeta表中.

      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.

      这是我更新的代码.

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

      用法,以检索数据:

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


      这是代码

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

      使用情况,以检索数据:

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

      此代码会出现在您活动的子主题(或主题)的function.php文件中,也可能会出现在任何插件文件中.

      所有代码均已通过测试,并且功能齐全.

      参考:

      • Tutorial: Adding Custom Fields to WooCommerce Product Category
      • Official Doc: product_cat_edit_form_fields

      这篇关于将自定义字段添加到产品类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆