如何在Woocommerce中将自定义字段添加到类别中? [英] How do I add custom fields to the categories in Woocommerce?

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

问题描述

我有一个woocommerce网站,该网站将商品归类,并在初始商店页面上显示它们.

I have a woocommerce site that groups products within categories and displays them on the initial shop page.

我想将自定义字段添加到类别中.

I want to add custom fields to the categories.

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

Theory: On the home page is a search form, the user types in their postcode and the categories with the matching postcode details (which will be specified within the custom fields) are displayed.
I don't need the custom fields to show on the shop page, I just need to be able to apply the matching postcodes in the backend.

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

Please does anyone know how I can do this?

我已经研究了很久了,似乎找不到解决方案.
非常感谢您提前提出任何答案或建议.
附注:我不想使用插件,请仅使用php/html/javascript(及其他代码).

I've been working on it for ages and can't seem to find a solution.
A huge thank you any answers or suggestions in advance.
P.S: I don't want to use a plugin, just php/html/javascript (& other code) please.

推荐答案

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

Add below code in your themes function.php to create custom field on your product categories taxonomy.

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

您可以保存此自定义字段以及类别.您可以使用此自定义字段进行过滤.

you can save this custom field along with categories. you can used this custom field for your filtration.

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

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