Skip to content

Instantly share code, notes, and snippets.

@devinsays
Created May 15, 2015 22:55
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 devinsays/6f4738c585a3bc8c43d8 to your computer and use it in GitHub Desktop.
Save devinsays/6f4738c585a3bc8c43d8 to your computer and use it in GitHub Desktop.
FriendBuy Conversion Tracker
<?php
/**
* Friendbuy conversion tracker
*
*/
function friendbuy_conversion_tracker( $order_id ) {
// Lets grab the order
$order = new WC_Order( $order_id );
?>
<script>
window['friendbuy'] = window['friendbuy'] || [];
window['friendbuy'].push(['track', 'order',
{
id: '<?php echo $order->get_order_number(); ?>',
amount: '<?php echo $order->get_total(); ?>',
email: '<?php echo $order->billing_email; ?>'
}
]);
window['friendbuy'].push(['track', 'products', [
<?php
$count = 0;
foreach( $order->get_items() 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', 'friendbuy_conversion_tracker' );
@devinsays
Copy link
Author

We used the boilerplate code on http://docs.friendbuy.com/article/85-woocommerce but got two PHP errors:

Notice: Indirect modification of overloaded property WC_Order::$items has no effect in [filename]
Warning: end() expects parameter 1 to be array, string given in [filename]

The issue was this line:

<?php if ($item !== end($order->items)) { echo ","; } ?>

Fixed with by using a $count variable and:

<?php if ( count( $order->get_items() ) > $count ) { echo ","; } ?>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment