Skip to content

Instantly share code, notes, and snippets.

@tankbar
Created March 21, 2018 10:39
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 tankbar/770a0a3565229324126b36c70931637f to your computer and use it in GitHub Desktop.
Save tankbar/770a0a3565229324126b36c70931637f to your computer and use it in GitHub Desktop.
Trustpilot AFS product tracking in order confirmation mail for WooCommerce
add_action( 'woocommerce_email_after_order_table', 'add_trustpilot_afs_tracking', 20, 2 );
function add_trustpilot_afs_tracking( $order, $sent_to_admin ) {
if ( ! $sent_to_admin ) {
?>
<script type="application/json+trustpilot">
{
"recipientEmail": "<?php echo $order->billing_email; ?>",
"recipientName": "<?php echo $order->billing_first_name.' '. $order->billing_last_name; ?>",
"referenceId": "<?php echo $order->get_order_number(); ?>",
"products": [<?php
// This is how to grab line items from the order
$line_items = $order->get_items();
// This loops over line items
foreach ( $line_items as $item ) {
$product_name = $item['name'];
$product_id = $item['product_id'];
$product_qty = $item['qty'];
$product = $order->get_product_from_item( $item );
// Get SKU
$product_sku = $product->get_sku();
//Get Product Object
$product_obj = new WC_Product( $item["product_id"] );
$product_url = $product_obj->get_permalink();
//$product_image = $product_obj->get_image();
?>{
"productUrl": "<?php echo $product_url; ?>",
"name": "<?php echo $product_name; ?>",
"sku": "<?php echo $product_sku; ?>"
},
<?php
} ?> ]
}
</script>
<?php
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment