Forum Moderators: open

Message Too Old, No Replies

Javascript errors

Integrating Bing Cashback program with eCommerce website

         

camaroboy

1:01 am on Nov 18, 2009 (gmt 0)

10+ Year Member



I am working on integrating my site with Microsoft and could use a bit of javascript help. Basically, this javascript is supposed to take order information from the confirmation page of the site (after someone places an order) and send it over to Microsoft's Bing Cashback program.

This is the javascript, suggested by Microsoft, that should be in my payment method page:


<!–Begin cashback Tracking Pixel Code -->
<script type='text/javascript'>
<!--
var jf_merchant_id = „YOUR_CASHBACK_SHOPPING_HASHED_MERCHANT_ID?;
var jf_merchant_order_num = 'ORDER_NUMBER';
var jf_purchased_items = new Array();
// adding cart items
FOR EACH PURCHASED ITEM...
// add cart item
var jf_item = new Object();
jf_item.mpi = 'ITEM_PRODUCT_ID';
jf_item.price = 'ITEM_PRICE';
jf_item.quantity = ITEM_QUANTITY;
jf_purchased_items.push (jf_item);
NEXT PURCHASED ITEM
//-->
</script>
<script type='text/javascript' src= 'https://ssl.bing.com/cashback/javascripts/1x1tracking.js'>
</script>
<!--End cashbackTracking Pixel Code -->

This is the modified code, given to me by my hosting company, that needs to be fixed up:


<!–Begin cashback Tracking Pixel Code -->
<script type='text/javascript'>
<!--
var jf_merchant_id = 'YOUR_CASHBACK_SHOPPING_HASHED_MERCHANT_ID?';
var jf_merchant_order_num = '<%Order.Number%>';
var jf_purchased_items = new Array();
// product information data
var data = <%Order.ProductsObject%>;
// adding cart items
for (var i = 0; i < data.products.length; i++) {
var jf_item = new Object();
jf_item.mpi = data.products[i].id;
jf_item.price = data.products[i].itemPrice;
jf_item.quantity = data.products[i].quantity;
// add cart item
jf_purchased_items.push (jf_item);
}
//-->
</script>
<script type='text/javascript' src= 'https://ssl.bing.com/cashback/javascripts/1x1tracking.js'>
</script>
<!--End Cashback Tracking Pixel Code -->

This is an example, from Microsoft, of what the order confirmation page (output of above code)should have on a multiple item order:


<script type='text/javascript'>
<!--
var jf_merchant_id = „YOUR_CASHBACK_SHOPPING_HASHED_MERCHANT_ID?';
var jf_merchant_order_num = 'A9812753';
var jf_purchased_items = new Array();
// adding cart items
// add cart item
var jf_item = new Object();
jf_item.mpi = '85343';
jf_item.price = '42.50';
jf_item.quantity = 1;
jf_purchased_items.push( jf_item);
// add cart item
var jf_item = new Object();
jf_item.mpi = '78426';
jf_item.price = '52.40';
jf_item.quantity = 4;
jf_purchased_items.push( jf_item);
// add cart item
var jf_item = new Object();
jf_item.mpi = '25907';
jf_item.price = '20.45';
jf_item.quantity = 1;
jf_purchased_items.push( jf_item);
// -->
</script>
<script type='text/javascript' src= 'https://ssl.bing.com/cashback/javascripts/1x1tracking.js'>
</script>

And finally, here is what I get with my modified javascript on a multi item order:


<script type='text/javascript'>
<!--
var jf_merchant_id = 'YOUR_CASHBACK_SHOPPING_HASHED_MERCHANT_ID?';
var jf_merchant_order_num = '22272';
var jf_purchased_items = new Array();
// product information data
var data = {products : [{id : '290652', name : 'Belkin 6\' 3.5mm Stereo Audio Cable - Male To Male (F8V203-06GLD-AP)', partNumber : 'F8V203-06GLD-AP', mfgPartNumber : 'F8V203-06GLD-AP', quantity : '1', itemPrice : '8.29', totalPrice : '8.29' },{id : '321207', name : 'Motorola Talkabout GMRS\/FRS 2-Way Radios With 20-Mile Range (EM1000R)', partNumber : 'EM1000R', mfgPartNumber : 'EM1000R', quantity : '1', itemPrice : '44.09', totalPrice : '44.09' }]};
// adding cart items
for (var i = 0; i < data.products.length; i++) {
var jf_item = new Object();
jf_item.mpi = data.products[i].id;
jf_item.price = data.products[i].itemPrice;
jf_item.quantity = data.products[i].quantity;
// add cart item
jf_purchased_items.push (jf_item);
}
//-->
</script>
<script type='text/javascript' src= 'https://ssl.bing.com/cashback/javascripts/1x1tracking.js'>
</script>

