简体   繁体   English

在 WooCommerce 的 HEAD 中包含一些 javascript 代码收到订单

[英]Include some javascript code in HEAD on WooCommerce Order received

Goal: I need to include an event snippet for specific tracking on the Woocommerce Order Confirmation page (the page immediately following a purchase).目标:我需要在 Woocommerce 订单确认页面(购买后的页面)上包含一个特定跟踪的事件片段。 I must pass order_id , currency and total to the tracker.我必须将order_idcurrencytotal传递给跟踪器。 Here is the code I've written:这是我写的代码:

function wh_CustomReadOrder($order_id)
{
    //getting order object
    $order = wc_get_order($order_id);
    echo  "<script> gtag('event', 'conversion', { 'send_to': 'AW-995109926/CWeECK-epPYBEKbYwNoD', 'value': '".$order->get_total()."', 'currency': '".$order->currency."', 'transaction_id': '".$order_id."' }); </script>";
}
add_action('woocommerce_thankyou', 'wh_CustomReadOrder');

This includes the code correctly on the page and passes the relevant info to the tracker.这包括页面上正确的代码并将相关信息传递给跟踪器。 But this code does not appear in the <head> of the page;但是这段代码并没有出现在页面的<head>中; it appears in the body.它出现在体内。 Per instructions this code must be placed in the <head> .根据说明,此代码必须放在<head>中。

I have not found a snippet plugin that allows for this kind of control.我还没有找到允许这种控制的片段插件。 Is there another way I can achieve this?还有另一种方法可以实现这一目标吗?

If I were to include this code on all pages in the <head> but wrap it in an if statement that checks for the url, /checkout/order-received/{order-id}/ and fires if true, is that possible or there another way?如果我要在<head>的所有页面上包含此代码,但将其包装在一个if语句中,该语句检查 url、 /checkout/order-received/{order-id}/并在 true 时触发,这是可能的还是还有其他方法吗?

You can use the following to inject your Javascript code in the head on "Order received" page:您可以使用以下内容将 Javascript 代码注入“已收到订单”页面的头部:

add_action( 'wp_head', 'wc_custom_redirect_after_purchase' );
function wc_custom_redirect_after_purchase() {
    if ( ! is_wc_endpoint_url('order-received') ) return;
    global $wp;

    // If order_id is defined
    if ( isset($wp->query_vars['order-received']) && absint($wp->query_vars['order-received']) > 0 ) :

    $order_id       = absint($wp->query_vars['order-received']); // The order ID
    $order          = wc_get_order($order_id ); // The WC_Order object
    
    $send_to        = 'AW-995109926/CWeECK-epPYBEKbYwNoD';
    $transaction_id = empty($order->get_transaction_id()) ? $order_id : $order->get_transaction_id(); // The transaction ID
    
    ?>
    <script type="text/javascript">
    gtag('event', 'conversion', {
        'send_to'       : '<?php echo $send_to; ?>', 
        'value'         : '<?php echo $order->get_total(); ?>', 
        'currency'      : '<?php echo $order->get_currency(); ?>', 
        'transaction_id': '<?php echo $transaction_id; ?>' 
    });
    </script>
    <?php
    endif;
}

Code goes in functions.php file of the active child theme (or active theme).代码进入活动子主题(或活动主题)的functions.php文件。 It should work.它应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM