Updated: It is possible to use many different hooks for that:
wp_head
wp_footer
woocommerce_thankyou
You can try to use the:
- Order id (Is easy to get it)
- Order Key:
$order_key = get_post_meta( $order_id, '_order_key', true );
- Transaction id:
$transaction_id = get_post_meta( $order_id, '_transaction_id', true );
1) Using woocommerce_thankyou
hook: The simpler way as the Order Id is a hook argument:
add_action( 'woocommerce_thankyou', 'checkout_clickwork_js_script', 22, 1 );
function checkout_clickwork_js_script( $order_id ) {
if ( ! $order_id ) return; // Exit
$transaction_id = get_post_meta( $order_id, '_transaction_id', true );
$order_key = get_post_meta( $order_id, '_order_key', true );
if( ! empty($transaction_id) ){
$value = $transaction_id; // TRANSACTION ID
}
elseif( ! empty($order_key) ){
$value = $transaction_id; // ORDER KEY
}
else {
$value = $transaction_id; // ORDER ID
$url = "https://clickwork7secure.com/p.ashx?o=45875&e=12995&f=js&t=$value";
?><script type="text/javascript" src="<?php echo $url; ?>"></script> <?php
}
Code goes in function.php file of your active child theme (or active theme). It should work.
2) Using wp_head
hook:
add_action( 'wp_head', 'checkout_clickwork_js_script', 998 );
function checkout_clickwork_js_script() {
// Only order-received page
if( is_wc_endpoint_url('order-received') ) :
global $wp;
$order_id = absint( $wp->query_vars['order-received'] );
if ( ! $order_id || empty($order_id) )
return; // Exit
$transaction_id = get_post_meta( $order_id, '_transaction_id', true );
$order_key = get_post_meta( $order_id, '_order_key', true );
if( ! empty($transaction_id) ){
$value = $transaction_id; // TRANSACTION ID
}
elseif( ! empty($order_key) ){
$value = $transaction_id; // ORDER KEY
}
else {
$value = $transaction_id; // ORDER ID
$url = "https://clickwork7secure.com/p.ashx?o=45875&e=12995&f=js&t=$value";
?><script type="text/javascript" src="<?php echo $url; ?>"></script> <?php
endif;
}
Code goes in function.php file of your active child theme (or active theme). It should work.
The condition if( is_wc_endpoint_url('order-received') ) :
can be extended to handle cancelled and failed orders custom endpoints too …
Similar answers:
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…