Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save clayb/728d072ff3b2e96b0969636a404d56c6 to your computer and use it in GitHub Desktop.
Save clayb/728d072ff3b2e96b0969636a404d56c6 to your computer and use it in GitHub Desktop.
<?php
/**
* Render dataLayer on a WooCommerce thank you page.
* This file should be added at the end of Theme Functions (functions.php)
*
* Official Class descriptions:
* https://docs.woocommerce.com/wc-apidocs/class-WC_Abstract_Order.html
* https://docs.woocommerce.com/wc-apidocs/class-WC_Order.html
* https://docs.woocommerce.com/wc-apidocs/class-WC_Order_Item.html
* https://docs.woocommerce.com/wc-apidocs/class-WC_Abstract_Legacy_Order.html
*
* Useful links:
* https://docs.woocommerce.com/document/custom-tracking-code-for-the-thanks-page/
* https://stackoverflow.com/questions/39401393/how-to-get-woocommerce-order-details/44708344#44708344
* https://stackoverflow.com/questions/39831875/enhanced-ecommerce-datalayer-woocommerce
*
* There are some uncertainties around the implementation.
* Sometimes $order items appear as an associative array, sometimes as object.
* The final version should be determined at debugging stage.
* Also, certain methods are deprecated but yet still appear in "fresh" blog posts.
*
* PLEASE NOTE:
* Below script was not tested at all and the only purpose of it is to
* demonstrate how the final JS dataLayer object could be renedered on
* WooCommerce thank you page. Certain adjustment may be needed.
* Please reference above documentation.
*
* IMPORTANT: The top "<?php" should be removed
* when pasting this entire script into functions.php.
*/
add_action('woocommerce_thankyou', 'woocommerce_thankyou_push_data_layer');
function woocommerce_thankyou_push_data_layer($order_id) {
// Lets grab the order
$order = wc_get_order($order_id);
?>
<script>
window.dataLayer = window.dataLayer || [];
var nsala_order_info = {
'event': 'purchase',
'transactionId': '<?php echo $order->get_order_number(); ?>',
'transactionAffiliation': '<?php echo get_bloginfo('name'); ?>',
'transactionTotal': <?php echo number_format($order->get_total(), 2, ".", ""); ?>,
'transactionTax': <?php echo number_format($order->get_total_tax(), 2, ".", ""); ?>,
'transactionShipping': <?php echo number_format($order->calculate_shipping(), 2, ".", ""); ?>,
'transactionProducts': []
};
<?php
$line_items = $order->get_items();
foreach ($line_items as $item) {
// get_product_from_item() seems to be deprecated
// (https://docs.woocommerce.com/wc-apidocs/class-WC_Abstract_Legacy_Order.html#_get_product_from_item)
// but let's hope still works as it may be the only way to get SKU and categories:
$product = $order->get_product_from_item($item);
?>
nsala_order_info['transactionProducts'].push({
'sku': '<?php echo $product->get_sku(); ?>',
'name': '<?php echo $item->get_name(); ?>',
'category': '<?php echo strip_tags($product->get_categories(', ', '', '')); ?>',
'price': <?php echo number_format($order->get_line_total($item), 2, ".", ""); ?>,
'quantity': <?php echo $item->get_quantity(); ?>
});
<?php
}
?>
dataLayer.push(nsala_order_info);
</script>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment