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:

  1. Track the Transaction ID + Affiliate ID data & save it to cookies
  2. Pass the data through the order process
  3. Send data to HasOffers
  4. 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 ) . '

'; }
}

 

 

How useful was this post?

Click on a star to rate it!

Average rating / 5. Vote count:

We are sorry that this post was not useful for you!

Let us improve this post!