API Questions on Kentico API.
Version 5.x > API > Custom Macro Product List HTML Encoding View modes: 
User avatar
Member
Member
kj.rehsi-wearei3 - 4/16/2013 11:10:10 AM
   
Custom Macro Product List HTML Encoding
Hi Guys,

I wonder if somebody here can help me out.

I have a created a custom macro for Product List for my invoice. My macro works fine and returns the HTML table with the desired value.

But the issue is when the invoice is generated it outputs as follows:
<table width="100%" style="text-align: right" cellspacing="0" cellpadding="2" class="productsList"> <thead><tr><th colspan="6" class="headerBorder"> </th></tr><tr><th style="text-align: left;padding-left:2px">InvoiceTemplate.skuNumber </th> <th>Product name </th> <th>Units </th> <th>Price/unit </th> <th>Discount/unit </th> <th>Tax </th> <th>Subtotal </th></tr></thead><tbody><tr> <td style="text-align: left">CEFT2 </td> <td>SINELCO CRIMP TOOL CORD END 4 TO 16MM </td> <td>1 </td> <td>21.00 </td> <td>0.00 </td> <td>4.20 </td> <td>25.20 </td></tr> <tr> <td colspan="6" class="bottomBorder">  </td> </tr> </tbody></table>

When I debug the code, it can clearly be seen that the HTML generated by the macro is decoded. The rest of the invoice is fine except the product list itself.

I have tried the following:
sender.AllowParameters = false;

sender.EncodeSpecialMacros = true;

but no luck whatsoever.

for this
sender.AllowParameters = false;
it complains no defination found for 'AllowParameters'.

Please advice?

How do I encode the HTML so that I can see the actual table format of the product list instead of the raw html?

User avatar
Kentico Consulting
Kentico Consulting
richards@kentico.com - 4/17/2013 1:00:55 AM
   
RE:Custom Macro Product List HTML Encoding
Hi,

the "sender.AllowParameters = false" is the right way to go here. I'm wondering why is this causing you issues. Could you post your macro here? Maybe along with the screnshot of the page you are using it in?

You should pass this property of the sender object in your custom macro right before you return the final string.

Edit: I have also find this topic regarding the very same issue. The solution indeed was to use of this property right before the text is returned.

Kind regards,
Richard Sustek

User avatar
Member
Member
kj.S1ngh - 4/17/2013 3:40:21 AM
   
RE:Custom Macro Product List HTML Encoding
Thanks Richard for the reply.

Here is my custom macro:
 case "productlist":
match = true;
result = "";

sender.AllowParameters = false;

//Get Shopping Cart Object from resolver
CMS.Ecommerce.ShoppingCartInfo cartObj3 = sender.SourceObject as CMS.Ecommerce.ShoppingCartInfo;

bool renderDiscount = true;
CMS.Ecommerce.CurrencyInfo currency = CMS.Ecommerce.CurrencyInfoProvider.GetCurrencyInfo(cartObj3.ShoppingCartCurrencyID);
StringBuilder sb = new StringBuilder();

// Header
sb.Append(" <table width=\"100%\" style=\"text-align: right\" cellspacing=\"0\" cellpadding=\"2\" class=\"productsList\">");
sb.Append(" <thead><tr><th colspan=\"" + (5 + (renderDiscount ? 1 : 0)) + "\" class=\"headerBorder\"> </th></tr><tr>");
sb.Append("<th style=\"text-align: left;padding-left:2px\">");
sb.Append(ResHelper.GetString("InvoiceTemplate.skuNumber", culture));
sb.Append(" </th>");
sb.Append(" <th>");
sb.Append(ResHelper.GetString("InvoiceTemplate.SKUName", culture));
sb.Append(" </th>");
sb.Append(" <th>");
sb.Append(ResHelper.GetString("InvoiceTemplate.SKUUnits", culture));
sb.Append(" </th>");
sb.Append(" <th>");
sb.Append(ResHelper.GetString("InvoiceTemplate.SKUPrice", culture));
sb.Append(" </th>");
if (renderDiscount)
{
sb.Append(" <th>");
sb.Append(ResHelper.GetString("InvoiceTemplate.UnitDiscount", culture));
sb.Append(" </th>");
}
sb.Append(" <th>");
sb.Append(ResHelper.GetString("InvoiceTemplate.Tax", culture));
sb.Append(" </th>");
sb.Append(" <th>");
sb.Append(ResHelper.GetString("InvoiceTemplate.Subtotal", culture));
sb.Append(" </th>");
sb.Append("</tr></thead><tbody>");

double value = 0.0;

DataTable dt = cartObj3.ShoppingCartContentTable;

