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

WooCommerce结账自定义字段无法显示在Elementor定制感谢页

WooCommerce自定义字段在Elementor感谢页不显示的解决方法

方法1:使用Elementor动态标签直接调用

  • 打开Elementor编辑你的自定义感谢页,定位到要展示自定义字段的位置
  • 添加「文本」或「HTML」模块,点击模块内容栏的动态标签按钮(小闪电图标)
  • 选择「WooCommerce > 订单元数据」,在设置弹窗中输入自定义字段的Meta Key(即你保存字段时用的键名,比如my_custom_checkout_field)
  • 可设置字段为空时显示的默认文本,保存页面后测试下单查看效果

方法2:自定义短代码调用

如果动态标签无法满足需求,可通过短代码实现:

  1. 打开子主题的functions.php文件,添加以下代码(替换your_meta_key和字段名称为实际内容):
function display_custom_checkout_field_thankyou() {
    global $wp;
    // 获取感谢页的订单ID
    if ( ! isset( $wp->query_vars['order-received'] ) ) return '';
    
    $order_id = absint( $wp->query_vars['order-received'] );
    $order = wc_get_order( $order_id );
    if ( ! $order ) return '';
    
    // 获取自定义字段值
    $custom_field_value = $order->get_meta( 'your_meta_key', true );
    if ( $custom_field_value ) {
        return '<p><strong>自定义字段名称:</strong>' . esc_html( $custom_field_value ) . '</p>';
    }
    return '';
}
add_shortcode( 'show_custom_thankyou_field', 'display_custom_checkout_field_thankyou' );
  1. 返回Elementor编辑页面,添加「短代码」模块,输入短代码[show_custom_thankyou_field],保存后测试

方法3:修复钩子触发问题

如果之前使用钩子未成功,大概率是自定义感谢页未触发默认WooCommerce钩子,可按以下步骤修复:

  1. 在子主题functions.php中保留你的钩子函数(示例):
add_action( 'woocommerce_thankyou', 'render_my_custom_fields', 10, 1 );
function render_my_custom_fields( $order_id ) {
    $order = wc_get_order( $order_id );
    // 依次获取多个自定义字段
    $field1 = $order->get_meta( 'custom_field_1', true );
    $field2 = $order->get_meta( 'custom_field_2', true );
    
    if ( $field1 ) echo '<p><strong>字段1:</strong>' . esc_html( $field1 ) . '</p>';
    if ( $field2 ) echo '<p><strong>字段2:</strong>' . esc_html( $field2 ) . '</p>';
}
  1. 在Elementor感谢页中添加「自定义HTML」模块,输入以下代码手动触发钩子:
<?php
global $wp;
if ( isset( $wp->query_vars['order-received'] ) ) {
    $order_id = absint( $wp->query_vars['order-received'] );
    do_action( 'woocommerce_thankyou', $order_id );
}
?>
  1. 注意:Elementor默认不允许在HTML模块执行PHP,需安装激活「Elementor Custom Code」插件,或使用支持PHP执行的Elementor扩展模块

内容的提问来源于stack exchange,提问作者Lucia Guarino

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 06:01:00