1

I'm looking to integrate tracking from the affiliate program commission junction.

They have provided me with the following sample code to add to my order received page, any help would be appreciated regarding how to place this in /order-received/ endpoint and the modifications its asking for. I'm absolutely lost, there are plugins for several similar programs.

(They offer a javascript and asp alternative by the looks of things if that helps)

<!-- BEGIN COMMISSION JUNCTION TRACKING CODE -->

<iframe height="1" width="1" frameborder="0" scrolling="no"         src="https://www.emjcd.com/tags/c?containerTagId=14209&ITEMx=[ItemSku]&AMTx=    [AmountofItem]&QTYx=[Quantity]&CID=1529328&OID=[OID]&TYPE=385769&AMOUNT=[Subtotal]&DISCOUNT=[DiscountAmount]&CURRENCY=[CURRENCY]&COUPON=[couponcode]" name="cj_conversion" ></iframe>

<!-- END COMMISSION JUNCTION TRACKING CODE -->
Thomas Reimer
  • 11
  • 1
  • 2

2 Answers2

0

Add the following code in your themes functions.php file

add_action( 'woocommerce_thankyou', 'my_custom_tracking' );

function my_custom_tracking( $order_id ) {

    global  $woocommerce;

    $order = wc_get_order( $order_id );
    $total = $order->get_total();
    $currency = get_woocommerce_currency();

    $coupons = $order->get_used_coupons();

    $coupon_code = '';

    foreach ($coupons as $coupon){
        $coupon_code = $coupon;
    }

    $discount = $order->get_total_discount();

    $tracking = '<iframe height="1" width="1" frameborder="0" scrolling="no" src="https://www.emjcd.com/tags/c?containerTagId=14209&ITEMx=[ItemSku]&AMTx=[AmountofItem]&QTYx=[Quantity]&CID=1529328&OID=[OID]&TYPE=385769&AMOUNT='. $total .'&DISCOUNT='. $discount .'&CURRENCY='. $currency .'&COUPON='. $coupon_code .'" name="cj_conversion" ></iframe>';
    echo $tracking;

    }

Looking at your tracking code, I am having a guess that we may need to fill the src url with correct values. But i am not sure.

Dezefy
  • 2,048
  • 1
  • 14
  • 23
  • Correct, I'm under the assumption that I need to add woocommerce details for the order in questions (SKU, Amountof Item, Quantity, Coupons, etc... insanely confusing for me. – Thomas Reimer May 20 '16 at 01:41
  • Hi, I have updated the answer, I managed to include the order total, currency , used coupon code, discount amount etc. I could not include itemSku / amount of item / quantity cause their can be more than one product in order. – Dezefy May 20 '16 at 02:36
  • @MashR. why not do a for loop and iterate across the products in the order? – jonaz May 21 '16 at 00:01
  • @jonaz , I am not sure how they wanted the sku and other cart details. comma separated or something else. – Dezefy May 23 '16 at 12:28
  • @MashR. Take a look at my solution in JavaScript which is the way CJ wants it delimited. I think OP would prefer a solution in PHP, so if you want to revise your solution to mirror my output I think OP should select yours as the accepted answer. – jonaz May 24 '16 at 17:24
0

You'll need to list out all of the SKUs purchased along with their prices. If you want you can use the DISCOUNT parameter to apply a dollar discount to the order, unless you're already deducting discounts in the price you put in the iframe. Coupon code is also a nice-to-have.

If you want to do this in JavaScript and assuming your order data is in an array of objects that looks like the dummy data below here's how you'd do that in JavaScript:

// Some dummy data (populate real data in your code)
var order = {
 id: "AB12345",
 subtotal: "200.00",
 discount: "10.00",
 coupon: "DEAL10",
 items: [
  { sku: "FOO123", price: "75.00", quantity: 2 },
  { sku: "BAR234", price: "50.00", quantity: 1 }
 ]
};

// The actual code
var cj = {
 tagId: 14209,
 cid: 1529328,
 type: 385769
};
var cjString = "https://www.emjcd.com/tags/c?containerTagId=" + cj.tagId + "&";

for (i=0; i<order.items.length; i++) {
  cjString += "ITEM" + i + "=" order.items[i].sku + "AMT" + i + order.items[i].price + "QTY" + i + order.items[i].quantity + "&";
}

cjString += "CID=" + cj.cid + "&OID=" + order.id + "&TYPE=" + cj.type + "&AMOUNT=" + order.subtotal + ( discount.length ? "&DISCOUNT=" + order.discount : "" ) + "&CURRENCY=USD" + ( coupon.length ? "&COUPON=" + order.coupon : "" );

// Now we put it all together and insert into the page
var frame = document.createElement("iframe");
frame.name = "cj_conversion";
frame.height = 1;
frame.width = 1;
frame.frameBorder = 0;
frame.scrolling = "no";
frame.src = cjString;
document.body.insertBefore(frame, document.body.childNodes[0]);
jonaz
  • 2,096
  • 1
  • 17
  • 30