//Add the items
foreach (DataRow dr in dt.Rows)
{
//Get SKUName
string name = ResHelper.RegExLocalize.Replace(Convert.ToString(DataHelper.GetDataRowValue(dr, "SKUName")), LocalizedStringMatch);

string number = ResHelper.RegExLocalize.Replace(Convert.ToString(DataHelper.GetDataRowValue(dr, "skuNumber")), LocalizedStringMatch);

// If it is a product option
if (ValidationHelper.GetGuid(DataHelper.GetDataRowValue(dr, "CartItemParentGuid"), Guid.Empty) != Guid.Empty)
{
name = "  - " + name;
}

sb.Append("<tr>");
sb.Append(" <td style=\"text-align: left\">");
sb.Append(number);
sb.Append(" </td>");
sb.Append(" <td>");
sb.Append(name);
sb.Append(" </td>");
sb.Append(" <td>");
sb.Append(Convert.ToString(DataHelper.GetDataRowValue(dr, "SKUUnits")));
sb.Append(" </td>");
sb.Append(" <td>");
value = ValidationHelper.GetDouble(DataHelper.GetDataRowValue(dr, "SKUPrice"), 0);
sb.Append(CMS.Ecommerce.CurrencyInfoProvider.GetFormatedValue(value, currency));
sb.Append(" </td>");
if (renderDiscount)
{
sb.Append(" <td>");
value = ValidationHelper.GetDouble(DataHelper.GetDataRowValue(dr, "UnitDiscount"), 0);
sb.Append(CMS.Ecommerce.CurrencyInfoProvider.GetFormatedValue(value, currency));
sb.Append(" </td>");
}
sb.Append(" <td>");
value = ValidationHelper.GetDouble(DataHelper.GetDataRowValue(dr, "Tax"), 0);
sb.Append(CMS.Ecommerce.CurrencyInfoProvider.GetFormatedValue(value, currency));
sb.Append(" </td>");
sb.Append(" <td>");
value = ValidationHelper.GetDouble(DataHelper.GetDataRowValue(dr, "Subtotal"), 0);
sb.Append(CMS.Ecommerce.CurrencyInfoProvider.GetFormatedValue(value, currency));
sb.Append(" </td>");
sb.Append("</tr>");
}
sb.Append(" <tr>");
sb.Append(" <td colspan=\"" + (5 + (renderDiscount ? 1 : 0)) + "\" class=\"bottomBorder\"> ");
sb.Append(" </td>");
sb.Append(" </tr>");
sb.Append(" </tbody>");
sb.Append("</table> \n");

result = sb.ToString();
break;

There is a error for this line
sender.AllowParameters = false;
basically complains thats CMS.GlobalHelper.MacroResolver does not contain a definition for 'AllowParameters'.

User avatar
Member
Member
kj.S1ngh - 4/17/2013 3:43:39 AM
   
RE:Custom Macro Product List HTML Encoding
Also my macro is placed inside
App_Code/Global/CMS/CMSCustom.cs

I checked the 'MacroResolver' class and there is no method called AllowParameter.

User avatar
Kentico Consulting
Kentico Consulting
Kentico_RichardS - 4/17/2013 5:59:25 AM
   
RE:Custom Macro Product List HTML Encoding
Well, this is quite strange. Im unable to reproduce this issue on Kentico 7 instance (it was probably made to work better).

Have you included the GlobalHelper in your Custom macro file at "App_Code/Global/CMS/CMSCustom.cs"?

There has to be an
using CMS.GlobalHelper;

In the beginning of your file.

I would also suggest you to apply some hotfixes or even upgrade to a newer version.

Kind regards,
Richard Sustek

User avatar
Member
Member
kj.S1ngh - 4/17/2013 6:11:23 AM
   
RE:Custom Macro Product List HTML Encoding
Yes
using CMS.GlobalHelper;
namespace is present in the class.

Unfortunately open to upgrade is not an option for me. So have to try a workaround this.

Although what about if I replicated this method GetProductList() to say GetProductListInvoice() ?


public string GetProductList(DataTable dt, object currency, bool renderDiscount)
{
return OrderInfoProvider.GetProductList(dt, (CMS.Ecommerce.CurrencyInfo)currency, renderDiscount);
}


Basically I am trying to add an additional column/row to the product list which is eventually printed out into the invoice.

So far I have been able to add the 'skuNumber' column into the datatable (dt) but also want to include it into the HTML.

I know this
return OrderInfoProvider.GetProductList(dt, (CMS.Ecommerce.CurrencyInfo)currency, renderDiscount); 
returns the HTML string. So I was thinking of parsing the HTML string and adding my additional column etc?

What do you suggest?

User avatar
Kentico Consulting
Kentico Consulting
Kentico_RichardS - 4/17/2013 7:01:41 AM
   
RE:Custom Macro Product List HTML Encoding
Hi,

