Overview
When a customer places an order in your store, they have the option to create a user account, login, or just checkout as a guest. However, when they checkout as a guest, they'll still have an account created for them. This is because user accounts are required to associate orders to customers so that you can perform customer service tasks.
When customers use the guest checkout, they'll receive a couple of user account e-mails that they don't expect since they chose guest checkout. If this is the case with your clients, we'd recommend that you update the e-mail templates to let them know why they are receiving the e-mails. However, this article gives you an alternative approach.
Requirements
The following prerequisites will be necessary to accomplish the goals of this article:
- Have access to your Hotcakes store administration
- Knowledge of C# and .NET development
- Knowledge of using the custom order workflow extension point
Getting Started
You'll need to first have firsthand knowledge and experience writing and understanding C# code. Also, you should have familiarity with how the order workflow extension point works.
Replacing the User Account Creation Workflow Task
Warning!
If you're reading this, please know that this is very much an edge-case. You should very much consider not changing anything about how the e-mails and account creation happens. Changing this functionality will likely result in creating an undesirable user experience in most stores.
In order to override the notifications that are sent to new users, you’ll need to create a new task for your custom order workflow and replace the DnnCreateUserAccountForNewCustomer() task with your own. This example will allow you to send e-mails to your customer, EXCEPT when they're checking out using the guest checkout option.
Steps:
- Create a custom order workflow project (if you don't already have one).
- Create a new order task in your project and add the code below to it.
- Find the DnnCreateUserAccountForNewCustomerTask() in the MyWorkflow.cs file and comment it out or delete it.
- Add a new task in its place using the class name from the code below, such as MyCustomCreateDNNUserAccountTask().
- You may need to add references to DotNetNuke.dll and System.Web in your project.
- Build, test, and deploy this new order workflow as described in the documentation linked above.
using System;
using System.Collections.Generic;
using System.Linq;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Entities.Users;
using DotNetNuke.Security.Membership;
using DotNetNuke.Security.Roles;
using Hotcakes.Commerce.Accounts;
using Hotcakes.Commerce.BusinessRules;
using Hotcakes.Commerce.Content;
using Hotcakes.Commerce.Membership;
using Hotcakes.Commerce.Utilities;
using Hotcakes.Web.Logging;
namespace MyCompany.CustomWorkflow.Tasks
{
[Serializable]
public class MyCustomCreateDNNUserAccountTask : OrderTask
{
private const string GUEST_ROLENAME = "GuestCheckout";
private bool _isCreateGuestAccount;
public override bool Execute(OrderTaskContext context)
{
CustomerAccount existAccount = context.HccApp.MembershipServices.Customers.Find(context.UserId.ToString());
if (existAccount == null)
{
var username = context.Inputs.GetProperty("hcc", "RegUsername");
var password = context.Inputs.GetProperty("hcc", "RegPassword");
_isCreateGuestAccount = string.IsNullOrEmpty(username);
if (_isCreateGuestAccount && MembershipProviderConfig.RequiresUniqueEmail)
{
existAccount = context.HccApp.MembershipServices.Customers.FindByEmail(context.Order.UserEmail).FirstOrDefault();
}
if (existAccount == null)
{
CustomerAccount account = new CustomerAccount();
if (_isCreateGuestAccount)
{
int length = Hotcakes.Commerce.WebAppSettings.PasswordMinimumLength;
account.Password = Hotcakes.Web.PasswordGenerator.GeneratePassword(length);
}
else
{
account.Username = username;
account.Password = password;
}
account.Email = context.Order.UserEmail;
if (context.Order.HasShippingItems)
{
account.FirstName = context.Order.ShippingAddress.FirstName;
account.LastName = context.Order.ShippingAddress.LastName;
}
else
{
account.FirstName = context.Order.BillingAddress.FirstName;
account.LastName = context.Order.BillingAddress.LastName;
}
CreateAccount(context, account);
context.UserId = account.Bvin;
}
}
if (existAccount != null)
{
context.UserId = existAccount.Bvin;
}
return true;
}
private void CreateAccount(OrderTaskContext context, CustomerAccount n)
{
if (context.HccApp.MembershipServices.CreateCustomer(n, n.Password))
{
if (string.IsNullOrEmpty(context.HccApp.CurrentCustomerId))
LoginNewUser(context, n);
// Update Addresses for Customer
context.Order.BillingAddress.CopyTo(n.BillingAddress);
context.Order.ShippingAddress.CopyTo(n.ShippingAddress);
context.HccApp.MembershipServices.UpdateCustomer(n);
if (_isCreateGuestAccount)
{
context.Order.CustomProperties.Add("hcc", "allowpasswordreset", "1");
AssignToGuestRole(n);
}
if (!_isCreateGuestAccount)
{
// Email Password to Customer
EmailPasswordToCustomer(context, n);
}
}
}
private void AssignToGuestRole(CustomerAccount n)
{
var roleCtl = new RoleController();
var portalId = PortalSettings.Current.PortalId;
var ui = UserController.GetUserById(portalId, Convert.ToInt32(n.Bvin));
var gRole = roleCtl.GetRoleByName(portalId, GUEST_ROLENAME);
if (gRole == null)
{
gRole = new RoleInfo
{
PortalID = portalId,
RoleName = GUEST_ROLENAME,
IsPublic = false,
RoleGroupID = Null.NullInteger,
Status = RoleStatus.Approved
};
gRole.RoleID = roleCtl.AddRole(gRole);
}
roleCtl.AddUserRole(portalId, ui.UserID, gRole.RoleID, Null.NullDate);
}
private static void LoginNewUser(OrderTaskContext context, CustomerAccount n)
{
string errorMessage = string.Empty;
string userId = string.Empty;
context.HccApp.MembershipServices.LoginUser(n.Username, n.Password, out errorMessage, out userId);
}
private void EmailPasswordToCustomer(OrderTaskContext context, CustomerAccount n)
{
HtmlTemplate t = context.HccApp.ContentServices.GetHtmlTemplateOrDefault(HtmlTemplateType.ForgotPassword);
if (t != null)
{
System.Net.Mail.MailMessage m;
List<IReplaceable> replacers = new List<IReplaceable>();
replacers.Add(n);
replacers.Add(new Replaceable("[[NewPassword]]", n.Password));
t = t.ReplaceTagsInTemplate(context.HccApp, replacers);
m = t.ConvertToMailMessage(n.Email);
if (MailServices.SendMail(m, context.HccApp.CurrentStore) == false)
{
Hotcakes.Commerce.EventLog.LogEvent("Create Account During Checkout", "Failed to send email to new customer " + n.Email, EventLogSeverity.Warning);
}
}
}
public override bool Rollback(OrderTaskContext context)
{
return true;
}
public override string TaskId()
{
return "d46c0c56-4787-4972-9d30-5d104426eeba";
}
public override string TaskName()
{
return "Create DNN User Account for New Customer (CUSTOM)";
}
public override string StepName()
{
string result = string.Empty;
result = "Create DNN User Account for New Customer (CUSTOM)";
if (result == string.Empty)
{
result = this.TaskName();
}
return result;
}
public override Task Clone()
{
return new MyCustomCreateDNNUserAccountTask();
}
}
}
Need More Help?
Do you need more assistance with this article? Please review your support options.