Integrate HasOffers (By Tune) With WooCommerce
In this post I will include my code to track, store and pass HasOffers (affiliate id + transaction id + amount of order) data from the the first page, through the order details and back to HasOffers when the purchase is successfull.
I will divide the process to a few steps:
- Track the Transaction ID + Affiliate ID data & save it to cookies
- Pass the data through the order process
- Send data to HasOffers
- Show HasOffers parameters on order page
You need to copy & paste all snippets into your theme’s functions.php file to make it work. Please test this code first before pushing into production – it’s on your own responsibility only. 🙂
1. Catch Affiliate ID + Transaction ID & Save in cookies
add_action('wp_loaded', 'catch_hasoffers_querystring',1); function catch_hasoffers_querystring() { if( $_GET["transaction_id"] != "" ) { setcookie("_hasoffers_transaction_id",$_GET["_hasoffers_transaction_id"],time()+31556926 ,'/'); } if( $_GET["affiliate_id"] != "" ) { setcookie("_hasoffers_affiliate_id",$_GET["_hasoffers_affiliate_id"],time()+31556926 ,'/'); } }
2. Update Cookie’d Affiliate ID + Transaction ID in order meta when the order is created
// Saving the hidden field value in the order metadata add_action( 'woocommerce_checkout_update_order_meta', 'save_custom_checkout_hidden_field' ); function save_custom_checkout_hidden_field( $order_id ) { if ( ! empty( $_COOKIE["_hasoffers_affiliate_id"] ) ) { update_post_meta( $order_id, '_hasoffers_affiliate_id', sanitize_text_field( $_COOKIE["_hasoffers_affiliate_id"] ) ); } if ( ! empty( $_COOKIE["_hasoffers_transaction_id"] ) ) { update_post_meta( $order_id, '_hasoffers_transaction_id', sanitize_text_field( $_COOKIE["_hasoffers_transaction_id"] ) ); } }
3. Send Affiliate ID + Transaction ID to HasOffers when order is completed
It will also add an order note with the affiliate id + transaction id.
Note: You need to replace the NETWORKID parameter in the URL with your own network id that was provided to you by HasOffers.
add_action( 'woocommerce_order_status_completed', 'pp_hasoffers_send_data'); function pp_hasoffers_send_data($order_id) { //getting order object $order = wc_get_order($order_id); $transaction_id = get_post_meta( $order_id, '_hasoffers_transaction_id', true ); $affiliate_id = get_post_meta( $order_id, '_hasoffers_affiliate_id', true ); if ($affiliate_id == "" && $transaction_id == "") { return; } $items = $order->get_items(); $product_js = []; $tprice = 0; foreach ($items as $item_id => $item_data) { $tprice = $tprice + $pro_price; } $url = 'http://NETWORKID.go2cloud.org/aff_lsr?amount='.$tprice.'&transaction_id='.$transaction_id.'&affiliate_id='.$affiliate_id; $curl = curl_init(); curl_setopt ($curl, CURLOPT_URL, $url); curl_setopt ($curl, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($curl); if (!curl_errno($curl)) { $http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); if ($http_code === 200) { $order->add_order_note( 'HasOffers Success! Affiliate: '.$affiliate_id.' (Transaction '.$transaction_id.')' ); } else { $order->add_order_note( 'HasOffers Failed: '.$result ); } } curl_close($curl); unset($result); }
4. Display Affiliate ID + Transaction ID in Order Details (wp-admin)
// Display HasOffers data on Admin order edit page add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_verification_id_in_admin_order_meta', 10, 1 ); function display_verification_id_in_admin_order_meta( $order ) { $order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id; if (get_post_meta( $order_id, '_hasoffers_affiliate_id', true )) { echo ' <strong>'.__('[HO] Affiliate ID', 'woocommerce').':</strong> ' . get_post_meta( $order_id, '_hasoffers_affiliate_id', true ) . ' '; } if (get_post_meta( $order_id, '_hasoffers_transaction_id', true )) { echo ' <strong>'.__('[HO] Transaction ID', 'woocommerce').':</strong> ' . get_post_meta( $order_id, '_hasoffers_transaction_id', true ) . ' '; } }
I am getting the “The site is experiencing technical difficulties. Please check your site admin email inbox for instructions. ” error.
When i check my e-mail with the error message:
“An error of type E_PARSE was caused in line 54 of the file /sites/xxx/files/wp-content/plugins/theme-customisations-master/custom/functions.php. Error message: syntax error, unexpected ‘;’
”
I get this error when adding the 3th block, and line 54 is:
if ($affiliate_id == “” && $transaction_id == “”) { return; }
$items = $order->get_items();
$product_js = [];
Any Idea??
Try changing the ” return; ” to ” return false; ”
or rewrite the whole line, just noticed some of the codes didn’t pass through good enough for copy paste :S
I have tried your code, But I am not getting any data from Tune hasoffers redirect. Could you, please help.
Hey,
At what step do you get stuck?
Do you put the order status on “completed”? That’s what triggers it.
Hi,
Can you make hasoffers and woocommerce script work for a small fee?
Thanks
Hello Frank
Please email me the exact issue: [email protected]
Cheers!
your code has major issues but i have corrected it here it is below:
add_action(‘wp_loaded’, ‘catch_hasoffers_querystring’,1);
function catch_hasoffers_querystring() {
if( $_GET[“transactionid”] != “” ) { setcookie(“_hasoffers_transaction_id”,$_GET[“transactionid”],time()+31556926 ,’/’); }
if( $_GET[“affiliate_id”] != “” ) { setcookie(“_hasoffers_affiliate_id”,$_GET[“affiliate_id”],time()+31556926 ,’/’); }
}
// Saving the hidden field value in the order metadata
add_action( ‘woocommerce_checkout_update_order_meta’, ‘save_custom_checkout_hidden_field’ );
function save_custom_checkout_hidden_field( $order_id ) {
if ( ! empty( $_COOKIE[“_hasoffers_affiliate_id”] ) ) {
update_post_meta( $order_id, ‘_hasoffers_affiliate_id’, sanitize_text_field( $_COOKIE[“_hasoffers_affiliate_id”] ) );
}
if ( ! empty( $_COOKIE[“_hasoffers_transaction_id”] ) ) {
update_post_meta( $order_id, ‘_hasoffers_transaction_id’, sanitize_text_field( $_COOKIE[“_hasoffers_transaction_id”] ) );
}
}
add_action( ‘woocommerce_order_status_processing’, ‘pp_hasoffers_send_data’);
function pp_hasoffers_send_data($order_id)
{
//getting order object
$order = wc_get_order($order_id);
$order_ids = $order->get_id();
$transaction_id = get_post_meta( $order_id, ‘_hasoffers_transaction_id’, true );
$affiliate_id = get_post_meta( $order_id, ‘_hasoffers_affiliate_id’, true );
if ($affiliate_id == “” && $transaction_id == “”) { return; }
$items = $order->get_items();
$product_js = [];
$tprice = 0;
$p_name = “”;
foreach ( $order->get_items() as $item_id => $item ) {
$p_name = $item->get_name();
$tprice = $tprice + $item->get_total();
}
$url = ‘https://candlefox.go2cloud.org/aff_lsr?offer_id=5&adv_sub=’.urlencode($p_name).’&adv_sub2=’.$order_ids.’&amount=’.$tprice.’&transaction_id=’.$transaction_id.’&affiliate_id=’.$affiliate_id;
$curl = curl_init();
curl_setopt ($curl, CURLOPT_URL, $url);
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curl);
if (!curl_errno($curl)) {
$http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($http_code === 200) {
$order->add_order_note( ‘HasOffers Success! Affiliate: ‘.$affiliate_id.’ (Transaction ‘.$transaction_id.’) Total Price : ‘.$tprice .’ ‘ );
} else {
$order->add_order_note( ‘HasOffers Failed: ‘.$result. $url );
}
}
curl_close($curl);
echo $url;
unset($result);
}
// Display HasOffers data on Admin order edit page
add_action( ‘woocommerce_admin_order_data_after_billing_address’, ‘display_verification_id_in_admin_order_meta’, 10, 1 );
function display_verification_id_in_admin_order_meta( $order ) {
$order_id = method_exists( $order, ‘get_id’ ) ? $order->get_id() : $order->id;
if (get_post_meta( $order_id, ‘_hasoffers_affiliate_id’, true )) { echo ‘
‘.__(‘[HO] Affiliate ID’, ‘woocommerce’).’: ‘ . get_post_meta( $order_id, ‘_hasoffers_affiliate_id’, true ) . ‘
‘; }
if (get_post_meta( $order_id, ‘_hasoffers_transaction_id’, true )) { echo ‘
‘.__(‘[HO] Transaction ID’, ‘woocommerce’).’: ‘ . get_post_meta( $order_id, ‘_hasoffers_transaction_id’, true ) . ‘
‘; }
}
Hi friend
In the name of all future readers, thank you for the corrections =) I will review it and update my post as well.