Skip to content

Commit

Permalink
Add notification to vendor when order cancelled nopSolutions#7108
Browse files Browse the repository at this point in the history
  • Loading branch information
hsjalilian committed May 9, 2024
1 parent d31bc90 commit 3fa485b
Show file tree
Hide file tree
Showing 10 changed files with 201 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ public static partial class MessageTemplateSystemNames
/// </summary>
public const string ORDER_CANCELLED_CUSTOMER_NOTIFICATION = "OrderCancelled.CustomerNotification";

/// <summary>
/// Represents system name of notification vendor about cancelled order
/// </summary>
public const string ORDER_CANCELLED_VENDOR_NOTIFICATION = "OrderCancelled.VendorNotification";

/// <summary>
/// Represents system name of notification store owner about refunded order
/// </summary>
Expand Down
42 changes: 42 additions & 0 deletions src/Libraries/Nop.Data/Migrations/UpdateTo480/DataMigration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using FluentMigrator;
using Nop.Core.Domain.Messages;

namespace Nop.Data.Migrations.UpdateTo480
{
[NopMigration("2024-05-09 00:00:00", "4.80.0", UpdateMigrationType.Data, MigrationProcessType.Update)]
public class DataMigration : Migration
{
private readonly INopDataProvider _dataProvider;

public DataMigration(INopDataProvider dataProvider)
{
_dataProvider = dataProvider;
}

/// <summary>
/// Collect the UP migration expressions
/// </summary>
public override void Up()
{

//#7108 New message template
if (!_dataProvider.GetTable<MessageTemplate>().Any(st => string.Compare(st.Name, MessageTemplateSystemNames.ORDER_CANCELLED_VENDOR_NOTIFICATION, StringComparison.InvariantCultureIgnoreCase) == 0))
{
var eaGeneral = _dataProvider.GetTable<EmailAccount>().FirstOrDefault() ?? throw new Exception("Default email account cannot be loaded");
_dataProvider.InsertEntity(new MessageTemplate()
{
Name = MessageTemplateSystemNames.ORDER_CANCELLED_VENDOR_NOTIFICATION,
Subject = "%Store.Name%. Order #%Order.OrderNumber% cancelled",
Body = $"<p>{Environment.NewLine}<a href=\"%Store.URL%\">%Store.Name%</a>{Environment.NewLine}<br />{Environment.NewLine}<br />{Environment.NewLine}Order #%Order.OrderNumber% has been cancelled.{Environment.NewLine}<br />{Environment.NewLine}Customer: %Order.CustomerFullName%,{Environment.NewLine}<br />{Environment.NewLine}<br />{Environment.NewLine}<br />{Environment.NewLine}Order Number: %Order.OrderNumber%{Environment.NewLine}<br />{Environment.NewLine}Date Ordered: %Order.CreatedOn%{Environment.NewLine}<br />{Environment.NewLine}<br />{Environment.NewLine}%Order.Product(s)%{Environment.NewLine}</p>{Environment.NewLine}",
IsActive = true,
EmailAccountId = eaGeneral.Id
});
}
}

public override void Down()
{
//add the downgrade logic if necessary
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2263,6 +2263,13 @@ protected virtual async Task InstallMessageTemplatesAsync()
IsActive = true,
EmailAccountId = eaGeneral.Id
},
new() {
Name = MessageTemplateSystemNames.ORDER_CANCELLED_VENDOR_NOTIFICATION,
Subject = "%Store.Name%. Order #%Order.OrderNumber% cancelled",
Body = $"<p>{Environment.NewLine}<a href=\"%Store.URL%\">%Store.Name%</a>{Environment.NewLine}<br />{Environment.NewLine}<br />{Environment.NewLine}Order #%Order.OrderNumber% has been cancelled.{Environment.NewLine}<br />{Environment.NewLine}Customer: %Order.CustomerFullName%,{Environment.NewLine}<br />{Environment.NewLine}<br />{Environment.NewLine}<br />{Environment.NewLine}Order Number: %Order.OrderNumber%{Environment.NewLine}<br />{Environment.NewLine}Date Ordered: %Order.CreatedOn%{Environment.NewLine}<br />{Environment.NewLine}<br />{Environment.NewLine}%Order.Product(s)%{Environment.NewLine}</p>{Environment.NewLine}",
IsActive = true,
EmailAccountId = eaGeneral.Id
},
new() {
Name = MessageTemplateSystemNames.ORDER_PROCESSING_CUSTOMER_NOTIFICATION,
Subject = "%Store.Name%. Your order is processing",
Expand Down
12 changes: 12 additions & 0 deletions src/Libraries/Nop.Services/Messages/IWorkflowMessageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,18 @@ public partial interface IWorkflowMessageService
/// The task result contains the queued email identifier
/// </returns>
Task<IList<int>> SendOrderCancelledCustomerNotificationAsync(Order order, int languageId);

/// <summary>
/// Sends an order cancelled notification to a vendor
/// </summary>
/// <param name="order">Order instance</param>
/// <param name="vendor">Vendor instance</param>
/// <param name="languageId">Message language identifier</param>
/// <returns>
/// A task that represents the asynchronous operation
/// The task result contains the queued email identifier
/// </returns>
Task<IList<int>> SendOrderCancelledVendorNotificationAsync(Order order, Vendor vendor, int languageId);

/// <summary>
/// Sends an order refunded notification to a store owner
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1645,6 +1645,7 @@ MessageTemplateSystemNames.ORDER_PAID_AFFILIATE_NOTIFICATION or
MessageTemplateSystemNames.ORDER_PLACED_CUSTOMER_NOTIFICATION or
MessageTemplateSystemNames.ORDER_PROCESSING_CUSTOMER_NOTIFICATION or
MessageTemplateSystemNames.ORDER_COMPLETED_CUSTOMER_NOTIFICATION or
MessageTemplateSystemNames.ORDER_CANCELLED_VENDOR_NOTIFICATION or
MessageTemplateSystemNames.ORDER_CANCELLED_CUSTOMER_NOTIFICATION => [TokenGroupNames.StoreTokens, TokenGroupNames.OrderTokens, TokenGroupNames.CustomerTokens],

MessageTemplateSystemNames.SHIPMENT_SENT_CUSTOMER_NOTIFICATION or
Expand Down
46 changes: 46 additions & 0 deletions src/Libraries/Nop.Services/Messages/WorkflowMessageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1147,6 +1147,52 @@ public virtual async Task<IList<int>> SendOrderCancelledCustomerNotificationAsyn
}).ToListAsync();
}

