如何从代码中停止 Kentico 结账通知邮件
结论
Kentico 是否发送这两类邮件,通常由站点级电子商务设置控制:
CMSStoreSendOrderNotification:控制 “Send order notification”CMSStoreSendPaymentNotification:控制 “Send payment notification”
代码可以通过 SettingsKeyInfoProvider 读取这些设置,再决定是否执行订单或支付通知逻辑。实际的邮件发送代码通常编译在 CMS.Ecommerce 相关程序集中,网站项目里不一定有可编辑的源文件。查找时可以从 OrderInfoProvider、支付结果处理程序,以及 SendOrderNotification、SendPaymentNotification 等关键词入手。
管理后台中的设置位置
Kentico 不同版本的菜单名称略有区别,设置一般位于:
Settings
└── E-commerce
└── Emails
或:
Settings
└── E-commerce
└── Configuration
└── Emails
操作时需要选择正确的站点,因为这些设置可能既有全局值,也有站点覆盖值。
对应的选项通常是:
- Send order notification
- Send payment notification
在代码中读取设置
在较老版本的 Kentico 中,可以用以下方式读取当前站点的设置:
using CMS.Helpers;
bool sendOrderNotification =
SettingsKeyInfoProvider.GetBoolValue("CMSStoreSendOrderNotification");
bool sendPaymentNotification =
SettingsKeyInfoProvider.GetBoolValue("CMSStoreSendPaymentNotification");
如果运行代码时无法保证存在可靠的当前站点上下文,应明确传入站点名称:
using CMS.Helpers;
using CMS.SiteProvider;
SiteInfo site = SiteContext.CurrentSite;
bool sendOrderNotification = SettingsKeyInfoProvider.GetBoolValue(
"CMSStoreSendOrderNotification",
site.SiteName
);
bool sendPaymentNotification = SettingsKeyInfoProvider.GetBoolValue(
"CMSStoreSendPaymentNotification",
site.SiteName
);
可用的重载可能因 Kentico 版本而异。如果当前版本不接受站点名称参数,可以改用该版本 API 提供的站点设置读取方式,或者在 Visual Studio 的对象浏览器中检查 SettingsKeyInfoProvider.GetBoolValue 支持哪些重载。
自定义控制示例
如果结账和通知发送由自定义代码处理,可以在调用通知方法前加入业务判断:
using CMS.Helpers;
bool kenticoAllowsNotification =
SettingsKeyInfoProvider.GetBoolValue("CMSStoreSendOrderNotification");
bool customCondition =
order != null &&
order.OrderTotalPrice > 0 &&
!IsNotificationSuppressed(order);
if (kenticoAllowsNotification && customCondition)
{
// 调用当前 Kentico 版本提供的订单通知 API
SendOrderNotification(order);
}
支付通知可以采用相同的方式:
bool kenticoAllowsPaymentNotification =
SettingsKeyInfoProvider.GetBoolValue("CMSStoreSendPaymentNotification");
if (kenticoAllowsPaymentNotification && ShouldSendPaymentNotification(order))
{
// 调用当前 Kentico 版本提供的支付通知 API
SendPaymentNotification(order);
}
这里的 SendOrderNotification 和 SendPaymentNotification 只是示意调用。公开方法的实际名称、参数和所在类型取决于 Kentico 版本。在核对程序集 API 之前,不应直接照搬。
从哪里查找 Kentico 的发送代码
可以按以下顺序查找:
- 在解决方案中搜索:
CMSStoreSendOrderNotification
CMSStoreSendPaymentNotification
SendOrderNotification
SendPaymentNotification
OrderNotification
PaymentNotification
- 使用 Visual Studio 的 Object Browser 检查以下程序集:
CMS.Ecommerce.dll
CMS.Ecommerce.Web.UI.dll
- 重点检查:
OrderInfoProvider
ShoppingCartInfoProvider
PaymentResultInfo
PaymentOptionInfoProvider
- 如果网站项目中找不到具体实现,相关逻辑很可能已经编译进 Kentico DLL。此时需要查看对应 Kentico 版本的 API 文档、调试符号或合法获得的源代码包。
支付通知通常不是在普通的订单保存操作中发送,而是在支付网关返回结果、订单付款状态更新或付款成功处理时触发。因此,还需要检查自定义支付提供程序的回调代码。
注意事项
不要直接修改 Kentico 核心程序集或系统文件。升级和 hotfix 很容易覆盖这些改动,也会增加后续排查问题的难度。更稳妥的处理方式包括:
- 在自定义 checkout 流程中决定是否调用通知 API;
- 使用当前版本提供的全局事件或 provider customization;
- 在自定义支付回调中控制支付通知;
- 将后台设置保留为总开关,再叠加具体的业务条件。
关闭通知设置只能阻止 Kentico 标准流程发送邮件。如果项目中的自定义事件处理器、计划任务、营销自动化流程或支付网关代码也会发送相同邮件,就需要逐一检查这些入口,否则通知仍可能重复发送,或者无法被彻底关闭。
备注:内容仅供参考。
