Write a Remember Me cookie after Customer Checks out

For general questions and discussions specific to the AbleCommerce 7.0 Asp.Net product.
Post Reply
Mike718NY
Commodore (COMO)
Commodore (COMO)
Posts: 485
Joined: Wed Jun 18, 2008 5:24 pm

Write a Remember Me cookie after Customer Checks out

Post by Mike718NY » Tue May 18, 2010 12:26 pm

After a customer (who created an account) checks out, . . and they placed their order, . .
I want to write a "remember me" cookie.
So the next time they return, their Email address will be in the
"E-Mail Address : " field of the Login Page.

I tried making this "true" but it didn't work (from OnePageCheckout):

User.Migrate(oldUser, newUser, true);
Token.Instance.InitUserContext(newUser);
FormsAuthentication.SetAuthCookie(newUser.UserName, false); // true didn't work

Anyone know how I can do this?

User avatar
s_ismail
Commander (CMDR)
Commander (CMDR)
Posts: 162
Joined: Mon Nov 09, 2009 12:20 am
Contact:

Re: Write a Remember Me cookie after Customer Checks out

Post by s_ismail » Wed May 19, 2010 3:36 am

I am not sure where do you want to write your cookie.Already on log in Page a cookie is working to remember User Name usually email address.

This is the code to write a cookie

Place this on login/register Button click event

Code: Select all

  HttpCookie EmailCookie = new HttpCookie("Email", EmailTextBox.Text);
                            EmailCookie.Expires = DateTime.MaxValue;
                            Response.Cookies.Add(EmailCookie);
On Page Load place this code

Code: Select all

 HttpCookie EmailCookie = Request.Cookies["Email"];
        if (EmailCookie != null && !string.IsNullOrEmpty(EmailCookie.Value))
            EmailTextBox.Text.Text = EmailCookie.Value;

User avatar
mazhar
Master Yoda
Master Yoda
Posts: 5084
Joined: Wed Jul 09, 2008 8:21 am
Contact:

Re: Write a Remember Me cookie after Customer Checks out

Post by mazhar » Wed May 19, 2010 5:49 am

AbleCommerce login page already have a remember me option. Did you tried that?

Mike718NY
Commodore (COMO)
Commodore (COMO)
Posts: 485
Joined: Wed Jun 18, 2008 5:24 pm

Re: Write a Remember Me cookie after Customer Checks out

Post by Mike718NY » Wed May 19, 2010 9:39 am

mazhar wrote:AbleCommerce login page already have a remember me option.
That works only after the customer puts in their Email address
the first time they return to the site, after placing their first order.
From then on, the Email address will appear

The problem is when they return to the site after they placed their first order :
the Email address doesn't show in the box.

Mike718NY
Commodore (COMO)
Commodore (COMO)
Posts: 485
Joined: Wed Jun 18, 2008 5:24 pm

Re: Write a Remember Me cookie after Customer Checks out

Post by Mike718NY » Wed May 19, 2010 10:16 am

I tried this and I think it works, . . added code at end (OnePageCheckout):

Code: Select all

 void CheckedOut(object sender, CheckedOutEventArgs e)
  {
        ...............
        //MIGRATE THE USER
        User oldUser = Token.Instance.User;
        string newUserName = oldUser.PrimaryAddress.Email;
        string newEmail = oldUser.PrimaryAddress.Email;
        string newPassword = NewUserPassword.Text;
        if (!CreateAccountPanel.Visible)
        {
          anonCheckout = true;
          newUserName = "zz_anonymous_" + Guid.NewGuid().ToString("N") + "@domain.xyz";
          newPassword = Guid.NewGuid().ToString("N");
        }
        MembershipCreateStatus createStatus;
        User newUser = UserDataSource.CreateUser(newUserName, newEmail, newPassword, string.Empty, string.Empty, true, Token.Instance.User.AffiliateId, out createStatus);
        //IF THE CREATE FAILS, IGNORE AND CONTINUE CREATING THE ORDER
        if (createStatus == MembershipCreateStatus.Success)
        {
          if (anonCheckout)
          {
            // CHANGE THE NAME AND EMAIL TO SOMETHING MORE FRIENDLY THAN GUID
            newUser.UserName = "zz_anonymous_" + newUser.UserId.ToString() + "@domain.xyz";
            newUser.Save();
          }
          User.Migrate(oldUser, newUser, true);
          Token.Instance.InitUserContext(newUser);
          FormsAuthentication.SetAuthCookie(newUser.UserName, false);

          // *********  I ADDED THIS TO WRITE THE COOKIE ********************
          if (!anonCheckout)
          {
            HttpCookie cookie = new HttpCookie("UserName", newUser.UserName);
            cookie.Expires = DateTime.MaxValue;
            Response.Cookies.Add(cookie);
          }

        }
      }
      ......................
Now when the customer return, the Email Address appears in Log In email text box.
This will avoid a lot of confusion, and make it easier for returning customers.

Mike718NY
Commodore (COMO)
Commodore (COMO)
Posts: 485
Joined: Wed Jun 18, 2008 5:24 pm

Re: Write a Remember Me cookie after Customer Checks out

Post by Mike718NY » Wed May 19, 2010 12:06 pm

This worked, . . but change
if (!anonCheckout)
{....
to:
if (CreateAccountPanel.Visible)
{....

Post Reply