Custom macro not getting resolved in email template

Jon Tetzlaff asked on May 25, 2016 18:02

I have an order email confirmation that gets sent out with a transformation used to show products. You can look at my code below

Email Template snippet:

{% Order.OrderItems.ApplyTransformation("custom.Transformations.OrderConfirmationLineItem") #%}

Then within that transformation I am trying to use a custom macro I created, but I can't seem to get the custom macro to work. I've used the system console app to see if my macro was being loaded into the global macro resolver and it works fine in there, but can't figure out why it doesn't work in my transformation.

Below is the custom macro I'm using in my transformation. OrderItemText is where the SKUId is being stored.

Transformation:

{% Custom.GetSampleSKU(OrderItemText).SKUName #%}

Here is my code for registering the custom macro:

[MacroNamespaceLoader]
public partial class CMSModuleLoader
{
    /// <summary>
    /// Attribute class that ensures the registration of custom macro namespaces.
    /// </summary>
    private class MacroNamespaceLoaderAttribute : CMSLoaderAttribute
    {
        /// <summary>
        /// Called automatically when the application starts.
        /// </summary>
        public override void Init()
        {
            // Registers "Custom" into the macro engine
            MacroContext.GlobalResolver.SetNamedSourceData("Custom", CustomMacroNamespace.Instance);
        }
    }
}

Here is my custom macro:

[assembly: RegisterExtension(typeof(CustomMacroMethods), typeof(CustomMacroNamespace))]
public class CustomMacroMethods : MacroMethodContainer
{
    [MacroMethod(typeof(SKUInfo), "Gets the SKU of a sample product", 1)]
    [MacroMethodParam(0, "param1", typeof(string), "SKUID of the product")]
    public static object GetSampleSKU(EvaluationContext context, params object[] parameters)
    {
        int id = 0;
        // Branches according to the number of the method's parameters
        switch (parameters.Length)
        {
            case 1:
                id = ValidationHelper.GetInteger(parameters[0], 645);
                break;
            case 2:
                id = ValidationHelper.GetInteger(parameters[1], 645);
                break;
            default:
                // No other overloads are supported
                throw new NotSupportedException();
        }

        return SKUInfoProvider.GetSKUInfo(id);
    }
}

Correct Answer

Jon Tetzlaff answered on June 9, 2016 19:34

Turns out the custom order info provider I was using needed to add my macro provider to it's resolver. After I added that the macro was resolved in my email.

0 votesVote for this answer Unmark Correct answer

Recent Answers


Virgil Carroll answered on May 25, 2016 23:59

Jon,

Where there is not enough information above to let us look at your code well, my first question would be if you remove the custom macro and just try to resolve the OrderItemText field in your transformation does that show the value you are expecting? Also since this is a text field and not an integer field yet you are storing an integer in it (so you explain above), are you making sure your code is seeing it as an integer to grab the SKUName?

Also why are you storing the SKUID in the OrderItemText field vs just using the OrderItemSKUID field? Maybe I am just missing something.

0 votesVote for this answer Mark as a Correct answer

Jon Tetzlaff answered on May 26, 2016 14:31

@Virgil I have tried just outputing the OrderItemText field and that works just fine, but the macro resolves to nothing. I convert the string to an interger when using the SKUInfoProvider class. We are storing a reference to another SKU within that field and need to grab details of it in the email template.

What's strange to me is that it outputs nothing at all, like it never calls the method or throws an error. I'll add some more code to my original question.

0 votesVote for this answer Mark as a Correct answer

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