Skip to content

Instantly share code, notes, and snippets.

@StefsterNYC
Last active October 25, 2019 19:28
Show Gist options
  • Save StefsterNYC/f4441fb3ff86b4baaab76faf0d434950 to your computer and use it in GitHub Desktop.
Save StefsterNYC/f4441fb3ff86b4baaab76faf0d434950 to your computer and use it in GitHub Desktop.
Specific call for Dynamic Values within WooCommerce
<script>
var dataLayer = window.dataLayer || [];
dataLayer.push({
'event':'Transaction',
'ecommerce': {
'purchase': {
'actionField': {
'id': '<?php echo $order->get_order_number(); ?>', //Required for purchases and refunds.
'revenue': '<?php echo number_format($order->get_subtotal(), 2, ".", ""); ?>', // Total transaction value (incl. tax and shipping)
'tax':'<?php echo number_format($order->get_total_tax(), 2, ".", ""); ?>',
'shipping': '<?php echo number_format($order->calculate_shipping(), 2, ".", ""); ?>'
},
'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, ".", ""); ?>',
'category': '<?php echo strip_tags($product->get_categories(', ', '', '')); ?>',
'variant': '<?php echo ($variant_name) ? implode("-", $variant_name->get_variation_attributes()) : ''; ?>',
'quantity': '<?php echo $item['qty']; ?>'
},
<?php endforeach; ?>
]
}
}
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment