You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在WooCommerce后台产品页添加自定义产品字段?

WooCommerce后台产品定价区域添加自定义字段的钩子方案

一、找到精准钩子的方法

要在常规价格字段后添加仅后台可见的自定义字段,直接查看WooCommerce核心代码是最直接的方式:

  • 打开wp-content/plugins/woocommerce/includes/admin/meta-boxes/class-wc-meta-box-product-data.php文件
  • 定位到pricing选项卡的代码段,里面有个woocommerce_product_options_pricing动作钩子——这是专门给定价区域(包含常规价格、销售价格等字段)添加自定义内容的钩子,刚好能满足你放在常规价格字段后的需求。

二、完整实现代码

把下面的代码加到主题functions.php或者自定义插件里即可,字段仅在后台可见,前端不会展示:

// 在定价区域添加自定义字段
add_action( 'woocommerce_product_options_pricing', 'add_internal_reference_price_field' );
function add_internal_reference_price_field() {
    woocommerce_wp_text_input( array(
        'id' => '_internal_ref_price',
        'label' => '内部参考价格',
        'placeholder' => '填写内部参考价',
        'desc_tip' => true,
        'description' => '该字段仅后台可见,前端不展示',
        'class' => 'short',
    ) );
}

// 保存自定义字段值
add_action( 'woocommerce_process_product_meta', 'save_internal_ref_price_field' );
function save_internal_ref_price_field( $post_id ) {
    $ref_price = isset( $_POST['_internal_ref_price'] ) ? sanitize_text_field( $_POST['_internal_ref_price'] ) : '';
    update_post_meta( $post_id, '_internal_ref_price', $ref_price );
}

三、通用钩子查找技巧

  • 核心文件溯源:WooCommerce后台产品编辑的所有 metabox 逻辑都在includes/admin/meta-boxes/目录下,对应功能的钩子基本都能在这里找到
  • 钩子测试验证:如果拿不准钩子位置,可以给目标区域的几个候选钩子(比如woocommerce_after_product_settings)加个测试输出,看哪个位置最贴合需求
  • 开发者工具辅助:在后台右键检查常规价格字段的HTML结构,找到对应的容器类名,再去核心代码里搜索这个类名,快速定位到钩子触发点

内容的提问来源于stack exchange,提问作者Joppe Meijers

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 05:30:47