The "product information data" does seem to be getting the info, but it is not putting it into the locations it needs to be in. ie:


jf_item.mpi = data.products[i].id;
jf_item.price = data.products[i].itemPrice;
jf_item.quantity = data.products[i].quantity;

The script is really close but not quite there yet. Please help!

Fotiman

2:02 pm on Nov 18, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It looks like your product data has the quantity as a string instead of an integer:

var data = {products : [{id : '290652', name : 'Belkin 6\' 3.5mm Stereo Audio Cable - Male To Male (F8V203-06GLD-AP)', partNumber : 'F8V203-06GLD-AP', mfgPartNumber : 'F8V203-06GLD-AP', quantity : '1', itemPrice : '8.29', totalPrice : '8.29' },{id : '321207', name : 'Motorola Talkabout GMRS\/FRS 2-Way Radios With 20-Mile Range (EM1000R)', partNumber : 'EM1000R', mfgPartNumber : 'EM1000R', quantity : '1', itemPrice : '44.09', totalPrice : '44.09' }]};

According to the example code, though, it looks like it should be an integer value:

jf_item.quantity = 1;

Perhaps try this:

jf_item.quantity = parseInt(data.products[i].quantity, 10);

camaroboy

5:13 pm on Nov 18, 2009 (gmt 0)

10+ Year Member



Thanks for the response Fotiman. What does the "parseInt" and ", 10" change about the script?

In the example of quantity, what the script should do is take the quantity from the order, which it is getting as you noted with the bold quantity 1's, and place it into the jf_item.quantity = ... portion.

Fotiman

5:22 pm on Nov 18, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



parseInt will parse out the integer values in a string. The "10" value means use radix of 10 (in other words, not hex or octal, etc.) so you'll get a value of 8 if you were to give it a string of "08". The only thing it changes about your script is that it converts the string value into a number value. Again, I'm not sure if this will fix your problem, but it will be more correct.

Here's an example that might clarify things:


var x = "1";
var y = "2";
var z = x + y; // z will be the string "12"
var a = 1;
var b = 2;
var c = a + b; // c will be the number 3
z = parseInt(x, 10) + parseInt(y, 10); // z will be the number 3

camaroboy

5:51 pm on Nov 18, 2009 (gmt 0)

10+ Year Member



Again thank you. I see what you're saying.

Let's take the "product id" from my modified script for example. The javascript is clearly getting the product information it needs:


// product information data
var data = {products : [{id : '290652', ...

but it will not put that product id below where it needs to go:


jf_item.mpi = data.products[i].id;

so, something about the "data.products[i].id" needs to change to see the "id : '290652'"

Fotiman

6:19 pm on Nov 18, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It looks correct. data is an object that contains a products property that holds an array of objects, with each object in that array having an id property. I just took your code and whipped up a quick example page. As this example shows, the values are being set correctly:

<html>
<head>
<title>Test</title>
</head>
<body>
<script type="text/javascript" src="http://yui.yahooapis.com/2.8.0r4/build/yahoo/yahoo-min.js"></script>
<script type="text/javascript" src="http://yui.yahooapis.com/2.8.0r4/build/json/json-min.js"></script>
<script type="text/javascript">
var jf_merchant_id = 'YOUR_CASHBACK_SHOPPING_HASHED_MERCHANT_ID?';
var jf_merchant_order_num = '22272';
var jf_purchased_items = new Array();
// product information data
var data = {
products : [
{
id : '290652',
name : 'Belkin 6\' 3.5mm Stereo Audio Cable - Male To Male (F8V203-06GLD-AP)',
partNumber : 'F8V203-06GLD-AP',
mfgPartNumber : 'F8V203-06GLD-AP',
quantity : '1',
itemPrice : '8.29',
totalPrice : '8.29'
},
{
id : '321207',
name : 'Motorola Talkabout GMRS\/FRS 2-Way Radios With 20-Mile Range (EM1000R)',
partNumber : 'EM1000R',
mfgPartNumber : 'EM1000R',
quantity : '1',
itemPrice : '44.09',
totalPrice : '44.09'
}
]
};
// adding cart items
for (var i = 0; i < data.products.length; i++) {
var jf_item = new Object();
jf_item.mpi = data.products[i].id;
jf_item.price = data.products[i].itemPrice;
jf_item.quantity = parseInt(data.products[i].quantity, 10);
// add cart item
jf_purchased_items.push (jf_item);
}
var str = "";
for (i = 0; i < jf_purchased_items.length; i++) {
str += YAHOO.lang.JSON.stringify(jf_purchased_items[i]);
str += "\n";
}
alert(str);
</script>
</body>
</html>

[edited by: Fotiman at 1:50 pm (utc) on Nov. 19, 2009]

camaroboy

4:31 am on Nov 19, 2009 (gmt 0)

10+ Year Member



If the product info says:

var data = {products : [{id : '290652'

then this:


jf_item.mpi = data.products[i].id;

should say this:


jf_item.mpi = '290652';

I just need to make that to happen.

Fotiman

1:54 pm on Nov 19, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Maybe I'm not understanding what you are asking. In my example, jf_item.mpi will be equal to the id value. As in:

jf_item.mpi = data.products[i].id;
// jf_item.mpi == '29065';

You can verify it by adding alert(jf_item.mpi); just after the assignment. So I'm not sure what you're asking for.

camaroboy

5:05 am on Nov 20, 2009 (gmt 0)

10+ Year Member



The javascript on my product page is this:


<!–Begin cashback Tracking Pixel Code -->
<script type='text/javascript'>
<!--
var jf_merchant_id = 'YOUR_CASHBACK_SHOPPING_HASHED_MERCHANT_ID?';
var jf_merchant_order_num = '<%Order.Number%>';
var jf_purchased_items = new Array();
// product information data
var data = <%Order.ProductsObject%>;
// adding cart items
for (var i = 0; i < data.products.length; i++) {
var jf_item = new Object();
jf_item.mpi = data.products[i].id;
jf_item.price = data.products[i].itemPrice;
jf_item.quantity = data.products[i].quantity;
// add cart item
jf_purchased_items.push (jf_item);
}
//-->
</script>
<script type='text/javascript' src= 'https://ssl.bing.com/cashback/javascripts/1x1tracking.js'>
</script>
<!--End Cashback Tracking Pixel Code -->

When an order is placed the confirmation page gives this code:


<script type='text/javascript'>
<!--
var jf_merchant_id = 'YOUR_CASHBACK_SHOPPING_HASHED_MERCHANT_ID?';
var jf_merchant_order_num = '22272';
var jf_purchased_items = new Array();
// product information data
var data = {products : [{id : '290652', name : 'Belkin 6\' 3.5mm Stereo Audio Cable - Male To Male (F8V203-06GLD-AP)', partNumber : 'F8V203-06GLD-AP', mfgPartNumber : 'F8V203-06GLD-AP', quantity : '1', itemPrice : '8.29', totalPrice : '8.29' },{id : '321207', name : 'Motorola Talkabout GMRS\/FRS 2-Way Radios With 20-Mile Range (EM1000R)', partNumber : 'EM1000R', mfgPartNumber : 'EM1000R', quantity : '1', itemPrice : '44.09', totalPrice : '44.09' }]};
// adding cart items
for (var i = 0; i < data.products.length; i++) {
var jf_item = new Object();
jf_item.mpi = data.products[i].id;
jf_item.price = data.products[i].itemPrice;
jf_item.quantity = data.products[i].quantity;
// add cart item
jf_purchased_items.push (jf_item);
}
//-->
</script>
<script type='text/javascript' src= 'https://ssl.bing.com/cashback/javascripts/1x1tracking.js'>
</script>

So I guess my question is what do I need to change about the first code so that the second code will fill in the jf_item.mpi values?

Fotiman

3:53 pm on Nov 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



As I said above, it does not appear that anything needs to be changed. It works in the example you've given, as can be seen by simply looping through the jf_puarchased_items array.