Yeah, that is good idea. I have now also been thinking that you could do this by customizing the current function which is responsible for displaying the additional column.

Could you tell me where in CMS desk do you want this additional column to be displayed? I would check if it is possible to just customize it.

Kind regards,
Richard Sustek

User avatar
Member
Member
kj.S1ngh - 4/17/2013 7:06:42 AM
   
RE:Custom Macro Product List HTML Encoding
Hi,

I want the additional column i.e. skuNumber / item number to appear in the productlist column. I have been able to add the skuNumber to the datatable in the GetProductList() method after following this link

Only thing left is to add the skuNumber to my product list which is displayed on the invoice.

Hope this helps?

User avatar
Kentico Consulting
Kentico Consulting
Kentico_RichardS - 4/17/2013 7:35:50 AM
   
RE:Custom Macro Product List HTML Encoding
Hi,

To get the clear picture I may need more information.

So to clarify: You want to display additional custom field value in CMS Desk -> Tools -> E-commerce -> Configuration -> Invoice. Is that Correct? Have you added the field in Site Manager -> Development -> System Tables -> Order ?

This is how I understannd your issue. Its generally easy to add new fields to the invoice, but back in the 5 version there is no support for this. But if this is your case you can simply change the invoice and call some custom Macro method with OrderID and then get the value of your field in it and return it.

Kind regards,
Richard Sustek


User avatar
Member
Member
kj.S1ngh - 4/17/2013 8:32:24 AM
   
RE:Custom Macro Product List HTML Encoding
Hi Richard,

I will explain my situation again.

I have a invoice which sent to a customer when a quote is requested or an order is completed.

The invoice is present inside the location you suggested i.e.
CMS Desk -> Tools -> E-commerce -> Configuration -> Invoice


Inside the invoice there is a standard macro used called #PRODUCTLIST# it outputs the value into the invoice as follows:



Product name Units Price/unit Tax Subtotal

KLIPPON ADK1 TERMINAL 61 0.57 6.95 41.72

KLIPPON AP END PLATE 5 0.38 0.38 2.28



Now the customer would like the product list in the invoice to include 'Product Code' or Item Number as follows:



Product Code Product name Units Price/unit Tax Subtotal

121323 KLIPPON ADK1 TERMINAL 61 0.57 6.95 41.72

121514 KLIPPON AP END PLATE 5 0.38 0.38 2.28



Hence I made a new custom macro as mentioned above which is bringing the field back but the value in the invoice is printed out in raw HTML.

Hope this helps?

User avatar
Kentico Consulting
Kentico Consulting
Kentico_RichardS - 4/18/2013 3:40:13 AM
   
RE:Custom Macro Product List HTML Encoding
Hi,

I found something regarding the macro encoding.

You can use the encode macro function. For example:

{%macro|(encode)false%}


Could you please try that?

Kind regards,
Richard Sustek

User avatar
Member
Member
kj.S1ngh - 4/18/2013 3:46:51 AM
   
RE:Custom Macro Product List HTML Encoding
Hi Richard,

I have tried that aswell. But no luck with that :(

Thanks

User avatar
Kentico Consulting
Kentico Consulting
Kentico_RichardS - 4/18/2013 5:18:01 AM
   
RE:Custom Macro Product List HTML Encoding
Hi,

Finally I have been able to resolve this and It has been such a unfortunate mistake... The thing is that you have to use the
ender.AllowParameters = false;


right before the
Return


Here is an examle how it can look like:

   public static string ResolveCustomMacro(MacroResolver sender, string expression, out bool match)
{
match = false;
string result = expression;

// Add your custom macro evaluation

switch (expression.ToLower())
{
case "test":
match = true;
result = "<td><td>test<td>it is encoding correctly</table>";
break;
}

sender.AllowParameters = false;
return result;
}


Please let me know if that is working for you.

Kind regards,
Richard Sustek

User avatar
Member
Member
kj.S1ngh - 4/18/2013 5:43:22 AM
   
RE:Custom Macro Product List HTML Encoding
Hi Richard,

I had tried that aswell.

My CMS.GlobalHelper.MacroResolver doesn't contain definition for 'AllowParameters'.

Thanks

User avatar
Kentico Consulting
Kentico Consulting
Kentico_RichardS - 4/18/2013 5:50:16 AM
   
RE:Custom Macro Product List HTML Encoding
Hi,

Could you please send me an email to support@kentico.com with the attachment of your CMSCustom.cs file so I can take a look at it?

Kind regards,
Richad Sustek

User avatar
Member
Member
kj.S1ngh - 4/18/2013 8:16:55 AM
   
RE:Custom Macro Product List HTML Encoding
Hi Richard,

I have sent the email with my CMSCustom.cs file.

Regards,
KJ