Pricing examples
Describes how to work with the pricing API in Optimizely Customized Commerce.
Optimizely Customized Commerce provides two interfaces related to prices:
IPriceService
: Used for read-only prices.IPriceDetailService
: Used for editing prices.
The following examples demonstrate how to get (list), save, and delete prices.
List prices
Use IPriceDetailService.List()
to retrieve price details for a catalog entry. If the entry is a variant or package, this method returns its own price. If it is a product or a bundle, this method returns prices for its child variants. There are overloads of List()
that support filtering and paging, as shown below.
List all prices
Use IPriceDetailService.List(ContentReference)
to get price details for an entry.
public IList<IPriceDetailValue> ListAllPriceDetailValue(ContentReference catalogContentReference)
{
var priceDetailService = ServiceLocator.Current.GetInstance<IPriceDetailService>();
// Gets the price details of a CatalogEntry
return priceDetailService.List(catalogContentReference);
}
List prices with paging
Use IPriceDetailService.List(ContentReference, int, int , int)
to get price details for an entry with paging.
public IList<IPriceDetailValue> ListPriceDetailValueWithPaging(ContentReference catalogContentReference, int offset, int numberOfItems, out int totalCount)
{
var priceDetailService = ServiceLocator.Current.GetInstance<IPriceDetailService>();
// Gets price details for the CatalogEntry with paging
return priceDetailService.List(catalogContentReference, offset, numberOfItems, out totalCount);
}
List prices with PriceFilter
Use IPriceDetailService.List(ContentReference, MarketId, PriceFilter, int, int , int)
to get price details for an entry with filtering and paging.
public IList<IPriceDetailValue> ListPriceDetailWithPriceFilter(ContentReference catalogContentReference, int offset, int numberOfItems, out int totalCount)
{
var priceDetailService = ServiceLocator.Current.GetInstance<IPriceDetailService>();
// Gets price details for the CatalogEntry with paging support and filters for market, currencies, and customer pricings.
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);
}
Save prices
Use IPriceDetailService.Save(IEnumerable<IPriceDetailService>)
to add or edit price details. Customized Commerce also provides an extension, IPriceDetailService.Save(IPriceDetailService)
, to add or edit a single price.
public IPriceDetailValue SavePriceDetailValue(Entry catalogEntry)
{
var priceDetailService = ServiceLocator.Current.GetInstance<IPriceDetailService>();
// Set Price Detail value for Catalog Entry.
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);
}
Delete prices
Use IPriceDetailService.Delete(IEnumerable<long>)
to delete price details. You can also use the IPriceDetailService.Delete(long)
extension to delete a specific price detail.
The example below illustrates how to delete all prices for a catalog entry.
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(); // List price value Id
priceDetailService.Delete(priceValueIds);
}
Updated about 24 hours ago