L
L
Levelse2021-02-02 22:09:38
Google
Levelse, 2021-02-02 22:09:38

Get id for Google Tag Manager from Woocommerce?

Good afternoon.
There was a need to execute a script that will send data about a completed purchase to the CRM associated with Google Tag Manager, the script itself looks like this:

<script>
window.dataLayer = window.dataLayer || [];
dataLayer.push({
 'ecommerce': {
   'currencyCode': 'USD',
   'purchase': {
     'actionField': {
       'id': 'TransactionID',   
       'revenue': 120, 
     },
     'products': [{  
       'name': 'Product 1',
       'id': 'ID1',
       'price': '23.5',
       'sku': '6517',
       'quantity': 2
},{
       'name': 'Product 2',
       'id': 'ID3',
       'price': '23.5',
       'sku': '2117',
       'quantity': 1
     }]
   }
 },
 'event': 'purchaseEEC',
});
</script>


Actually the question is how to get the necessary data from Woocommerce? I tried to make a shortcode from the script on the example of the price, and indicate in it, for example
add_shortcode('twit', 'gb_twit');

function gb_twitro() {
  $product_id = get_the_ID();
  $product = wc_get_product( $product_id );
return "
 <script>
window.dataLayer = window.dataLayer || [];
dataLayer.push({
 'ecommerce': {
   'currencyCode': 'USD',
   'purchase': {
     'actionField': {
       'id': 'TransactionID',   
       'revenue': 120, 
     },
     'products': [{  
       'name': 'Product 1',
       'id': 'ID1',
       'price': '". $product->get_sale_price(); ."',
       'sku': '6517',
       'quantity': 2
},{
       'name': 'Product 2',
       'id': 'ID3',
       'price': '23.5',
       'sku': '2117',
       'quantity': 1
     }]
   }
 },
 'event': 'purchaseEEC',
});
</script>";
}
add_shortcode('twitro', 'gb_twitro');


but nothing works, which is not surprising...
tell me which way to dig?
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex, 2021-02-02
@Kozack

I think you need to call wc_get_order inside the woocommerce_order_payment_status_changed hook to get order details.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question