WooCommerce Thankyou tracking code installation placement

我只是一个虾纸丫 提交于 2020-01-01 19:33:23

问题


I am new to Wordpress development and am trying to install a traffic junky tracking code on the thankyou.php page.

My attempt has failed so far and I think it boils down to not understanding PHP well enough.

The two tracking codes from trafficjunky that were available were HTML or 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" />

I tried to install the HTML version after the order processes under some PHP code on the thankyou.php WooCommerce template:

**<?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>**

This led to order errors so i am trying to figure out how to add the PHP version of the tracking code safely without throwing errors.

Any help would be appreciated.

Thank you for the replies, I have set up the function in functions.php as instructed but I am still having trouble getting the tracking code to fire. Here is my current code:

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

} }


回答1:


You can try adding the php tracking code inside the header.php (before the end of the head tag) or footer.php (before the end of the body tag). You need to get the id of the page if you want it to run exclusively on that page only.

Do something like this

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



回答2:


UPDATED

Instead of overriding the thankyou.php template as you can see its source code there is this line:

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

Which means that you can use this woocommerce_thankyou action hook with a custom function, to output anything in this template line. First get the template clean of your code...

Also I have corrected your code in the image link, as it was not correct. You should use the following code in the function.php file of your active child theme or active theme:

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" />';
}

It will output your code just after customers details at the end of the order-received page…



来源:https://stackoverflow.com/questions/47424391/woocommerce-thankyou-tracking-code-installation-placement

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