/// <summary>
/// Sends an order cancelled notification to a vendor
/// </summary>
/// <param name="order">Order instance</param>
/// <param name="vendor">Vendor instance</param>
/// <param name="languageId">Message language identifier</param>
/// <returns>
/// A task that represents the asynchronous operation
/// The task result contains the queued email identifier
/// </returns>
public virtual async Task<IList<int>> SendOrderCancelledVendorNotificationAsync(Order order, Vendor vendor, int languageId)
{
ArgumentNullException.ThrowIfNull(order);

ArgumentNullException.ThrowIfNull(vendor);

var store = await _storeService.GetStoreByIdAsync(order.StoreId) ?? await _storeContext.GetCurrentStoreAsync();
languageId = await EnsureLanguageIsActiveAsync(languageId, store.Id);

var messageTemplates = await GetActiveMessageTemplatesAsync(MessageTemplateSystemNames.ORDER_CANCELLED_VENDOR_NOTIFICATION, store.Id);
if (!messageTemplates.Any())
return new List<int>();

//tokens
var commonTokens = new List<Token>();
await _messageTokenProvider.AddOrderTokensAsync(commonTokens, order, languageId, vendor.Id);
await _messageTokenProvider.AddCustomerTokensAsync(commonTokens, order.CustomerId);

return await messageTemplates.SelectAwait(async messageTemplate =>
{
//email account
var emailAccount = await GetEmailAccountOfMessageTemplateAsync(messageTemplate, languageId);
var tokens = new List<Token>(commonTokens);
await _messageTokenProvider.AddStoreTokensAsync(tokens, store, emailAccount);
//event notification
await _eventPublisher.MessageTokensAddedAsync(messageTemplate, tokens);
var toEmail = vendor.Email;
var toName = vendor.Name;
return await SendNotificationAsync(messageTemplate, emailAccount, languageId, tokens, toEmail, toName);
}).ToListAsync();
}

