-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkout_dataLayer.txt
47 lines (42 loc) · 2.14 KB
/
checkout_dataLayer.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Add this into the file functions.php of your (child-)theme
// Do not do this if you already use the plugin 'Google Tag manager for WordPress' by DuracellTomi simultaneously with integration of 'woocommerce' and active enhanced ecommerce tracking.
add_action( 'woocommerce_thankyou', 'checkout_dataLayer', 10, 1 );
function checkout_dataLayer( $order_id ) { ?>
// Lets grab the order
$order = wc_get_order( $order_id );
<script>
dataLayer.push({
'ecommerce': {
'currencyCode': '<?php echo $order->get_order_currency(); ?>',
'purchase': {
'actionField':{
'id': '<?php echo $order->get_order_number(); ?>',
'affiliation': 'Website',
'revenue': <?php echo number_format($order->get_total(), 2, ".", ""); ?>,
'shipping': <?php echo number_format($order->calculate_shipping(), 2, ".", ""); ?>,
<?php if($order->get_used_coupons()): ?>
'coupon': '<?php echo implode("-", $order->get_used_coupons()); ?>'
<?php endif; ?>
},
'products': [
<?php
foreach($order->get_items() as $key => $item):
$product = $order->get_product_from_item( $item );
$variant_name = ($item['variation_id']) ? wc_get_product($item['variation_id']) : '';
?>
{
'name': '<?php echo $item['name']; ?>',
'id': '<?php echo $item['product_id']; ?>',
'price': '<?php echo number_format($order->get_line_subtotal($item), 2, ".", ""); ?>',
'brand': '',
'category': <?php echo strip_tags($product->get_categories(', ', '', ''); ?>',
'variant': <?php echo ($variant_name) ? implode("-", $variant_name->get_variant_attributes()); ?>',
'quantity': <?php echo $item['qty']; ?>
},
<?php endforeach; ?>
]
}
}
});
</script>
<?php }