I note that there is a similar query here.
I’m trying to add a “Contribution” product to the cart. This product is a bit like a donation, and doesn’t really exist. I created it like this:
internal class ContributionProduct : IProductSnapshot
{
public const string ContributionName = "Contribution";
public const string ContributionSku = "Contrib";
public ContributionProduct(Guid storeId, Guid? taxClassId, ProductPrice price)
{
this.StoreId = storeId;
this.Prices = new[] { price };
this.TaxClassId = taxClassId;
this.Properties = new Dictionary<string, string>() { { MyConstants.Store.ContributionProductIdentifierPropertyName, bool.TrueString } };
}
public Guid StoreId { get; }
public IEnumerable<ProductPrice> Prices { get; }
public IDictionary<string, string> Properties { get; }
public bool IsGiftCard => false;
public string ProductReference => nameof(ContributionProduct);
public string Sku => ContributionSku;
public string Name => ContributionName;
public Guid? TaxClassId { get; }
public static ContributionProduct Create(OrderReadOnly order, decimal contributionValue)
{
return new ContributionProduct(order.StoreId, order.TaxClassId, new ProductPrice(contributionValue, order.CurrencyId));
}
}
and I add it like this
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult AddContribution(AddContributionDto postModel)
{
try
{
var store = this.CurrentPage.GetStore();
using (var uow = this._uowProvider.Create())
{
var order = this._sessionManager.GetOrCreateCurrentOrder(store.Id)
.AsWritable(uow);
var contributionProduct = ContributionProduct.Create(order, postModel.ContributionValue);
var productPrices = contributionProduct.Prices;
var currencyPrice = productPrices?.FirstOrDefault(x => x.CurrencyId == order.CurrencyId);
if (currencyPrice == null)
{
Debug.WriteLine($"Can't add a Product to an Order where there is no price for the Order currency.");
}
order.AddProduct(contributionProduct, 1);
this._orderService.SaveOrder(order);
uow.Complete();
}
}
catch (ValidationException)
{
this.ModelState.AddModelError("productReference", "Failed to add contribution to cart");
return this.CurrentUmbracoPage();
}
return this.RedirectToCurrentUmbracoPage();
}
However I’m getting an exception stating “Can’t add a Product to an Order where there is no price for the Order currency.” at AddProduct(...) and my Debug.WriteLine(...) is never hit.
I’m a bit lost. Any points appreciated.
This is a companion discussion topic for the original entry at https://our.umbraco.com/forum/104287-cant-add-a-product-to-an-order-where-there-is-no-price-for-the-order-currency