如何在管理单个产品编辑页面中获取产品类别自定义元字段的值?

evavienna

使用以下代码,我向产品类别添加了自定义元字段

    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( 'Short form', 'woocommerce' ); ?></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','woocommerce' ); ?></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( 'Short form', 'woocommerce' ); ?></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','woocommerce' ); ?></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 );

如果父类别在产品编辑页面中具有ID 33,那么如何将所有类别的自定义元值和类别名称填充到以下示例中的选择选项下拉列表中,这是什么想法吗?我只想念自定义元值部分-似乎类别名称工作正常。

    <?php // Populate Dropdown with Brand from Parent Category with ID 33 ?>
    <?php
        $args = array(
            'orderby' => 'name',
            'order' => 'ASC',
            'hide_empty' => 0,
            'taxonomy' => 'product_cat',
            'parent' => '33'
        );

        $categories = get_categories($args);

        foreach($categories as $cat) {                      

        ?>
        <option value="<?php echo $cat->name; ?>" ><?php echo $cat->CUSTOM-META???; ?></option>
        <?php

        }
    ?>  
埃尔文·哈奇(Elvin Haci)

要获取给定类别的自定义元值,可以使用get_term_meta函数。

<option value="<?php echo $cat->name; ?>" >
<?php echo get_term_meta($cat->term_id,'custom_term_meta',true); ?>
</option>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在单个产品页面上的价格后添加自定义元字段值

从Woocommerce档案库中的自定义显示按钮中排除产品类别

基于Woocommerce中产品类别的条件自定义结帐字段

在Woocommerce单一产品页面中获取产品类别名称和描述

在Woocommerce中为特定产品类别添加自定义按钮

通过主题自定义Woocommerce中的产品类别面包屑链接

从Woocommerce中的子产品类别ID获取父产品类别ID

产品类别的WooCommerce产品价格中的自定义小数

从产品类别和自定义meta_key对Woocommerce产品进行排序

获取Woocommerce中特定产品类别的特定产品属性值

如何在Java HashMap中基于产品类别获取产品详细信息?

Woocommerce 3中按产品类别的自定义购物车项目计数

在Woocommerce中显示基于自定义文本的产品类别订单商品名称

根据Woocommerce中的运输区域和产品类别显示自定义消息

根据Woocommerce中的产品类别添加自定义结帐字段

在自定义家庭和产品类别档案中显示WooCommerce产品属性

在Woocommerce商店和产品类别页面上显示高级自定义字段

根据WooCommerce结帐页面上的产品类别应用自定义数量参数

如何在woocomerce产品类别部分中添加编辑器

产品类别内的“高级自定义字段中继器”(woocommerce)

在 WooCommerce 中输出特定产品类别的自定义短代码

获取 WooCommerce 中顶级产品类别的自定义数组

WooCommerce 中特定产品类别的自定义条款和条件复选框

如何从产品类别页面中删除价格?

如何在插件文件中获取 Woocommerce 产品类别

如何在“继续结帐”按钮上添加自定义 URL,特定产品类别 [WooCommerce]

如何在wordpress自定义页面上获取产品类别的所见即所得自定义字段(ACF)的值

在 WooCommerce 循环产品标题后显示来自产品类别的自定义字段

根据 WooCommerce 电子邮件通知中的产品类别添加自定义文本“每个项目”