php - Insert javascript code into the head tags of the woocommerce thank you page - Stack Overflow

I'm trying to add some google tracking script to my thank you page. I've written this code wh

I'm trying to add some google tracking script to my thank you page. I've written this code which successfully injects the tracker into the of the thank you with dynamic values, but I need, instead, to add it within the tags.

function mv_google_conversion( $order_id ) {
    $order = new WC_Order( $order_id );
    $currency = $order->get_currency();
    $total = $order->get_total();
    ?>
    <script>
      gtag('event', 'conversion', {
          'send_to': 'AW-746876528/x5W1CLfA8JoBEPDckeQC',
          'value': <?php echo $total; ?>,
          'currency': '<?php echo $currency; ?>',
          'transaction_id': '<?php echo $order_id; ?>'
      });
    </script>
    <?php
  }
  add_action( 'woomerce_thankyou', 'mv_google_conversion' );

How would I be able to use this code, with the dynamic values in header.php, or is there a hook that targets the tags on the woomerce thank you page.

I'm trying to add some google tracking script to my thank you page. I've written this code which successfully injects the tracker into the of the thank you with dynamic values, but I need, instead, to add it within the tags.

function mv_google_conversion( $order_id ) {
    $order = new WC_Order( $order_id );
    $currency = $order->get_currency();
    $total = $order->get_total();
    ?>
    <script>
      gtag('event', 'conversion', {
          'send_to': 'AW-746876528/x5W1CLfA8JoBEPDckeQC',
          'value': <?php echo $total; ?>,
          'currency': '<?php echo $currency; ?>',
          'transaction_id': '<?php echo $order_id; ?>'
      });
    </script>
    <?php
  }
  add_action( 'woomerce_thankyou', 'mv_google_conversion' );

How would I be able to use this code, with the dynamic values in header.php, or is there a hook that targets the tags on the woomerce thank you page.

Share Improve this question edited May 13, 2019 at 11:51 LoicTheAztec 255k24 gold badges399 silver badges446 bronze badges asked May 13, 2019 at 11:31 jermainecraigjermainecraig 3115 silver badges20 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 14

You will use the following to inject code on the head tags on "Order received" (thankyou) page:

add_action( 'wp_head', 'my_google_conversion' );
function my_google_conversion(){
    // On Order received endpoint only
    if( is_wc_endpoint_url( 'order-received' ) ) :

    $order_id = absint( get_query_var('order-received') ); // Get order ID

    if( get_post_type( $order_id ) !== 'shop_order' ) return; // Exit

    $order = wc_get_order( $order_id ); // Get the WC_Order Object instance
    ?>
    <script>
      gtag('event', 'conversion', {
          'send_to': 'AW-746876528/x5W1CLfA8JoBEPDckeQC',
          'value': <?php echo $order->get_total(); ?>,
          'currency': '<?php echo $order->get_currency(); ?>',
          'transaction_id': '<?php echo $order_id; ?>'
      });
    </script>
    <?php   
    endif;
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743732325a4497650.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信