Reputation: 3285
My affiliate script tracks a conversion after an order is placed. it runs inside the woocommerce_thankyou
action hook:
function affiliate_tracking_code( $order_id ) {
// get the order info for the script
?>
<script>
// affiliate script here
</script>
<?php
}
add_action( 'woocommerce_thankyou', 'affiliate_tracking_code', 10, 1 );
I do not want this script to fire if the order has failed or is pending. Only if it is successful. I can not find in the documentation whether or not the woocommerce_thankyou
action hook fires for anything but successful orders.
If it does then what is the best way to make sure that my script only tracks conversions for successful orders and not failed ones.?
One way I have tested is to wrap my script in an if and check if ( $order->get_status() == 'processing' ) : // run the script
however I am not sure if there are hidden loopholes.
Upvotes: 5
Views: 2358
Reputation: 11841
Yes, it will fire or failed orders as well.
add_action('woocommerce_before_thankyou', 'woocommerce_before_thankyou_failed_order')
function woocommerce_before_thankyou_failed_order( $order_id ) {
$order = wc_get_order( $order_id );
if ( !$order->has_status( 'failed' ) ) {
// if order not failed
}
}
See the hook under wp-content/plugins/woocommerce/templates/checkout/thankyou.php
Upvotes: 5