Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

Data Element in a for loop with if statement

EstherV
Level 1
Level 1

Hi Community,

I'm currently using some custom code in a Data Element to return a price range of a specific product.

This code is working, I'm already using it on pages with a single product.

var productPrice= window.tc_vars.product_unitprice_ttc;
var tranche;

switch (true) {
  case (0 <= productPrice && productPrice < 299):
    tranche = "/Less than 299€";
    break;
  case (299 <= productPrice && productPrice < 301):
    tranche = "/Between 299 and 300€";
    break;
}

 

I would like to use this data element on my basket page where I have multiple products and stock the returned value in an eVar:

s.products = "";

for (var i = 0; i < window.tc_vars.basket_products.length; i++) {
  s.products +=
    /*window.tc_vars.basket_products[i].product_cat2_name +*/ ";" +
    window.tc_vars.basket_products[i].product_id +
    ";"

  if (
    window.tc_vars.basket_products[i].product_unitprice_ttc &&
    window.tc_vars.basket_products[i].product_unitprice_ttc !== ""
  ) {
    s.products +=
      "|eVar34=" + "HOW CAN I USE MY DATA ELEMENT HERE?";
  }
}

 

My goal is to use the formula stocked in my Data Element for every product in my basket. But I'm not sure how to use my data element here.

I know that this is not working:

s.products += "|eVar34=" + window.tc_vars.basket_products[i]._satellite.getVar('ProductPriceTranche');

I tried the following :

s.products += "|eVar34=" + _satellite.getVar('ProductPriceTranche');

It's returning an empty string.

I can't find how to fill in the product index dinamically in this line of code.

 

Any suggestions here ?

Thanks !

3 Replies
Andrey_Osadchuk
Community Advisor
Community Advisor

Your last code sample is how a DE should be referenced

 

s.products += "|eVar34=" + _satellite.getVar('ProductPriceTranche');

 

 

The DE code should return a value

 

return 'value'

 

Swanan_
Level 3
Level 3

Hi @Andrey_Osadchuk

 

The DE is actually returning a value we forgot to add the last line. But the DE is actually returning the var tranche

return tranche;

So if I'm understanding well your comment, the DE reference is correct so I just need to do some test with some other products ?