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

如何让Google Ads转化跟踪动态读取WooCommerce订单转化价值?

WooCommerce Google Ads 转化价值动态配置修改方案

你只需要将现有functions.php里的跟踪函数按照以下逻辑修改,即可动态读取订单实际金额:

核心修改逻辑

  • 提前从订单对象中提取订单总金额、货币类型、订单ID三个字段
  • 将三个字段的值输出到gtag转化事件的对应参数位置,替换原来的固定值
  • 新增的transaction_id参数可避免用户刷新感谢页导致的重复转化统计

修改后完整代码

<?php
// Add custom Theme Functions here
/**
 * Add custom tracking code to the thank-you page
 */
add_action( 'woocommerce_thankyou', 'my_custom_tracking' );

function my_custom_tracking( $order_id ) {
    // 获取订单对象
    $order = wc_get_order( $order_id );
    // 提前取出订单相关字段
    $order_total = $order->get_total();
    $order_currency = $order->get_currency();
    $order_transaction_id = $order->get_id();
    ?>
    <script>
    fbq('track', 'Purchase');
        
    gtag('event', 'conversion', {
        'send_to': 'AW-608570056/jkryCPDw7_ACEMiVmKIC',
        'value': <?php echo (float)$order_total; ?>,
        'currency': '<?php echo esc_js($order_currency); ?>',
        'transaction_id': '<?php echo esc_js($order_transaction_id); ?>'
    });
    </script>
    <?php
    // 以下原有的商品循环逻辑可根据自身需求保留或删除,不影响转化金额取值
    $line_items = $order->get_items();
    foreach ( $line_items as $item ) {
        $product = $order->get_product_from_item( $item );
        $sku = $product->get_sku();
        $qty = $item['qty'];
        $total = $order->get_line_total( $item, true, true );
        $subtotal = $order->get_line_subtotal( $item, true, true );
    }
}

可选调整

如果你的店铺仅使用美元结算,不需要适配多货币,可以把currency参数改回固定值'USD'即可。

内容的提问来源于stack exchange,提问作者Shaker Abady

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.27 02:36:05