/// <summary>
/// Sends an order refunded notification to a store owner
/// </summary>
Expand Down
9 changes: 9 additions & 0 deletions src/Libraries/Nop.Services/Orders/OrderProcessingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1046,6 +1046,15 @@ protected virtual async Task SetOrderStatusAsync(Order order, OrderStatus os, bo
var orderCancelledCustomerNotificationQueuedEmailIds = await _workflowMessageService.SendOrderCancelledCustomerNotificationAsync(order, order.CustomerLanguageId);
if (orderCancelledCustomerNotificationQueuedEmailIds.Any())
await AddOrderNoteAsync(order, $"\"Order cancelled\" email (to customer) has been queued. Queued email identifiers: {string.Join(", ", orderCancelledCustomerNotificationQueuedEmailIds)}.");

var vendors = await GetVendorsInOrderAsync(order);
foreach (var vendor in vendors)
{
var orderCancelVendorNotificationQueuedEmailIds = await _workflowMessageService.SendOrderCancelledVendorNotificationAsync(order, vendor, _localizationSettings.DefaultAdminLanguageId);

if (orderCancelVendorNotificationQueuedEmailIds.Any())
await AddOrderNoteAsync(order, $"\"Order cancelled\" email (to vendor) has been queued. Queued email identifiers: {string.Join(", ", orderCancelVendorNotificationQueuedEmailIds)}.");
}
}

//reward points
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using FluentMigrator;
using Nop.Core.Infrastructure;
using Nop.Data;
using Nop.Data.Migrations;
using Nop.Services.Localization;
using Nop.Web.Framework.Extensions;

namespace Nop.Web.Framework.Migrations.UpgradeTo480
{
[NopMigration("2024-05-09 00:00:00", "4.80.0", UpdateMigrationType.Localization, MigrationProcessType.Update)]
public class LocalizationMigration : MigrationBase
{
/// <summary>Collect the UP migration expressions</summary>
public override void Up()
{
if (!DataSettingsManager.IsDatabaseInstalled())
return;

//do not use DI, because it produces exception on the installation process
var localizationService = EngineContext.Current.Resolve<ILocalizationService>();

var (languageId, languages) = this.GetLanguageData();

#region Delete locales

#endregion

#region Rename locales

#endregion

#region Add or update locales

localizationService.AddOrUpdateLocaleResource(new Dictionary<string, string>
{
//#7108
["Admin.ContentManagement.MessageTemplates.Description.OrderCancelled.VendorNotification"] = "This message template is used to notify a vendor that the certain order was cancelled.The order can be cancelled by a customer on the account page or by store owner in Customers - Customers in Orders tab or in Sales - Orders.",
}, languageId);

#endregion
}

/// <summary>Collects the DOWN migration expressions</summary>
public override void Down()
{
//add the downgrade logic if necessary
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using FluentMigrator;
using Nop.Core.Infrastructure;
using Nop.Data;
using Nop.Data.Migrations;
using Nop.Services.Configuration;

namespace Nop.Web.Framework.Migrations.UpgradeTo480
{
[NopMigration("2024-05-09 00:00:00", "4.80.0", UpdateMigrationType.Settings, MigrationProcessType.Update)]
public class SettingMigration : MigrationBase
{
/// <summary>Collect the UP migration expressions</summary>
public override void Up()
{
if (!DataSettingsManager.IsDatabaseInstalled())
return;

//do not use DI, because it produces exception on the installation process
var settingService = EngineContext.Current.Resolve<ISettingService>();
}

public override void Down()
{
//add the downgrade logic if necessary
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10695,6 +10695,9 @@
<LocaleResource Name="Admin.ContentManagement.MessageTemplates.Description.OrderCancelled.CustomerNotification">
<Value>This message template is used to notify a customer that the certain order was cancelled. The order can be cancelled by a customer on the account page or by store owner in Customers - Customers in Orders tab or in Sales - Orders.</Value>
</LocaleResource>
<LocaleResource Name="Admin.ContentManagement.MessageTemplates.Description.OrderCancelled.VendorNotification">
<Value>This message template is used to notify a vendor that the certain order was cancelled. The order can be cancelled by a customer on the account page or by store owner in Customers - Customers in Orders tab or in Sales - Orders.</Value>
</LocaleResource>
<LocaleResource Name="Admin.ContentManagement.MessageTemplates.Description.OrderCompleted.CustomerNotification">
<Value>This message template is used to notify a customer that the certain order was completed. The order gets the order status Complete when it's paid and delivered, or it can be changed manually to Complete in Sales - Orders.</Value>
</LocaleResource>
Expand Down

0 comments on commit 3fa485b

Please sign in to comment.