Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Nguyen Hien 52 posts 133 karma points
    Jan 19, 2016 @ 14:33
    Nguyen Hien
    0

    How to clear CurrentCustomer when member logout

    Hi Rusty,

    In my source code, sometime I logout member and login with other member but old customer key still keep in Invoice when checking out.

    How can I remove permanent CurrentCustomer when customer log out?

  • Lee 1130 posts 3088 karma points
    Jan 19, 2016 @ 16:00
    Lee
    0

    Isn't this controlled by cookies? Maybe clear your local cookies for the dev domain?

  • Rusty Swayne 1655 posts 4993 karma points c-trib
    Jan 19, 2016 @ 16:57
    Rusty Swayne
    0

    @Nguyen,

    This should be happening behind the scenes.

    What member type are your members set to when you create the member?

    Any chance you could post your member creation code and your code for logging in/logging out?

  • Nguyen Hien 52 posts 133 karma points
    Jan 19, 2016 @ 17:44
    Nguyen Hien
    0

    Hi Rusty,

    I have 2 member type. With Facility member type, I defined in merchello config as

    customer memberTypes="Customer,Facility" />

    This member type, I created new member by manual in Umbraco

    With ClientStaff member type, I need create member at checkout page. In checkout page, I created member as below line of codes

    if (Session["qantas"] != null && int.Parse(Session["qantas"].ToString()) > 0) {

                    var memberService = ApplicationContext.Services.MemberService;
                    var member = memberService.CreateMember(model.EmployeeNumber, model.BillingEmail, model.BillingName, "ClientStaff");
                    member.SetValue("address1", model.BillingAddress1);
                    member.SetValue("address2", model.BillingAddress2);
                    member.SetValue("suburb", model.BillingLocality);
                    member.SetValue("state", model.BillingRegion);
                    member.SetValue("country", model.BillingCountryCode);
                    member.SetValue("postCode", model.BillingPostalCode);
                    member.SetValue("phone", model.BillingPhone);
                    member.SetValue("client", int.Parse(Session["qantas"].ToString()));
                    memberService.Save(member);
                    LogHelper.Warn(this.GetType(), "Member: " + member.Id);
    
                    var customer = new Customer(member.Email)
                                   {
                                       FirstName = member.Username,
                                       LastName = member.Name,
                                       Email = member.Email,
                                       TaxExempt = false
                                   };
    
                    MerchelloServices.CustomerService.Save(customer);
                    Session["CustomerKey"] = customer.Key;
                    ////CurrentCustomer = customer;
                    //CustomerContext.Reinitialize(customer);
    
                    FormsAuthentication.SetAuthCookie(member.Username, true);
                }
    

    When logout: I clear all session variable following code:

    string[] myCookies = Request.Cookies.AllKeys;

            foreach (string cookie in myCookies)
            {
    
                var httpCookie = Response.Cookies[cookie];
                if (httpCookie != null) httpCookie.Expires = DateTime.Now.AddDays(-1);
            }
    
            Session.Abandon();
            Session.Clear();
    
            return Redirect("/");
    
  • Rusty Swayne 1655 posts 4993 karma points c-trib
    Jan 19, 2016 @ 18:26
    Rusty Swayne
    0

    Checkout the HandleSignUp method in the Bazaar.

    https://github.com/Merchello/Merchello/blob/merchello-dev/src/Merchello.Bazaar/Controllers/MembershipOperationsController.cs#L48

    You will also need to add ClientStaff to the Merchello member types.

    so

      <!-- A comma delimited list of Umbraco MemberTypes to be considered as Merchello Customers -->
    

    You should also have your controller inherit from MerchelloSurfaceController as the CustomerContext gets instantiated.

    You should not need to manually create the Customer and the Basket will be managed for you based on the membership login status.

  • Nguyen Hien 52 posts 133 karma points
    Jan 19, 2016 @ 17:49
    Nguyen Hien
    0

    I also need a trick to save new customer key to new invoice in InvoiceFactory like this

    public InvoiceDto BuildDto(IInvoice entity) { Guid? customerKey = null;

            if (HttpContext.Current.Session["qantas"] != null)
            {
                if (HttpContext.Current.Session["CustomerKey"] != null)
                {
                    customerKey = Guid.Parse(HttpContext.Current.Session["CustomerKey"].ToString());
                }
            }
            else
            {
                if (entity.CustomerKey != null)
                {
                    customerKey = entity.CustomerKey;
                }
            }
    
            return new InvoiceDto()
            {
                Key = entity.Key,
                CustomerKey = customerKey,
                InvoiceNumberPrefix = entity.InvoiceNumberPrefix,
                InvoiceNumber = entity.InvoiceNumber,
                InvoiceDate = entity.InvoiceDate,
                PoNumber = entity.PoNumber,
                InvoiceStatusKey = entity.InvoiceStatusKey,
                VersionKey = entity.VersionKey,
                BillToName = entity.BillToName,
                BillToAddress1 = entity.BillToAddress1,
                BillToAddress2 = entity.BillToAddress2,
                BillToLocality = entity.BillToLocality,
                BillToRegion = entity.BillToRegion,
                BillToPostalCode = entity.BillToPostalCode,
                BillToCountryCode = entity.BillToCountryCode,
                BillToEmail = entity.BillToEmail,
                BillToPhone = entity.BillToPhone,
                BillToCompany = entity.BillToCompany,
                Exported = entity.Exported,
                Archived = entity.Archived,
                Total = entity.Total,
                InvoiceIndexDto = new InvoiceIndexDto()
                {
                    Id = ((Invoice)entity).ExamineId,
                    InvoiceKey = entity.Key,
                    UpdateDate = entity.UpdateDate,
                    CreateDate = entity.CreateDate
                },
                CreateDate = entity.CreateDate,
                UpdateDate = entity.UpdateDate,
                DueDate = entity.DueDate,
                BillNote = entity.BillNote
            };
        }
    
  • Rusty Swayne 1655 posts 4993 karma points c-trib
    Jan 19, 2016 @ 18:33
    Rusty Swayne
    0

    So what you are trying to do is update an invoice after it was generated by an anonymous checkout where later in the process you create the customer.

    I think I would get the invoice key for the invoice generated in the checkout (the one with CustomerKey = null) and then after you create your member and query for your customer,

       var invoiceService = MerchelloContext.Current.Services.InvoiceService;
    
       var invoice = invoiceService.GetByKey(invoiceKey);
    
      invoice.CustomerKey = [YOUR_CUSTOMER_KEY];
    
      invoiceService.Save(invoice);
    
  • Nguyen Hien 52 posts 133 karma points
    Jan 19, 2016 @ 23:01
    Nguyen Hien
    0

    Rusty, about logout problem, why sometime customerkey still keep old logged customer key? Is it cache issue?

  • Rusty Swayne 1655 posts 4993 karma points c-trib
    Jan 20, 2016 @ 16:56
    Rusty Swayne
    0

    @Nguyen - you must be circumventing the CustomerContext in your application.

    I don't understand why this is needed -

     if (HttpContext.Current.Session["qantas"] != null)
        {
            if (HttpContext.Current.Session["CustomerKey"] != null)
            {
                customerKey = Guid.Parse(HttpContext.Current.Session["CustomerKey"].ToString());
            }
        }
        else
        {
            if (entity.CustomerKey != null)
            {
                customerKey = entity.CustomerKey;
            }
        }
    

    The customer key for the respective customer BOTH anonymous and a previously established customer are stored in the "merchello" cookie.

    When the customer logs out, the customer context generates a completely new key to represent a new anonymous customer. If they log in again, the CustomerContext looks for a Merchello customer that has a matching login name as the Umbraco member that has logged in.

    We do not store anonymous customer keys on invoices - since there will be no Merchello customer association. Anonymous customers are a requirement for Merchello, since Umbraco does not setup a front end UI for members to log in by default.

    When a customer has logged in, the CurrentCustomer retrieved from the CustomerContext with be of type ICustomer. If that customer completes a checkout, the customer key IS added to the invoice (since there will be a customer association).

    You should NOT need to keep track of the customer key - it will be provided for you.

    This is also true for completing a checkout as an anonymous customer and creating a new Umbraco member as an option after the checkout. In this case, you need to keep of the invoice key that can be retrieved from the IPaymentResult.Invoice after a payment attempt. From there you can get the invoice via the InvoiceService, update the customer key and then save the invoice to make the association.

  • Alexander Bush 4 posts 93 karma points
    Oct 27, 2017 @ 14:54
    Alexander Bush
    0

    I have been having the same problem using the standard FastTrack login form.

    When testing locally, I have logged in with one user and viewed their sales history which is retrieved based on the CurrentCustomer value. When I then logout and log back in with a different customer, the CurrentCustomer is set back to the first one (after going anonymous when logging out). This results with the different account being able to view the sales history of the first customer (the entire checkout will also be based on the first one).

    You mentioned that the merchello cookie stores a previously established customer. Is it possible this cookie is somehow returning the previous user?

    I don't have any problems when logging in from a separate browser window.

  • Nguyen Hien 52 posts 133 karma points
    Jan 20, 2016 @ 09:05
    Nguyen Hien
    0

    Rusty, How can I debug where you create customer in merchello when member payment invoice? Currently, I set customer memberTypes="Customer,Office" /> in merchello.config But I don't see new customer when paid invoice.

  • Simon 692 posts 1068 karma points
    Oct 01, 2019 @ 13:47
    Simon
    0

    Hi Nguyen,

    How have you solved you very first question please?

Please Sign in or register to post replies

Write your reply to:

Draft