Working with WooCommerce

When working with a WooCommerce installation to track sales for affiliates, you only need to add a small piece of code to your thank you page that passes through the sale amount and order ID for reference in your conversion report.

First, access the WordPress admin interface and go to Appearance > Editor from the main navigation. On the right side of the editor page is a list of files for the activated theme. Click the “Theme Functions (functions.php)” file.

In this file, you need to add a way to send information from WooCommerce to HasOffers. One way to do this is to assemble and fire a conversion using your network ID, your offer ID, the order subtotal, and the order number.

To do so, paste in the following code at the top of the file, after the <?php line:

add_action( 'woocommerce_thankyou', 'my_custom_tracking' );

function my_custom_tracking( $order_id ) {
  $order = wc_get_order( $order_id );
  $total = $order->get_subtotal();
  $order_num = str_replace('#', '', $order->get_order_number());
  echo '<iframe src="https://NETWORKID.go2cloud.org/aff_l?offer_id=OFFERID&amount=' . $total . '&adv_sub=' . $order_num . '" scrolling="no" frameborder="0" width="1" height="1"></iframe>';
}

Replace “NETWORKID” and “OFFERID” with the appropriate network ID and offer ID from your network. Save the “functions.php” file and the tracking code is ready for test conversions.

For other questions about the WooCommerce API, reference their official documentation.

31 Comments

