WooCommerce Conversion Tracking Script for two Pixel

99封情书 提交于 2019-12-02 09:58:55
Raunak Gupta

You can get the list of product ID(s) by Order ID, and then you can apply your conditioning and trigger different pixel.

Here is a sample code which should solve your query:

add_action('woocommerce_thankyou', 'wh_custom_tracking');

function wh_custom_tracking($order_id)
{
    $product_ids = [2004, 2000]; //<-- list of product_id(s) for which 2nd pixels should fire
    $checkSecond = FALSE;
    $order = wc_get_order($order_id);
    $total = $order->get_subtotal();
    $id = str_replace('#', '', $order->get_order_number());

    $items = $order->get_items();

    foreach ($items as $item)
    {
        $item_id = $item['product_id']; // <= Here is your product ID
        if (in_array($item_id, $product_ids))
        {
            $checkSecond = TRUE;
            break;
        }
    }

    if ($checkSecond)
    {
        //add your 2nd pixel here 2nd pixel
    }
    else
    {
        echo '<iframe src="https://network.com/track?offer_id=666&amount=' . $total . '&track_id=' . $id . '" scrolling="no" frameborder="0" width="1" height="1"></iframe>';
    }
}

Code goes in functions.php file of your active child theme (or theme). Or also in any plugin php files.

Related Question: How to insert Google Merchant Review JS code in WooCommerce Order Complete page

Hope this helps!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!