I'll love to know what is wrong in the code below. I'm using WordPress WooCommerce and my site tag makes my website crash when I upload this functions.php -> The global site tag is in the head, detected by the google tag chrome extension -> The conversion tracking code is in functions.php and it need to be visible only in the thank you page when the order is done.
Here is my functions.php page.
Any help welcomed, I'm just getting started with PHP.
Julien,
<?php
/**
* Add custom tracking code to the thank-you page
*/
add_action( 'woocommerce_thankyou', 'conversion_tracking' );
function conversion_tracking( $order_id ) {
// Lets grab the order
$order = new WC_Order( $order_id );
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('event', 'conversion', {
'send_to': 'AW-954963704/lbfjCPunp_kCEPitrscD',
'value': '<?php echo $order->get_total(); ?>',
'currency': '<?php echo $order->get_currency(); ?>',
'transaction_id': '<?php echo $order_id; ?>'
});
</script>
// This is the order total
$order_total = $order->get_total();
// 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 ) {
// This will be a product
$product = $order->get_product_from_item( $item );
// This is the products SKU
$sku = $product->get_sku();
// This is the qty purchased
$qty = $item['qty'];
// Line item total cost including taxes and rounded
$total = $order->get_line_total( $item, true, true );
// Line item subtotal (before discounts)
$subtotal = $order->get_line_subtotal( $item, true, true );
}
}
<?php
}
/*-----------------------------------------------------------------------------------*/
/* Init theme framework
/*-----------------------------------------------------------------------------------*/
require( 'auxin/auxin-include/auxin.php' );
/*-----------------------------------------------------------------------------------*/
Here is some woocommerce and google example page https://support.google.com/searchads/answer/9133542?hl=fr https://woocommerce.com/document/custom-tracking-code-for-the-thanks-page/#
CodePudding user response:
Your issue is that you're not using echo or any method for outputting the script. This is causing your fatal error.
Additionally, your script should be added with wc_enqueue_js
which executes the script cleanly.
function conversion_tracking( $order_id ) {
// Lets grab the order
$order = new WC_Order( $order_id );
$script = "window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('event', 'conversion', {
'send_to': 'AW-954963704/lbfjCPunp_kCEPitrscD',
'value': '{$order->get_total()}',
'currency': {$order->get_currency()}',
'transaction_id': '{$order_id}'
});";
wc_enqueue_js($script);
}
The rest of your function can be cleaned up, but that's not part of your questions.
CodePudding user response:
This should do (there are some function parts you don't need you need to echo the script, as you're inside PHP):
add_action( 'woocommerce_thankyou', 'bloomer_conversion_tracking' );
function bloomer_conversion_tracking( $order_id ) {
$order = wc_get_order( $order_id );
?>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('event', 'conversion', {
'send_to': 'AW-954963704/lbfjCPunp_kCEPitrscD',
'value': '<?php echo $order->get_total(); ?>',
'currency': '<?php echo $order->get_currency(); ?>',
'transaction_id': '<?php echo $order_id; ?>'
});
</script>
<?php
}