Macro Checkout Array

Les Girvan asked on February 24, 2021 15:07

Hi Guys,

Just looking for help in formatting a macro results, I have this for loop for current shopping cart and want to create an array with bracket separators between the items in the basket, so looking to get a result like:

"ItemNames": ["Winnie the Pooh", "A Tale of Two Cities"],

I have setup this macro :

"ItemNames": ["{% foreach(cartItem in ECommerceContext.CurrentShoppingCart.CartProducts){+ cartItem.SKU.SKUName + ","} #%}"],

But cannot figure out how to apply to be able to get the brackets to enclosed each item, currently it only does the whole string:

"ItemNames": ["Winnie the Pooh, A Tale of Two Cities"],

Can you advise how I can change this so it works as I require please?

Regards Les

Correct Answer

Juraj Ondrus answered on March 1, 2021 07:17

If the macro is getting this complicated, the best practice is to create a custom macro which will output the desired values in desired format. K# macros are pretty powerful but not almighty.
The open macro conditions are used when you need to add e.g. some HTML code based on some conditions, break the macro between lines of text, so the return command is not really applicable here.

0 votesVote for this answer Unmark Correct answer

Recent Answers


Juraj Ondrus answered on February 25, 2021 08:48

I would try using the open conditions and loops syntax.
Maybe something like this: "ItemNames": [{% foreach (cartItem in ECommerceContext.CurrentShoppingCart.CartProducts) { %} "{%cartItem.SKU.SKUName%}", {%} %} ],
Plus you may want to handle the last item somehow, so there is not the comma at the end and also, I am not sure if you will need to encode the double quotes (") and comma (,) too.

1 votesVote for this answer Mark as a Correct answer

Les Girvan answered on February 27, 2021 13:13

Hi Juraj, many thanks for that answer, it has resolved that issue, however, I an struggling to understand the logic of where/when you place the {%} to enclose the items of the foreach, can you advise please.

The reason to ask is, as part of this Javascript, I also need to add a line to pull in all the items of the cart, plus image, SKUID, url path, price etc.

The current settings I have is:

{% additems = foreach(cartItem in ECommerceContext.CurrentShoppingCart.CartProducts) { "ProductName:" + cartItem.SKU.SKUName + "," + "ProductID: " + cartItem.SKU.SKUID + "," + "SKU:" + cartItem.SKU.SKUName + "," + "Quantity: " + cartItem.SKUUnits + "," + "ItemPrice: " + cartItem.SKU.SKUPrice + "," + "RowTotal:" + cartItem.SKU.ItemTotal + "," + "ImageURL:" + cartItem.SKU.SKUIMagePath + "," + "ProductURL:" + cartItem.SKU.SKUAliasPath + "," } return; #%}  

As you can see we also need to include text identifiers, so have been trying with just a few items until I can figure out the structure required:

{% additems = foreach(cartItem in ECommerceContext.CurrentShoppingCart.CartProducts) { %} "ProductName:""{% cartItem.SKU.SKUName %}, "ProductID: "{% cartItem.SKU.SKUID %}", {%} %} }return; #%}  

But this is throwing errors plus the first item is repeated:

    "items":[" "ProductName:""Siena Lady GTX Shoe: BRAUN - 5H, "ProductID: "7337", 
 "ProductName:""Siena Lady GTX Shoe: BRAUN - 5H, "ProductID: "7337",  "ProductName:""Rab Mens Microlight Vest Black/Shark: Black/Shark - S, "ProductID: "23045", "],
    }]);

I have tried searching for examples of strings like this but cannot find any?

Thanks for you advise on this.

Les

0 votesVote for this answer Mark as a Correct answer

Les Girvan answered on February 28, 2021 13:14

Hi Juraj,

I have been tweaking the structure and have resolved the basket items now

"Items": [{% foreach(cartItem in ECommerceContext.CurrentShoppingCart.CartProducts) { %} "Product Name:{%cartItem.SKU.SKUName#%}", "Item Price:{%cartItem.UnitPrice#%}", "Quantity: {%cartItem.SKUUnits#%}", "Row Total:{%cartItem.TotalPrice#%}", {%} #%}],

I did want to do this as an array and pass in a variable, but unable to figure out where to put the 'return' to hide the function and pass the variable, this is what I have:

{% additems2 = foreach(cartItem in ECommerceContext.CurrentShoppingCart.CartProducts) { %} "Product Name:{%cartItem.SKU.SKUName#%}", "Item Price:{%cartItem.UnitPrice#%}", "Quantity: {%cartItem.SKUUnits#%}", "Row Total:{%cartItem.TotalPrice#%}", {%}  #%} return;

you can advise, also have two other issues, I can't get the product image and product page URL's into the string and also need to be able to set the pricing to 2 decimal points?

Thanks Les

0 votesVote for this answer Mark as a Correct answer

Les Girvan answered on March 3, 2021 14:14 (last edited on March 3, 2021 14:14)

Hi Juraj, thanks for your help and assistance with this, I have made progress. I am however, still strugling trying to get:

  1. Product url into string
  2. Adding the URL strings in the loop of items.

So for the image url I have added macro:

{% GetAbsoluteUrl(ECommerceContext.CurrentShoppingCart.ContentTable[0].SKUImagePath) #%}

Which works as the item, but cannot figure out how I add it to the foreach loop:

"Items": [{% foreach(cartItem in ECommerceContext.CurrentShoppingCart.CartProducts) { %} "Product Name:{% cartItem.SKU.SKUName#%}", "Item Price:{% cartItem.UnitPrice#%}", "Quantity: {% cartItem.SKUUnits#%}", "Row Total:{% cartItem.TotalPrice#%}", "Image Path:{% ContentTable.SKUIMagePath #%}", "Product URL:{% cartItem.SKUAliasPath#%}", {%} #%}],

I have also tried:

{%cartItem.SKUImagePath%}

{%cartItem.SKU.SKUImagePath%}

Going through the intelli-sense macro, I am not even seeing anything for the product URL path within the CartProducts choices, I assumed it would be SKUAliasPath?

but nothing I use seems to work, can you advise what I am missing please?

Regards Les

0 votesVote for this answer Mark as a Correct answer

Les Girvan answered on March 3, 2021 15:32

Just to advise, I have now figured out the Image URL, I have updated to:

"Items": [{% foreach(cartItem in ECommerceContext.CurrentShoppingCart.CartProducts) { %} "Product Name:{% cartItem.SKU.SKUName#%}", "Item Price:{% cartItem.UnitPrice#%}", "Quantity: {% cartItem.SKUUnits#%}", "Row Total:{% cartItem.TotalPrice#%}", "Image Path:{% GetAbsoluteUrl(ECommerceContext.CurrentShoppingCart.ContentTable[0].SKUImagePath) #%}", "Product URL:{% cartItem.SKUAliasPath#%}", {%} #%}],

Simply enclosing the {% GetAbsoluteUrl(ECommerceContext.CurrentShoppingCart.ContentTable[0].SKUImagePath) #%} in a loop array

Just need to find how to get the product Page URL now?

Les

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.