This document provides some basic examples of how to use the EPiServer Commerce APIs to work with pricing features. Examples covered here are for instance how to get, save and delete prices. EPiServer Commerce provides two interfaces related to prices: IPriceService, which is best suited for read-only prices, and IPriceDetailService which should be used for editing prices. In the following you will find some code examples for IPriceDetailService.
Listing prices
In these examples we use IPriceDetailService.List() to get price details for a catalog entry. If it is a variant or package, this method will return its own price. If it is a product or a bundle, this method will return prices for its child variants. There are overloads of List() which support filter and paging as examplified below.
Listing all prices
Using IPriceDetailService.List(ContentReference) to get the price details for an entry.
C#
public IList<IPriceDetailValue> ListAllPriceDetailValue(ContentReference catalogContentReference)
{
var priceDetailService = ServiceLocator.Current.GetInstance<IPriceDetailService>();
return priceDetailService.List(catalogContentReference);
}
Listing price with paging
Using IPriceDetailService.List(ContentReference, int, int , int) to get the price details for an entry with paging.
C#
public IList<IPriceDetailValue> ListPriceDetailValueWithPaging(ContentReference catalogContentReference, int offset, int numberOfItems, out int totalCount)
{
var priceDetailService = ServiceLocator.Current.GetInstance<IPriceDetailService>();
return priceDetailService.List(catalogContentReference, offset, numberOfItems, out totalCount);
}
Listing price with PriceFilter
Using IPriceDetailService.List(ContentReference, MarketId, PriceFilter, int, int , int) to get the price details for an entry with filter and paging.
C#
public IList<IPriceDetailValue> ListPriceDetailWithPriceFilter(ContentReference catalogContentReference, int offset, int numberOfItems, out int totalCount)
{
var priceDetailService = ServiceLocator.Current.GetInstance<IPriceDetailService>();
MarketId marketId = new MarketId("ER");
PriceFilter filter = new PriceFilter();
filter.Currencies = new List<Currency> { Currency.EUR, Currency.GBP };
return priceDetailService.List(catalogContentReference, marketId, filter, offset, numberOfItems, out totalCount);
}
Saving prices
Using IPriceDetailService.Save(IEnumerable<IPriceDetailService>) to add/edit price details. Commerce also provides an extension for IPriceDetailService.Save(IPriceDetailService) to add/edit only one price.
C#
public IPriceDetailValue SavePriceDetailValue(Entry catalogEntry)
{
var priceDetailService = ServiceLocator.Current.GetInstance<IPriceDetailService>();
var priceDetailValue = new PriceDetailValue
{
CatalogKey = new CatalogKey(catalogEntry),
MarketId = new MarketId("US"),
CustomerPricing = CustomerPricing.AllCustomers,
ValidFrom = DateTime.UtcNow.AddDays(-7),
ValidUntil = DateTime.UtcNow.AddDays(7),
MinQuantity = 0m,
UnitPrice = new Money(100m, Currency.USD)
};
return priceDetailService.Save(priceDetailValue);
}
Deleting prices
Using IPriceDetailService.Delete(IEnumerable<long>) to delete price details. The extension IPriceDetailService.Delete(long) can also be used to delete a specific price detail.
The example below illustrates how to delete all prices for an entry.
C#
public void DeletePriceDetailValue(ContentReference catalogContentReference)
{
var priceDetailService = ServiceLocator.Current.GetInstance<IPriceDetailService>();
var priceList = priceDetailService.List(catalogContentReference);
IEnumerable<long> priceValueIds = priceList.Select(p => p.PriceValueId).ToList();
priceDetailService.Delete(priceValueIds);
}
Do you find this information helpful? Please log in to provide feedback.