Leave a reply
  • Sazzy  •  January 4, 2016

    Hi I want to use custom tracking code but don't know how to pass order id and amount in code pl help

    • Patrick  •  January 7, 2016

      The code provided is complete and should work if placed into functions.php as directed. The code passes the order ID in the 'aff_sub' parameter and the sale amount in the 'amount' parameter. If you need further assistance, please contact suppo[email protected] or your account manager.

  • Mark Myers  •  January 19, 2016

    I've had a merchant try this and the site crashes in errors. Please provide more details on where to place this code in the functions.php file.

    • Patrick  •  January 21, 2016

      We've now clarified this issue in the article. Place the above code at the top of the file.

      • You have a ; in the amount='; in your code that you need to remove. Should be:

        add_action( 'woocommerce_thankyou', 'my_custom_tracking' ); function my_custom_tracking( $order_id ) { $order = new WC_Order( $order_id ); $total = $order->get_subtotal(); $id = str_replace('#', '', $order->get_order_number()); echo ''; }

  • Rebecca  •  October 11, 2016

    Hi,

    Will this pixel work in Google Tag Manager? I have placed it on a trigger that fires off after checkout. Please confirm.

    Also, what kind of conversion pixel is this? Should this be set up as which of the following in the offer:

    HTTP iFrame Pixel
    HTTP Image Pixel
    HTTPS iFrame Pixel
    HTTPS Image Pixel

    • Patrick  •  October 13, 2016

      Thanks for writing in! This should work with Google Tag Manager, but that depends on your setup. If you run into problems, please contact support.

      The code in the article is for a HTTPS iframe pixel.

  • Hello,
    I have inserted a code above, and added tracking code provided after creating an offer, however during testing sales, the sale amount is not being passed. Please help.

    • Patrick  •  December 1, 2016

      Please contact our customer support team for help on your issue, and they'll be happy to help you out.

  • Alex  •  May 3, 2017

    Thank you for the post / script!

    This script is powerful, if you are using only one network pixel. I am trying to figure out, how to add multiple conversion pixels (...for specific products and for specific situations)

    Lets say i have the following situation:

    I am Using example.com with WooCommerce.

    I sell the following Products (+ Suspected pixel type and Example Pixel):

    My own Products (with different Variants) -> CPS Pixel

    https://NETWORKID.go2cloud.org/aff_l?offer_id=10&amount=

    My own Products (without Variant) -> CPA Pixel

    CPS:https://NETWORKID.go2cloud.org/aff_l?offer_id=11

    External Products ((with different Variants) -> CPS Pixel

    https://NETWORKID.go2cloud.org/aff_l?offer_id=12&amount=

    External Products (without Variant) -> CPA Pixel

    https://NETWORKID.go2cloud.org/aff_l?offer_id=13

    How would I have to change / extend the code so that the corresponding pixels or several pixels only fires in certain situations?

    Maybe there is a way to create a direct thank you page for specific products?

    • Jonathan  •  May 4, 2017

      Greetings! One option could be to modify the PHP to insert an offer_id value based on the product being purchased, but the implementation of that would depend on other factors. I recommend reaching out to your account manager, and they'll help you out as best they can!

    • Jonas  •  March 18, 2019

      Hi Alex - did you find a solution for this issue?

      Was hoping to find a way, where the offer_id would be dynamic,

  • Rene  •  May 11, 2017

    The Code works very well only with auto approval. There is a trouble shooting when you are using "pending status" in order to check every transaction. For example a customers buys a product and the transaction is automatically confirmed. But what happens if the customer maybe returns the product? Thats actually an unpleasant situation, because my affiliate will get the message about a successful transaction. In the worst case, I would lose money.

    • Rene  •  May 11, 2017

      The transaction with "pending status" itself will be tracked, but the amount value will not pass and the only number that you can see is 0,00$.

      • Jonathan  •  May 16, 2017

        Thanks for your comment! Handling returns can be tricky. We recommend enabling the "Approve Conversions" tracking setting for your offer if you have a return window. That way, you can approve the conversions and notify publishers only after passing the return window.

        Sale amounts should be captured regardless of the conversion status. If you'd like some help troubleshooting this issue, contact our customer support team with more details about your situation.

  • Hi, i've implemented your code as is and my website does fire the postback but my agency needs me to add the transaction ID that your platform generate dynamicaly per session and send it within the postback to confirm the purchase. Anyway how to save the query parameter "transid" per session and add it in the postback url?

    Thanks,

    • Patrick  •  August 29, 2017

      Postbacks including transaction IDs change a little. The underlined part is what's different:

      https://NETWORKID.go2cloud.org/aff_l?offer_id=OFFERID&transaction_id=TRANSID&amount=…

      You'll need to implement storing our transaction ID in WooCommerce, and put that stored ID where "TRANSID" is in the URL.

      If you desire, see our article on implementing postbacks with transaction ID to learn more.

  • We implemented the pixels exactly the way explained above and the conversion was successfully recorded with correct order id and sale amount, but when a discounted product was purchased, the conversion recorded the actual sale amount, not the disounted one. Can you please advise on how can we record the discounted amount?

  • Hi, the script is perfect. But I have a particular request from a affiliate network with which I'm collaborating. I have a landing page with WooCommerce. I need HasOffers to recognize that user pays with credit card / Paypal or chooses cash on delivery. Then it associates different goals. Is it possible?

    • Patrick  •  January 4, 2018

      If you explicitly need to use offer goals, replace the conversion part of the URL with: /aff_goal?a=lsr&goal_id=GOAL_ID...

      Another option: Pass a value as an advertiser sub ID by appending &adv_sub2=PAYMENT_TYPE to the URL.

      Either way, you'll need to write the code that determines what values you implement in your WooCommerce setup, replacing the element in blue with the desired response.

  • Jennifer  •  March 8, 2018

    Hi, this keeps crashing my site - I am replacing network and offer IDs.

  • Jon  •  April 12, 2018

    We are using hasoffers to setup our affiliate program for a woocommerce site with just 3 products, and we will be paying a different fixed amount per product sold.

    We set up the offer within hassoffers with one product and then set 2 goals to cover the other two products.

    Woocommerce only allows one pixel placed for the order completion, but not on individual product sales.

    How would I alter the code above to pass the goals and sub-ids?

    And, if I place that code in the woocommerce functions.php file altered however it needs to be, will it automatically record the correct products and appropriate payouts?

    #1) Can you tell me how to alter that code for the functions.php file in woocommerce to handle the goals and sub-ids.

    #2)If I place that code, will it automatically record the correct products and appropriate payouts?

    #3) With that code placed in the functions.php file do I still need to place the advertiser pixel on the website or does this handle that and take its place?

    • Jonathan  •  April 17, 2018

      Hi Jon,
      If you explicitly need to use offer goals, replace the conversion part of the URL with: /aff_goal?a=lsr&goal_id=GOAL_ID...
      Note that your code will need to dynamically replace "GOAL_ID" with the appropriate goal ID. Functionally, you can add advertiser sub IDs in the same manner with parameters like &adv_sub2=.

      As for your last question, the URL of the iframe pixel being added (either with "/aff_l?" or "aff_goal") is the conversion pixel. Feel free to direct any other questions you have to our customer support team. They'll be able to better assess your situation and move you towards a solution.

  • Hi, I am using this code, so far so good. But I have a little issue. Let's say I have a product of ₹500. And I am giving 5% off coupon.so when the user buys and applies the coupon the product becomes ₹475 product. but the using the same queries it shows the subtotal of ₹500. It should be ₹475. So how can I query the subtotal after the coupon has applied?

  • Hello,

    code works great but with only one offer.

    Someone can supply a code that can work with multiple products on woocommerce? I need to pass different offers corresponding to different products on my woocommerce. (i have no skill as developer so if someone can supply a working code will be great)

    Any help will be greatly appreciated :)