为WooCommerce产品属性术语添加自定义字段的实现问题
WooCommerce属性术语自定义字段实现方案
你已经完成了产品属性端的字段添加与保存逻辑,下面补充术语端的显示与保存代码,实现当属性标记为"yes"时,在对应属性的术语编辑页显示自定义字段的需求:
完整代码(含已有属性端代码+新增术语端逻辑)
// -------------------------- // 属性端:添加并保存"Product addon"下拉字段 // -------------------------- function my_edit_wc_attribute_pr_addon_field() { $id = isset( $_GET['edit'] ) ? absint( $_GET['edit'] ) : 0; $value = $id ? get_option( "wc_attribute_pr_addon_field-$id" ) : ''; ?> <tr class="form-field"> <th scope="row" valign="top"> <label for="my-pr_addon_field">Product addon</label> </th> <td> <select name="pr_addon_field" id="pr_addon_field"> <option value="no" <?php selected( $value, 'no' ); ?>>No</option> <option value="yes" <?php selected( $value, 'yes' ); ?>>Yes</option> </select> </td> </tr> <?php } add_action( 'woocommerce_after_add_attribute_fields', 'my_edit_wc_attribute_pr_addon_field' ); add_action( 'woocommerce_after_edit_attribute_fields', 'my_edit_wc_attribute_pr_addon_field' ); function my_save_wc_attribute_pr_addon_field( $id ) { if ( is_admin() && isset( $_POST['pr_addon_field'] ) ) { $option = "wc_attribute_pr_addon_field-$id"; update_option( $option, sanitize_text_field( $_POST['pr_addon_field'] ) ); } } add_action( 'woocommerce_attribute_added', 'my_save_wc_attribute_pr_addon_field' ); add_action( 'woocommerce_attribute_updated', 'my_save_wc_attribute_pr_addon_field' ); add_action( 'woocommerce_attribute_deleted', function ( $id ) { delete_option( "wc_attribute_pr_addon_field-$id" ); } ); // -------------------------- // 术语端:根据属性设置显示并保存自定义字段 // -------------------------- function my_add_term_pr_addon_field( $taxonomy ) { // 仅针对WooCommerce产品属性(以pa_开头的分类) if ( strpos( $taxonomy, 'pa_' ) !== 0 ) { return; } // 获取当前属性的ID(从分类名中提取) $attribute_id = wc_attribute_taxonomy_id_by_name( str_replace( 'pa_', '', $taxonomy ) ); if ( ! $attribute_id ) { return; } // 检查该属性是否开启了"Product addon" $addon_enabled = get_option( "wc_attribute_pr_addon_field-$attribute_id" ); if ( $addon_enabled !== 'yes' ) { return; } // 获取当前术语的字段值 $term_id = isset( $_GET['tag_ID'] ) ? absint( $_GET['tag_ID'] ) : 0; $field_value = $term_id ? get_term_meta( $term_id, 'pr_term_addon_field', true ) : ''; ?> <tr class="form-field"> <th scope="row" valign="top"> <label for="pr_term_addon_field">术语附加字段</label> <p class="description">这里可以添加字段说明文字</p> </th> <td> <input type="text" name="pr_term_addon_field" id="pr_term_addon_field" value="<?php echo esc_attr( $field_value ); ?>" /> </td> </tr> <?php } add_action( 'woocommerce_after_edit_term_fields', 'my_add_term_pr_addon_field', 10, 1 ); function my_save_term_pr_addon_field( $term_id, $tt_id, $taxonomy ) { // 仅针对WooCommerce产品属性 if ( strpos( $taxonomy, 'pa_' ) !== 0 ) { return; } // 验证当前属性是否开启了addon $attribute_id = wc_attribute_taxonomy_id_by_name( str_replace( 'pa_', '', $taxonomy ) ); if ( ! $attribute_id || get_option( "wc_attribute_pr_addon_field-$attribute_id" ) !== 'yes' ) { return; } // 保存字段值 if ( isset( $_POST['pr_term_addon_field'] ) ) { update_term_meta( $term_id, 'pr_term_addon_field', sanitize_text_field( $_POST['pr_term_addon_field'] ) ); } else { delete_term_meta( $term_id, 'pr_term_addon_field' ); } } add_action( 'woocommerce_edit_term', 'my_save_term_pr_addon_field', 10, 3 );
代码说明
- 术语端字段显示控制:通过
wc_attribute_taxonomy_id_by_name获取当前术语所属的属性ID,再读取之前保存的属性选项值,仅当值为yes时才渲染自定义字段。 - 字段类型自定义:示例中用了文本输入框,你可以根据需求改成下拉框、复选框等,修改
<input>标签即可。 - 数据存储:使用
term_meta存储术语的字段值,符合WordPress/WooCommerce的规范,避免数据混乱。
内容的提问来源于stack exchange,提问作者Bent
相关产品推荐
相关产品推荐

