Pricing examples
Describes how to work with the pricing API in Optimizely Commerce Connect 13.
Optimizely Commerce Connect provides two interfaces related to prices:
- IPriceService. Used for read-only prices.
- PriceDetailService. Used for editing prices.
See examples in the following how to get (list), save, and delete prices.
List prices
Use IPriceDetailService.List()Â in the following examples to get price details for a catalog entry. If it 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() which support filtering and paging, as exemplified 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 price 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 price 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 filter 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) to add/edit price details. Commerce Connect also provides an extension for IPriceDetailService.Save(IPriceDetailService) to add/edit only one 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) to delete price details. You also can use the IPriceDetailService.Delete(long) extension to delete a specific price detail.
The example below illustrates how to delete all prices for an 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 2 months ago