How to add new message tokens to be used in message templates?

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
12 years ago
Hi Guys,

First of all, let me say that although I am new to nopCommerce I can already find my way around it and the reason for that is because it is a great product!
But I have a question related to message tokens. How can I add new message tokens in order to use them in my message templates? I would like to add %OrderNodes% to the order shipped message.
Help, guidance, anything at all is greatly appreciated!

Thanks
12 years ago
There is no %OrderNotes% token by default but you can add one in the following way:

1. Create a MessageTokenProvider which inherits the Nop.Services.Messages.MessageTokenProvider class

2. In your MessageTokenProvider override the AddOrderTokens method like this:

        
public override void AddOrderTokens(IList<Token> tokens, Order order, int languageId)
        {
            string orderNotesHtml = BuildOrderNotesHtml(order, languageId);
            tokens.Add(new Token("Order.OrderNotes", orderNotesHtml, true));

            base.AddOrderTokens(tokens, order, languageId);
        }


Note that you will have to implement the BuildOrderNotesHtml method in a way as to meet your requirements for how the notes should look like in the message.

3. Create a DependencyRegistrar class, which implements the IDependencyRegistrar interface.
Set the Order property to some number greater than zero, for example 60, so that your DependencyRegistrar is called before the default one.

4. In the your DependecyRegistrar.Register method register your MessageTokenProvider class like this:

        
public void Register(ContainerBuilder builder, ITypeFinder typeFinder)
        {
            builder.RegisterType<MessageTokenProvider7Spikes>().As<IMessageTokenProvider>().InstancePerHttpRequest();
        }


Please note that all this it is preferable to do from a plugin, and not directly in the nopCommerce code. For how to create a plugin please check the nopCommerce documentation:

https://www.nopcommerce.com/docs/77/how-to-write-a-nopcommerce-plugin.aspx

Hope this is useful!
12 years ago
Awesome stuff! It will save me a lot of time.
Thank you for your detailed answer!
11 years ago
7Spikes wrote:
There is no %OrderNotes% token by default but you can add one in the following way:

...
4. In the your DependecyRegistrar.Register method register your MessageTokenProvider class like this:

        
public void Register(ContainerBuilder builder, ITypeFinder typeFinder)
        {
            builder.RegisterType<MessageTokenProvider7Spikes>().As<IMessageTokenProvider>().InstancePerHttpRequest();
        }


https://www.nopcommerce.com/docs/77/how-to-write-a-nopcommerce-plugin.aspx

Hope this is useful!


This works only with the highest priority plugin.

I think this is simpler (add EntityTokensAddedEventConsumer.cs to plugin):



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Nop.Core.Domain.Messages;
using Nop.Core.Domain.Orders;
using Nop.Services.Events;
using Nop.Services.Messages;

namespace Nop.Plugin.Payments.KandHPaymentGateway
{
    public class EntityTokensAddedEventConsumer : IConsumer<EntityTokensAddedEvent<Order,Token>>
    {
        public void HandleEvent(EntityTokensAddedEvent<Order,Token> eventMessage)
        {
            eventMessage.Tokens.Add(new Token("Order.AuthorizationTransactionId", eventMessage.Entity.AuthorizationTransactionId));
            eventMessage.Tokens.Add(new Token("Order.AuthorizationTransactionCode", eventMessage.Entity.AuthorizationTransactionCode));
            eventMessage.Tokens.Add(new Token("Order.AuthorizationTransactionResult", eventMessage.Entity.AuthorizationTransactionResult));
        }
    }
}
10 years ago
In order to make create a new custom message you have to add tokens as well.

Could some one explain this a little more specific, wht to do etc?


Thanks


//Chris
9 years ago
And how to add new message templates to nop, by plugin?
9 years ago
I think I did it.. I will test..
4 years ago
what is MessageTokenProvider7Spikes in dependency register page?
4 years ago
sksoni wrote:
what is MessageTokenProvider7Spikes in dependency register page?


It might be custom message token provider service by Seven Spike plugin. may be developed by The NopTemplate .
4 years ago
sksoni wrote:
what is MessageTokenProvider7Spikes in dependency register page?

This should be part of nopTemplate's plugin's part.

Can you write in detail what issue you have, so the community can help on the same?
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.