Close

Custom Rules

IvanSlater
10 years ago
#8030 Quote
Avatar
  • 89
Hi there!

Is possible to create custom rules?

I want to schedule mails for some especifics situations based on procuts, shipments and others attributes.

Thanks!
hristian.dimov
10 years ago
#8036 Quote
Avatar
  • Moderator
  • 386
IvanSlater wrote:
Hi there!

Is possible to create custom rules?

I want to schedule mails for some especifics situations based on procuts, shipments and others attributes.

Thanks!


Hi IvanSlater,

Yes, it is possible to create a custom reminder rules.

Basically, you just need to implement the IReminderRule inferface.

When you do this, your rule will appear in the reminder rule dropdown in the administration.

Hope this helps!
Regards,
Hristian Dimov
Nop-Templates.com
IvanSlater
10 years ago
#8040 Quote
Avatar
  • 89
Hi Hristian,

thanks a lot. Do you have a simple sample? I need to create another plugin, a "rule plugin", is it right?

EDIT:

I have created a new plugin with all my new rules. Now, can you say more about both interface methods:

- GetAllowedTokens
- GetCustomerReminderInfos

I guess they are called at each task call, am I right? The CustomerReminderInfo list returned are all items that will received the message?

And about the tokens, if I create a new one, where it have to be replaced? Or is not possible to have new ones?


Thanks!
hristian.dimov
10 years ago
#8045 Quote
Avatar
  • Moderator
  • 386
IvanSlater wrote:
Hi Hristian,

thanks a lot. Do you have a simple sample? I need to create another plugin, a "rule plugin", is it right?

EDIT:

I have created a new plugin with all my new rules. Now, can you say more about both interface methods:

- GetAllowedTokens
- GetCustomerReminderInfos

I guess they are called at each task call, am I right? The CustomerReminderInfo list returned are all items that will received the message?

And about the tokens, if I create a new one, where it have to be replaced? Or is not possible to have new ones?


Thanks!


Yes, you can done it with a plugin.

Example of The CustomerReminderInfos method:

public IList<CustomerReminderInfo> GetCustomerReminderInfos(TimeSpan conditionMetDataEarlierThan, TimeSpan conditionMetDateLaterThan, int storeId)
{  
  var customerReminderInfos = new List<CustomerReminderInfo>();

  //perform your logic for selecting the data that you need
  IList<global::Nop.Core.Domain.Customers.Customer> inactiveCustomers = GetInactiveCustomers(conditionMetDataEarlierThan, conditionMetDateLaterThan);

  foreach(var customer in inactiveCustomers)
  {
    var customerReminderInfo = new CustomerReminderInfo()
                     {
                       Customer = customer,
                       ReminderMessageId = customer.Id,
                       RuleConditionMetDate = customer.CreatedOnUtc,
                       Tokens = GetTokens(customer), // or you can leave it as "new List<Token>()"
                       StoreId = storeId
                     };

    customerReminderInfos.Add(customerReminderInfo);
  }

  return customerReminderInfos;
}

private IList<Token> GetTokens(Customer customer)
{
  var tokens = new List<Token>();

  tokens.Add(new Token("Customer.FullName", customer.GetFullName()));

  return tokens;
}


and yes, you can create new tokens, but you must evaluate them in the GetTokens method, just like the example above.

GetAllowedTokens method example:

public IList<string> GetAllowedTokens()
{
  var allowedTokens = new List<string>()
              {
                "%Customer.FullName%"
              };

  return allowedTokens;
}


Hope this helps!
Regards,
Hristian Dimov
Nop-Templates.com
IvanSlater
10 years ago
#8047 Quote
Avatar
  • 89
Thanks!

Last question.

Do I have to set an exclusive Guid for the Id property or I can use return new Guid() ?
hristian.dimov
10 years ago
#8048 Quote
Avatar
  • Moderator
  • 386
IvanSlater wrote:
Thanks!

Last question.

Do I have to set an exclusive Guid for the Id property or I can use return new Guid() ?


You can use the visual studio built-in tool for creating a new GUID. Just go to Tools -> Create Guid.

It will generate you a GUID, which you can place it in the constructor, example:

public Guid Id
{
  get
  {
    return new Guid("3BF7F2D0-2907-4CBA-8A51-57576D490F60"); // you can replace it with your generated guid
  }
}

Regards,
Hristian Dimov
Nop-Templates.com
nicolasmuniere
10 years ago
#8096 Quote
Avatar
  • 1
Hi, I have two questions :

- Is it possible to implement a custom rule in app_code folder? I would like to avoid creating a plugin just for that.

- Could you provide the linq query that is used in UnpaidOrdersReminderRule, because I need to create UnpaidByCheckOrdersReminderRule and I'm not able to start the query.

Thanks
Nicolas
hristian.dimov
10 years ago
#8099 Quote
Avatar
  • Moderator
  • 386
nicolasmuniere wrote:
Hi, I have two questions :

- Is it possible to implement a custom rule in app_code folder? I would like to avoid creating a plugin just for that.

- Could you provide the linq query that is used in UnpaidOrdersReminderRule, because I need to create UnpaidByCheckOrdersReminderRule and I'm not able to start the query.

Thanks
Nicolas


Hi Nicolas,

You can implement a rule without creating a plugin, but you will need to register you rule in some DependencyRegistrar file.

Example:

builder.RegisterType<UnpaidByCheckOrdersReminderRule>().As<IReminderRule>().InstancePerLifetimeScope();


About the UnpaidOrdersReminderRule query, please submit a ticket.
Regards,
Hristian Dimov
Nop-Templates.com
IvanSlater
10 years ago
#8407 Quote
Avatar
  • 89
I have created a plugin, but my new reminders are not displayed in Reminder Rule list.

My plugin has 2 classes inside Services folder. Each for a new reminder I want.

Is the DependencyRegistrar file needed? How to create it, like this?


        public virtual void Register(ContainerBuilder builder, ITypeFinder typeFinder)
        {
            builder.RegisterType<Reminder1Service>().As<IReminderRule>().InstancePerHttpRequest();
            builder.RegisterType<Reminder2Service>().As<IReminderRule>().InstancePerHttpRequest();
        }


My plugin class is public class NewReminderRulesPlugin : BasePlugin, IMiscPlugin.
Does it need some extra code?

Thanks!!!
iliyan.tanev
10 years ago
#8411 Quote
Avatar
  • 347
Hi,

Yes, you need to register it in the DependencyRegister.
Maybe it should be InstancePerLifetimeScope.

Regarding your plugin. No, this should be enough.
Regards,
Iliyan Tanev
Nop-Templates Dev Team