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

Avantlink订单确认代码与Paid Membership Pro集成问题咨询

I’ve helped many users get this integration working, so let’s break down exactly how to replace those PMPro variables in your Avantlink code. The key here is that we’re mixing backend PHP variables (from PMPro) with frontend JavaScript (from Avantlink), so we need to output the JS from a PHP context where those variables are fully available.

Step 1: Use the Right PMPro Hook

The best place to add this code is on PMPro’s "Thank You" page (after a successful membership purchase), since that’s where the order details are fully loaded. We’ll use the pmpro_after_thankyou action hook, which fires right after the thank-you content displays—perfect for order tracking scripts.

Step 2: Replace Variables & Build the Full Code

Here’s the complete code snippet that maps your PMPro variables to Avantlink’s required placeholders. I’ve added comments to explain each mapping, plus safety checks to avoid errors if some data is missing:

add_action('pmpro_after_thankyou', 'avantlink_pmpro_tracking_code');
function avantlink_pmpro_tracking_code($order) {
    // Get the invoice linked to the completed order
    $pmpro_invoice = $order->getLastInvoice();
    
    // Exit early if no invoice exists (safety fallback)
    if (!$pmpro_invoice) {
        return;
    }

    // Sanitize variables to prevent JS errors or XSS risks
    $order_id = esc_js($pmpro_invoice->code); // PMPro's public-facing order ID
    $order_amount = esc_js($pmpro_invoice->total);
    $billing_state = esc_js(!empty($pmpro_invoice->billing->state) ? $pmpro_invoice->billing->state : '');
    $billing_country = esc_js(!empty($pmpro_invoice->billing->country) ? $pmpro_invoice->billing->country : '');
    $membership_id = esc_js($order->membership_id);
    $membership_sku = esc_js(get_post_meta($membership_id, '_pmpro_sku', true)); // Pull membership SKU if set

    // Output the Avantlink JS with replaced PMPro data
    ?>
    <script type="text/javascript">
        var _AvantMetrics = _AvantMetrics || [];
        _AvantMetrics.push(['order',{
            order_id:'<?php echo $order_id; ?>',
            amount:'<?php echo $order_amount; ?>',
            state:'<?php echo $billing_state; ?>',
            country:'<?php echo $billing_country; ?>'
        }]);
        _AvantMetrics.push(['item',{
            order_id:'<?php echo $order_id; ?>',
            parent_sku:'<?php echo $membership_sku ? $membership_sku : $membership_id; ?>', // Use SKU if available, else membership ID
            variant_sku:'<?php echo $membership_sku ? $membership_sku : $membership_id; ?>', // Adjust if you offer membership variants
            price:'<?php echo $order_amount; ?>',
            qty:'1' // Membership purchases are typically quantity 1
        }]);
    </script>
    <?php
}

Step 3: Add the Code to Your Site Safely

Never edit your parent Divi theme directly (updates will overwrite your changes). Use one of these safe methods:

  • Child Theme functions.php: Go to Appearance > Theme File Editor > Select your Divi Child Theme > Paste the code at the bottom of functions.php.
  • Code Snippets Plugin: Install the free Code Snippets plugin, create a new snippet, paste the code, and activate it.

Key Adjustments for Your Setup

  • Order ID: I used $pmpro_invoice->code because it’s the human-readable order ID PMPro shows users. If you need to use $pmpro_invoice->accountnumber instead, just swap that variable in.
  • Membership SKU: If your membership levels have a custom SKU set in PMPro (Memberships > Edit Membership > SKU field), the code will pull that. If not, it falls back to the membership ID, which works as a unique identifier for Avantlink.
  • Discount Codes: Your original list included $pmpro_invoice->discount_code->code, but Avantlink’s snippet doesn’t have a placeholder for discounts. If you need to track this, check Avantlink’s docs for an additional parameter to add to the order object.

Testing the Integration

Complete a test purchase (use PMPro’s test mode or a sandbox payment gateway), then view the page source of the thank-you page. You should see the Avantlink script with all placeholders replaced with actual order data (no [PLACEHOLDER] values left).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 08:15:42