Understanding Customers
Orders is one of the most essential parts of the platform. There would be no orders without customers, which is also an essential part of the platform. In this article we'll cover how customers works, how they are associated to orders, and what related data you can find for customers.
Customers And Baskets And Orders
Out of the box no customer will be assigned to the basket. What that means is that we will not identify the customer untill the checkout happens. If this is needed, you can move the CreateCustomer task from checkout to the basket pipeline. This will help identify the customer earlier in the process if need be.
Since customers can be related to members in the CMS (more of that later), you can also identify the customer object early in the process if the member is authenticated in the CMS, which is quite usefull for B2B scenarios.
During checkout (the checkoutpipeline) which is exeucted after credit cards has been entered we will:
- Create a customer based on the billing address
- If configured on store, we will create a member based on the customer, if it doesn't already exists (Email or already signed in member) and link that to the customer. See Catalog configuration for how to enable members for customers on store level.
This means that after checking out there will be a customer object associated with the order.
Order addresses and addresses for customers
When you're checking out a basket, we need a shipping and a billing address. Theese are created using the top level APIs during checkout
Theese are initially used to create the customer (billing address). The addresses will be associated with the order, but not stored as generic addresses on the customer. If you want to do that, you could hook into the checkout pipeline by creating a pipeline task
The addresses are stored in the uCommerce_Address table in the database and assoicated as a collection of addresses of the customer. Theese are not created or used out of the box as stated above, but you could use them to e.g fillout the billing and shipping address if you can identify the customer.
Associating Customers with Members in the CMS
If you mind the configuration article above, and configure it properly, a member will automatically be created and/or linked for the customer object on the order. This means that for each order there will be an associated customer, and a member associated with the customer.
Finding the current customer
Now when you want to get the customer for the signed in member (or create one) you can do this like this:
IMemberService memberService = ObjectFactory.Instance.Resolve<IMemberService>(); if (memberService.IsLoggedIn()) { var customer = Customer.All().Where(x => x.MemberId == memberService.GetCurrentMember().MemberId).FirstOrDefault(); if (customer == null) { customer = new Customer(); //set whatever data is needed. customer.save(); } }
Order history
Grabbing all orders for a customer/member is really easy.
You may want to use the top level API for this (mind it is not enabled on the static TransactionLibrary):
var transactinoLibraryInternal = ObjectFactory.Instance.Resolve<TransactionLibraryInternal>(); var orders = TransactionLibraryInternal.GetMemberOrderHistory();
You can also create a custom version of this if you need anything special. Really what happens is that we need to find all orders where the member id of the customer is the same as the signed in member.
IMemberService memberService = ObjectFactory.Instance.Resolve<IMemberService>(); if (memberService.IsLoggedIn()) { PurchaseOrder.All().Where(x => x.Customer.MemberId == memberService.GetCurrentMember().MemberId) .ToList(); }