Here’s an example of how you might add the code to retrieve the purchase value in the functions.php
file:
<?php
add_action( ‘woocommerce_thankyou’, ‘add_transaction_total_to_data_layer’ );
function add_transaction_total_to_data_layer( $order_id ) {
$order = wc_get_order( $order_id );
$transaction_total = $order->get_total();
?>
<script>
dataLayer.push({‘transaction_total’: <?php echo $transaction_total; ?>});
</script>
<?php
}
?>
This code will add the purchase value to the data layer when the woocommerce_thankyou
action is called, which occurs when the thank you page is loaded.
Once the purchase value is in the data layer, you can create a variable in GTM that pulls the value from the data layer. Then, you can use this variable in the Facebook Pixel tag’s value field, so the pixel will send the purchase value to Facebook Ads Manager, to track the conversion.
for example
fbq('track', 'Purchase', {value: '{{transaction_total}}', currency: 'USD'});