【问题标题】:WooCommerce Thankyou tracking code installation placementWooCommerceThankyou 跟踪代码安装位置
【发布时间】:2017-11-21 23:23:12
【问题描述】:

我是 Wordpress 开发的新手,我正在尝试在 thankyou.php 页面上安装流量垃圾跟踪代码。

到目前为止,我的尝试失败了,我认为归结为对 PHP 的理解不够好。

trafficjunky 提供的两个跟踪代码是 HTML 或 PHP

HTML:

<img id="1000143661_tester" src="https://ads.trafficjunky.net/tj_ads_pt?a=1000143661&member_id=1000734841&cb=[RANDOM_NUMBER]&cti=[TRANSACTION_UNIQ_ID]&ctv=[VALUE_OF_THE_TRANSACTION]&ctd=[TRANSACTION_DESCRIPTION]" width="1" height="1" border="0" />

PHP:

 <?php
    $randomNumber = time() . mt_rand(1000, 9999999);
    $currentPage = substr($_SERVER["REQUEST_URI"], 0, 255);
?>
<img id="1000143661_tester" src="https://ads.trafficjunky.net/tj_ads_pt?a=1000143661&member_id=1000734841&cb=<?=$randomNumber ?>&epu=<?=$currentPage ?>&cti=[TRANSACTION_UNIQ_ID]&ctv=[VALUE_OF_THE_TRANSACTION]&ctd=[TRANSACTION_DESCRIPTION]" width="1" height="1" border="0" />

我尝试在thankyou.php WooCommerce 模板上的一些 PHP 代码下的订单处理后安装 HTML 版本:

**<?php else : ?>

            <p class="woocommerce-notice woocommerce-notice--success woocommerce-thankyou-order-received"><?php echo apply_filters( 'woocommerce_thankyou_order_received_text', __( 'Thank you. Your order has been received.', 'woocommerce' ), $order ); ?></p>

            <ul class="woocommerce-order-overview woocommerce-thankyou-order-details order_details">

                <li class="woocommerce-order-overview__order order">
                    <?php _e( 'Order number:', 'woocommerce' ); ?>
                    <strong><?php echo $order->get_order_number(); ?></strong>
                </li>

                <li class="woocommerce-order-overview__date date">
                    <?php _e( 'Date:', 'woocommerce' ); ?>
                    <strong><?php echo wc_format_datetime( $order->get_date_created() ); ?></strong>
                </li>

                <?php if ( is_user_logged_in() && $order->get_user_id() === get_current_user_id() && $order->get_billing_email() ) : ?>
                    <li class="woocommerce-order-overview__email email">
                        <?php _e( 'Email:', 'woocommerce' ); ?>
                        <strong><?php echo $order->get_billing_email(); ?></strong>
                    </li>
                <?php endif; ?>

                <li class="woocommerce-order-overview__total total">
                    <?php _e( 'Total:', 'woocommerce' ); ?>
                    <strong><?php echo $order->get_formatted_order_total(); ?></strong>
                </li>

                <?php if ( $order->get_payment_method_title() ) : ?>
                    <li class="woocommerce-order-overview__payment-method method">
                        <?php _e( 'Payment method:', 'woocommerce' ); ?>
                        <strong><?php echo wp_kses_post( $order->get_payment_method_title() ); ?></strong>
                    </li>
                <?php endif; ?>

            </ul>
            <img id="1000143661_tester" src="https://ads.trafficjunky.net/tj_ads_pt?a=1000143661&member_id=1000734841&cb=[RANDOM_NUMBER]&cti=[TRANSACTION_UNIQ_ID]&ctv=[VALUE_OF_THE_TRANSACTION]&ctd=[TRANSACTION_DESCRIPTION]" width="1" height="1" border="0" />
        <?php endif; ?>

        <?php do_action( 'woocommerce_thankyou_' . $order->get_payment_method(), $order->get_id() ); ?>
        <?php do_action( 'woocommerce_thankyou', $order->get_id() ); ?>

    <?php else : ?>

        <p class="woocommerce-notice woocommerce-notice--success woocommerce-thankyou-order-received"><?php echo apply_filters( 'woocommerce_thankyou_order_received_text', __( 'Thank you. Your order has been received.', 'woocommerce' ), null ); ?></p>

    <?php endif; ?>

</div>**

这导致了订单错误,所以我试图弄清楚如何安全地添加跟踪代码的 PHP 版本而不会引发错误。

任何帮助将不胜感激。

感谢您的回复,我已按照说明在 functions.php 中设置了函数,但我仍然无法触发跟踪代码。这是我当前的代码:

add_action( 'woocommerce_thankyou', 'tracking_code_thankyou', 10, 1 );

function tracking_code_thankyou($order_id){

$random_number = time() . mt_rand(1000, 9999999);
$current_page = substr($_SERVER["REQUEST_URI"], 0, 255);
?>
<em>Your tracking code just below (for testing)</em>
<img id="1000145711_cpa_testing" src="https://ads.trafficjunky.net/tj_ads_pt?a=1000145711&member_id=1000785411&cb=<?=$randomNumber ?>&epu=<?=$currentPage ?>&cti=[TRANSACTION_UNIQ_ID]&ctv=[VALUE_OF_THE_TRANSACTION]&ctd=[TRANSACTION_DESCRIPTION]" width="1" height="1" border="0" />
<?php

} }

【问题讨论】:

标签: php wordpress woocommerce tracking orders


【解决方案1】:

您可以尝试在 header.php(head 标签末尾之前)或 footer.php(body 标签末尾之前)中添加 php 跟踪代码。如果您希望它仅在该页面上运行,则需要获取该页面的 id。

做这样的事情

if(is_page(yourpageidhere)):
//paste the tracking code here
endif;

【讨论】:

    【解决方案2】:

    更新

    您可以看到它的源代码,而不是覆盖thankyou.php 模板:

    <?php do_action( 'woocommerce_thankyou', $order->get_id() ); ?>
    

    这意味着你可以使用这个 woocommerce_thankyou 动作钩子和一个自定义函数,在这个模板行中输出任何东西。首先清理你的代码模板...

    我还更正了图片链接中的代码,因为它不正确。您应该在活动子主题或活动主题的function.php 文件中使用以下代码:

    add_action( 'woocommerce_thankyou', 'tracking_code_thankyou', 10, 1 );
    function tracking_code_thankyou( $order_id ) {
    
        $random_number = time() . mt_rand(1000, 9999999);
        $current_page = substr($_SERVER["REQUEST_URI"], 0, 255);
        $url = "https://ads.trafficjunky.net/tj_ads_pt?a=1000145711&member_id=1000785411&cb=$randomNumber&epu=$currentPage&cti=[TRANSACTION_UNIQ_ID]&ctv=[VALUE_OF_THE_TRANSACTION]&ctd=[TRANSACTION_DESCRIPTION]";
    
        echo '<em>Your tracking code just below (for testing)</em>
        <img id="1000143661_tester" src="'.$url.'" width="1" height="1" border="0" />';
    }
    

    它会在收到订单页面末尾的客户详细信息之后输出您的代码……

    【讨论】:

    • 抱歉在格式化时遇到了一些问题,我将更新后的代码放在原始帖子中。我已经测试了该功能并且它运行成功,但是我在让 Traffic Junky 跟踪现在启动时遇到了问题。关于我在这里做错了什么有什么建议吗?
    • @user2415458 我无法为您处理“Traffic Junky tracking to fire”。因此,当您说“并且工作成功”时,请您将此答案标记为已接受...谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-09
    • 2013-10-29
    • 2011-04-05
    相关资源
    最近更新 更多