Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Custom Placed Price Price Processor
using System;
using System.Collections.Generic;
using System.Linq;
using EPiServer;
using EPiServer.Commerce.Catalog.ContentTypes;
using EPiServer.Commerce.Order;
using Mediachase.Commerce;
using Mediachase.Commerce.Customers;
using Mediachase.Commerce.Catalog;
using Mediachase.Commerce.Pricing;
namespace CommerceExtensions.Order
{
public class CustomPlacedPriceProcessor : IPlacedPriceProcessor
{
private readonly IContentLoader _contentLoader;
private readonly ReferenceConverter _referenceConverter;
private readonly IServiceBasedPricing _serviceBasedPricing;
public CustomPlacedPriceProcessor(IContentLoader contentLoader, ReferenceConverter referenceConverter, IServiceBasedPricing serviceBasedPricing)
{
_contentLoader = contentLoader;
_referenceConverter = referenceConverter;
_serviceBasedPricing = serviceBasedPricing;
}
public bool UpdatePlacedPrice(ILineItem lineItem, CustomerContact customerContact, IMarket market, Currency currency,
Action<ILineItem, ValidationIssue> onValidationError)
{
EntryContentBase entryContent = lineItem.GetEntryContent(this._referenceConverter, this._contentLoader);
if (entryContent == null)
{
onValidationError(lineItem, ValidationIssue.RemovedDueToUnavailableItem);
return false;
}
Money? placedPrice = this.GetPlacedPrice(entryContent, lineItem.Quantity, customerContact, market, currency);
if (!placedPrice.HasValue)
{
onValidationError(lineItem, ValidationIssue.RemovedDueToInvalidPrice);
return false;
}
if (new Money(currency.Round(lineItem.PlacedPrice), currency) == placedPrice.Value)
{
return true;
}
onValidationError(lineItem, ValidationIssue.PlacedPricedChanged);
lineItem.PlacedPrice = placedPrice.Value.Amount;
return true;
}
public Money? GetPlacedPrice(EntryContentBase entry, decimal quantity, CustomerContact customerContact, IMarket market,
Currency currency)
{
return _serviceBasedPricing.GetPrice(entry.Code, quantity, market.DefaultCurrency, "20857D10-9D56-48FE-B616-509BC204A1A6", "AC0ACB01-D143-46FD-B1F0-2776BA6E3990", "5F2010AB-A164-4664-8C63-0032C63711CE");
}
}
}
using Mediachase.Commerce;
namespace CommerceExtensions.Order
{
public interface IServiceBasedPricing
{
Money GetPrice(string productCode, decimal quantit, Currency currency, string agentKey, string apiKey, string apiSecret);
}
}
using System;
using Mediachase.Commerce;
namespace CommerceExtensions.Order
{
public class ServiceBasedPricing : IServiceBasedPricing
{
public Money GetPrice(string productCode, decimal quantit, Currency currency, string customerKey, string apiKey, string apiSecret)
{
Random random = new Random();
Money m = new Money(random.Next(0, 100), currency);
return m;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment