Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bbbenji/cded65aec5915ff34f71ecac282f1aff to your computer and use it in GitHub Desktop.
Save bbbenji/cded65aec5915ff34f71ecac282f1aff to your computer and use it in GitHub Desktop.
function checkout_datalayer($order_id) {
// Get the order
$order = wc_get_order($order_id);
// Get products from order
$products = $order->get_items();
?>
<script >
zaraz.ecommerce('Order Completed', {
'order_id': '<?php echo $order->get_order_number(); ?>',
'affiliation': 'Website',
'total': '<?php echo number_format($order->get_total(), 2, ".", ""); ?>',
'revenue': '<?php echo number_format($order->get_subtotal(), 2, ".", ""); ?>',
'shipping': '<?php echo number_format($order->calculate_shipping(), 2, ".", ""); ?>',
'tax': '<?php echo number_format($order->get_total_tax(), 2, ".", ""); ?>',
<?php if($order->get_coupon_codes()): ?>
'coupon': '<?php echo $order->get_coupon_codes(); ?>,'
<?php endif; ?>
<?php if($order->get_total_discount()): ?>
'discount': '<?php echo implode("-", $order->get_total_discount()); ?>',
<?php endif; ?>
'currency': '<?php echo $order->get_order_currency(); ?>',
'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']) : '';
?>
{
'product_id': '<?php echo $item['product_id']; ?>',
'name': '<?php echo $item['name']; ?>',
'price': '<?php echo number_format($order->get_line_subtotal($item), 2, ".", ""); ?>',
'quantity': '<?php echo $item['qty']; ?>',
'category': '<?php echo strip_tags($product->get_categories(", ", "", "")); ?>',
},
<?php endforeach; ?>
]
});
</script>
<?php
}
add_action('woocommerce_thankyou', 'checkout_datalayer');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment