API Quick Tips

Follow

To get the shopping cart

var cart = Sitecore.Ecommerce.Context.Entity.GetInstance<Sitecore.Ecommerce.DomainModel.Carts.ShoppingCart>();

Then you can get things like cart item count and subtotal:

var itemCount = cart.ShoppingCartLines.Sum(line => line.Quantity);
var subtotal = cart.Totals.PriceExVat;

To get the shopping cart link

var shopContext = Sitecore.Ecommerce.Context.Entity.Resolve<Sitecore.Ecommerce.ShopContext>();
var configuration = shopContext.GetGeneralSettings();
var cartLink = Sitecore.Ecommerce.Utils.ItemUtil.GetItemUrl(configuration.ShoppingCartLink, false);

To check if user is logged-in

Sitecore.Ecommerce.Utils.MainUtil.IsLoggedIn()

To get the current user

var customerManager = Sitecore.Ecommerce.Context.Entity.Resolve<ICustomerManager<CustomerInfo>>();
var currentUser = customerManager.CurrentUser;

If logged-in user, you can then access customer details like Email, Shipping, and Billing address.

To check if current user is a customer

var customerMembership = Sitecore.Ecommerce.Context.Entity.Resolve();
var isCustomer = customerMembership.IsCustomer(Sitecore.Security.Accounts.User.Current);

To get products

Use the product repository to access products. Typically, this is to get a specific product (by product code).

var productRepository = Sitecore.Ecommerce.Context.Entity.Resolve<Sitecore.Ecommerce.DomainModel.Products.IProductRepository>();
var product = productRepository.Get<ProductBaseData>>(productCode);

To get all products in the repository:

var products = productRepository.GetAll<ProductBaseData>();

To get a product url

Use the GetUrl extension method (be sure to include ActiveCommerce.Extensions).

var url = product.GetUrl();

If you don't know whether an item is a product or regular Sitecore item:

if (item.Fields[Constants.Fields.FIELD_PRODUCT_CODE] != null && item.Fields[Constants.Fields.FIELD_PRODUCT_CODE].HasValue)
{
// Product
var repository = Sitecore.Ecommerce.Context.Entity.Resolve<IAdvancedProductRepository>();
var product = repository.GetProduct<ProductBaseData>(item);
return product.GetUrl();
}
else
{
// Everything else
return Sitecore.Links.LinkManager.GetItemUrl(item);
}

To get product totals

Use the GetTotals and GetDisplayTotals (GetProductTotals on older versions) extension methods (be sure to include ActiveCommerce.Extensions).

var totals = product.GetTotals();

GetTotals will get totals using the registered IProductPriceManager. GetDisplayTotals provides additional properties useful for pricing display, especially for variable products, such as MaxPrice and MinPrice.

To format prices

For price formatting, you can use the PriceFormatter. This takes into account the configured price format string (on General Settings) and current culture/currency.

var formatter = Sitecore.Ecommerce.Context.Entity.Resolve<ActiveCommerce.Prices.PriceFormatter>();
var formattedPrice = formatter.FormatPrice(24.55m);

To get orders

Use the order manager to access orders for a given shop context. You're able to retrieve by order number, customer id, or all, as well as save after updates. Remember to use a ShopContextSwitcher if running without a context, such as in a background job (see OrderUpdate example here).

var orderManager = Sitecore.Ecommerce.Context.Entity.Resolve<ActiveCommerce.Orders.Management.IOrderManager<ActiveCommerce.Orders.Order>>();
var order = orderManager.GetOrder("1234");
var ordersToProcess = orderManager.GetOrders().Where(order => order.State.Code == "Processing");
var ordersForCustomer = orderManager.GetOrdersForCustomer("{110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9}")

Use the order repository to retrieve all orders, regardless of shop context. This is useful in multi-site/shop scenarios. The order repository also provides a delete method (which the order manager does not).

var orderRepository = Sitecore.Ecommerce.Context.Entity.Resolve<ActiveCommerce.Orders.Management.IOrderRepository<ActiveCommerce.Orders.Order>>();
var ordersToProcess = orderRepository.GetOrders().Where(order => order.State.Code == "Processing");
orderRepository.Delete(order);
Have more questions? Submit a request

Comments