Skip to content

Instantly share code, notes, and snippets.

@devinsays
Created June 29, 2015 17:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devinsays/d65db56ad333782e9424 to your computer and use it in GitHub Desktop.
Save devinsays/d65db56ad333782e9424 to your computer and use it in GitHub Desktop.
Example of WooCommerce Conversion Tracking for Products
function prefix_service_conversion_tracking( $order_id ) {
// Lets grab the order
$order = new WC_Order( $order_id );
// Products
$products = $order->get_items();
?>
<script>
window['service'].push(['track', 'products', [
<?php
$count = 0;
foreach( $products as $item_id => $item ) {
$count++;
$product = $order->get_product_from_item( $item ); ?>
{
sku: '<?php echo $product->get_sku(); ?>',
price: '<?php echo $order->get_line_subtotal( $item ); ?>',
quantity: '<?php echo $item['qty']; ?>'
}<?php if ( count( $order->get_items() ) > $count ) { echo ","; } ?>
<?php } ?>
]]);
</script>
<?php
}
add_action( 'woocommerce_thankyou', 'prefix_service_conversion_tracking' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment