• Hey!

    On our wordpress-installation, we are using a custom orderId (with prefix) that breaks the custom javascript code located in “duracelltomi-google-tag-manager/integration/woocommerce.php” starting from line 678.

    That has been added to functions.php custom code to change orderid from (int) type “12345” to a mixed datatype “OnlineShop-12345”:

    // WOOCOMMERCE: ORDER NUMBER PRE- AND SUFFIX
    add_filter('woocommerce_order_number', 'change_woocommerce_order_number', 1, 2);
    function change_woocommerce_order_number($order_id, $order) {
    $prefix = 'Onlineshop-';
    $suffix = '';
    return $prefix . $order->get_id() . $suffix;
    }

    Browser console is showing “Uncaught ReferenceError: Onlineshop is not defined” … Here is the code rendered in frontend from GTM4WP:

    <script type="text/javascript" id="gtm4wp-additional-datalayer-pushes-js-after">
      /* <![CDATA[ */
      // Check whether this order has been already tracked in this browser.
    
      // Read order id already tracked from cookies or local storage.
      let gtm4wp_orderid_tracked = "";
    
      if ( !window.localStorage ) {
        let gtm4wp_cookie = "; " + document.cookie;
        let gtm4wp_cookie_parts = gtm4wp_cookie.split( "; gtm4wp_orderid_tracked=" );
        if ( gtm4wp_cookie_parts.length == 2 ) {
          gtm4wp_orderid_tracked = gtm4wp_cookie_parts.pop().split(";").shift();
        }
      } else {
        gtm4wp_orderid_tracked = window.localStorage.getItem( "gtm4wp_orderid_tracked" );
      }
    
      // Check whether this order has been already tracked before in this browser.
      let gtm4wp_order_already_tracked = false;
      if ( gtm4wp_orderid_tracked && ( Onlineshop-214355 == gtm4wp_orderid_tracked ) ) {
        gtm4wp_order_already_tracked = true;
      }
    
      // only push purchase action if not tracked already.
      if ( !gtm4wp_order_already_tracked ) {
        dataLayer.push({"event":"purchase","ecommerce":{"currency":"EUR","transaction_id":"Onlineshop-214355","affiliation":"","value":140.42,"tax":22.42,"shipping":0,"coupon":"","items":[{"item_id":"U3818","item_name":"Plom 113","sku":"113s","price":118,"stocklevel":null,"stockstatus":"instock","google_business_vertical":"retail","item_category":"Zubehoer","id":"U3818","item_brand":"Unbekannt","quantity":1}]}});
      }
    
      // Store order ID to prevent tracking this purchase again.
      if ( !window.localStorage ) {
        var gtm4wp_orderid_cookie_expire = new Date();
        gtm4wp_orderid_cookie_expire.setTime( gtm4wp_orderid_cookie_expire.getTime() + (365*24*60*60*1000) );
        var gtm4wp_orderid_cookie_expires_part = "expires=" + gtm4wp_orderid_cookie_expire.toUTCString();
        document.cookie = "gtm4wp_orderid_tracked=" + Onlineshop-214355 + ";" + gtm4wp_orderid_cookie_expires_part + ";path=/";
      } else {
        window.localStorage.setItem( "gtm4wp_orderid_tracked", Onlineshop-214355 );
      }
      /* ]]> */
    </script>

    Fix would be, adding the orderid to quotes?

    Thanks for helping with this issue!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Same issue here, we have also custom ids. Please fix asap

    Same issue.

    Comment in code:

    "https:// Store order ID to prevent tracking this purchase again."

    So while WooCommerce Order IDs are indeed numeric (order id = post id), your code is not using the Order ID. Your plugin is using the Order Number, which cannot be assumed to be numeric.

    Here is a band-aid until the author fixes the issue. Goes in functions.php. Test before you deploy. I am not responsible if it doesn’t work in your environment.

    add_filter('js_escape', 'fix_gtm4wp_js_bug', 10, 2);
    function fix_gtm4wp_js_bug ($safe_text, $text) {
    	global $wp;
    
    	$order_id = filter_var( wp_unslash( isset( $_GET['order'] ) ? $_GET['order'] : '' ), FILTER_VALIDATE_INT );
    
    	if ( ! $order_id & isset( $wp->query_vars['order-received'] ) ) {
    		$order_id = $wp->query_vars['order-received'];
    	}
    
    	$order_id = absint( $order_id );
    
    	if ( $order_id > 0 ) {
    		$order = wc_get_order( $order_id );
    
    		if($order->get_order_number() == $text) {
    			$safe_text = str_pad($safe_text, strlen($safe_text) + 2, "'", STR_PAD_BOTH);
    		}
    
    	}
    
    	return $safe_text;
    
    }
    • This reply was modified 8 months, 3 weeks ago by secureit.
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Javascript Error in JS-Code for preventing duplicate tracking’ is closed to new replies.