If you’re using WooCommerce to sell products on your WordPress site, you may find that you need to access information about an order inside the wp_head
hook on the thank you page. This is the page that a customer is redirected to after they have successfully completed an order.
There are a few reasons why someone might need to access order information inside wp_head
on the thank you page:
- To track conversions for advertising purposes: By accessing the order information, you can track conversions and attribute them to specific campaigns or ad groups. This can help you optimize your advertising efforts and better understand the return on investment for your advertising spend.
- To add custom scripts or styles: If you want to add custom scripts or styles to the thank you page, you can use the
wp_head
hook to do so. By accessing the order information, you can tailor these scripts or styles to the specific order that was just placed. - To send data to external services: If you use a service like Google Analytics or Facebook Pixel to track conversions and gather customer data, you can use the
wp_head
hook to send data about the order to these services.
To access WooCommerce order information inside wp_head
, you can use the get_query_var()
. function.
Here’s an example of how you might use the order ID inside wp_head
to get the Order object and add order_id
and order_total
to tracking script:
function add_custom_tracking_pixel() { if ( is_wc_endpoint_url( 'order-received' ) ) { $order_id = absint( get_query_var( 'order-received' ) ); $order = wc_get_order( $order_id ); $order_total = $order->get_total(); echo '<!-- Custom pixel --><img src="https://analytics.xyz/?action=order&value=' . $order_total . '&order_id=' . $order_id . '" width="1" height="1" border="0"><!-- End Pixel -->'; } } add_action('wp_head', 'add_custom_tracking_pixel');