Created
May 15, 2015 22:55
-
-
Save devinsays/6f4738c585a3bc8c43d8 to your computer and use it in GitHub Desktop.
FriendBuy Conversion Tracker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:
Fixed with by using a $count variable and: