Data Element in a for loop with if statement | Community
Skip to main content
October 20, 2021
Solved

Data Element in a for loop with if statement

  • October 20, 2021
  • 1 reply
  • 2037 views

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 !

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Andrey_Osadchuk

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'

 

1 reply

Andrey_Osadchuk
Andrey_OsadchukAccepted solution
Level 10
October 20, 2021

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'

 

Level 4
October 21, 2021

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 ? 

 

Andrey_Osadchuk
Level 10
October 21, 2021

correct