【问题标题】:How do I add custom fields to the categories in Woocommerce?如何将自定义字段添加到 Woocommerce 中的类别?
【发布时间】:2016-09-07 23:20:40
【问题描述】:

我有一个 woocommerce 网站,该网站将产品按类别分组并显示在初始商店页面上。

我想向类别添加自定义字段。

理论:主页上有一个搜索表单,用户输入他们的邮政编码,并显示具有匹配邮政编码详细信息的类别(将在自定义字段中指定)。
我不需要在商店页面上显示自定义字段,我只需要能够在后端应用匹配的邮政编码。

请问有人知道我该怎么做吗?

我已经研究了很长时间,但似乎无法找到解决方案。
非常感谢您提前提供任何答案或建议。
P.S:我不想使用插件,请使用 php/html/javascript (& 其他代码)。

【问题讨论】:

标签: html css wordpress woocommerce categories


【解决方案1】:

在您的主题 function.php 中添加以下代码以在您的产品类别分类中创建自定义字段。

// Add term page
function custom_product_taxonomy_add_new_meta_field() {
    // this will add the custom meta field to the add new term page
    ?>
    <div class="form-field">
        <label for="term_meta[custom_term_meta]"><?php _e( 'Postalcode', 'my-text-domain' ); ?></label>
        <input type="text" name="term_meta[custom_term_meta]" id="term_meta[custom_term_meta]" value="">
        <p class="description"><?php _e( 'Enter a value for this field','my-text-domain' ); ?></p>
    </div>
<?php
}
add_action( 'product_cat_add_form_fields', 'custom_product_taxonomy_add_new_meta_field', 10, 2 );
// Edit term page
function custom_product_taxonomy_edit_meta_field($term) {

    // put the term ID into a variable
    $t_id = $term->term_id;

    // retrieve the existing value(s) for this meta field. This returns an array
    $term_meta = get_option( "taxonomy_$t_id" ); ?>
    <tr class="form-field">
    <th scope="row" valign="top"><label for="term_meta[custom_term_meta]"><?php _e( 'Postalcode', 'my-text-domain' ); ?></label></th>
        <td>
            <input type="text" name="term_meta[custom_term_meta]" id="term_meta[custom_term_meta]" value="<?php echo esc_attr( $term_meta['custom_term_meta'] ) ? esc_attr( $term_meta['custom_term_meta'] ) : ''; ?>">
            <p class="description"><?php _e( 'Enter a value for this field','my-text-domain' ); ?></p>
        </td>
    </tr>
<?php
}
add_action( 'product_cat_edit_form_fields', 'custom_product_taxonomy_edit_meta_field', 10, 2 );
// Save extra taxonomy fields callback function.
function save_taxonomy_custom_meta( $term_id ) {
    if ( isset( $_POST['term_meta'] ) ) {
        $t_id = $term_id;
        $term_meta = get_option( "taxonomy_$t_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_$t_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 );

您可以将此自定义字段与类别一起保存。您可以使用此自定义字段进行过滤。

【讨论】:

  • 我不知道发生了什么,但这些建议都不起作用。
  • 您可以在 woocommerce 产品类别分类中使用此代码创建自定义字段(邮政编码)。
  • 通过复制粘贴到我的functions.php文件?
  • 当然。但是不幸的是仍然没有运气:(如果我使用的是本地主机,这确实有效吗?
  • 我刚刚在我的本地主机中应用了这段代码,这段代码在类别分类法中创建了邮政编码字段。这是获取更多参考的屏幕截图。 screencast.com/t/irMBLoVoXNPa
猜你喜欢
  • 2015-10-29
  • 1970-01-01
  • 2022-01-25
  • 1970-01-01
  • 1970-01-01
  • 2014-06-21
  • 2019-07-29
  • 2017-03-27
  • 1970-01-01
相关资源
最近更新 更多