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 - 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.
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.
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.
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?
Isn't this controlled by cookies? Maybe clear your local cookies for the dev domain?
@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?
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) {
When logout: I clear all session variable following code:
string[] myCookies = Request.Cookies.AllKeys;
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
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.
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;
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,
Rusty, about logout problem, why sometime customerkey still keep old logged customer key? Is it cache issue?
@Nguyen - you must be circumventing the CustomerContext in your application.
I don't understand why this is needed -
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 theCustomerContext
with be of typeICustomer
. 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 theInvoiceService
, update the customer key and then save the invoice to make the association.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.
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.
Hi Nguyen,
How have you solved you very first question please?
is working on a reply...