AI OnAI Off
Not sure what is wrong, but this is how we do it, perhaps it's useful for you?
private void ConfigureShippingMethods()
{
var marketService = ServiceLocator.Current.GetInstance<IMarketService>();
var enabledMarkets = marketService.GetAllMarkets().Where(x => x.IsEnabled).ToList();
foreach (var language in enabledMarkets.SelectMany(x => x.Languages).Distinct())
{
var languageId = language.TwoLetterISOLanguageName;
var dto = ShippingManager.GetShippingMethods(languageId);
var workingDto = (ShippingMethodDto)dto.Copy();
DeleteShippingMethods(workingDto);
ShippingManager.SaveShipping(workingDto);
var marketsForCurrentLanguage = enabledMarkets.Where(x => x.Languages.Contains(language)).ToList();
var shippingSet = CreateShippingMethodsForLanguageAndCurrencies(workingDto, marketsForCurrentLanguage, languageId);
ShippingManager.SaveShipping(workingDto);
AssociateShippingMethodWithMarkets(workingDto, marketsForCurrentLanguage, shippingSet);
ShippingManager.SaveShipping(workingDto);
}
}
private void DeleteShippingMethods(ShippingMethodDto dto)
{
foreach (var method in dto.ShippingMethod)
{
method.Delete();
}
}
private void ImportTaxes()
{
_taxImportExport.Service.Import(Path.Combine(HostingEnvironment.ApplicationPhysicalPath, @"App_Data\Taxes.csv"), ',');
}
private IEnumerable<ShippingMethodDto.ShippingMethodRow> CreateShippingMethodsForLanguageAndCurrencies(ShippingMethodDto dto, IEnumerable<IMarket> markets, string languageId)
{
var shippingOption = dto.ShippingOption.First(x => x.Name == "Generic Gateway");
var shippingMethods = new List<ShippingMethodDto.ShippingMethodRow>();
var sortOrder = 1;
var usdCostExpress = new Money(20, Currency.USD);
var usdCostFast = new Money(15, Currency.USD);
var usdCostRegular = new Money(5, Currency.USD);
foreach (var currency in markets.SelectMany(m => m.Currencies).Distinct())
{
shippingMethods.Add(CreateShippingMethod(dto, shippingOption, languageId, sortOrder++, "Express-" + currency, $"Express {currency} (1 day)({languageId})", usdCostExpress, currency));
shippingMethods.Add(CreateShippingMethod(dto, shippingOption, languageId, sortOrder++, "Fast-" + currency, $"Fast {currency} (2-3 days)({languageId})", usdCostFast, currency));
shippingMethods.Add(CreateShippingMethod(dto, shippingOption, languageId, sortOrder++, "Regular-" + currency, $"Regular {currency} (4-7 days)({languageId})", usdCostRegular, currency));
}
return shippingMethods;
}
private ShippingMethodDto.ShippingMethodRow CreateShippingMethod(ShippingMethodDto dto, ShippingMethodDto.ShippingOptionRow shippingOption, string languageId, int sortOrder, string name, string description, Money costInUsd, Currency currency)
{
Money shippingCost = CurrencyFormatter.ConvertCurrency(costInUsd, currency);
if (shippingCost.Currency != currency)
{
throw new InvalidOperationException("Cannot convert to currency " + currency + " Missing conversion data.");
}
return dto.ShippingMethod.AddShippingMethodRow(
Guid.NewGuid(),
shippingOption,
languageId,
true,
name,
"",
shippingCost.Amount,
shippingCost.Currency,
description,
false,
sortOrder,
DateTime.Now,
DateTime.Now);
}
private void AssociateShippingMethodWithMarkets(ShippingMethodDto dto, IEnumerable<IMarket> markets, IEnumerable<ShippingMethodDto.ShippingMethodRow> shippingSet)
{
foreach (var shippingMethod in shippingSet)
{
foreach (var market in markets.Where(m => m.Currencies.Contains(shippingMethod.Currency)))
{
dto.MarketShippingMethods.AddMarketShippingMethodsRow(market.MarketId.Value, shippingMethod);
}
}
}
I'm trying to set shipping methods through initialization and don't see why I'm getting this ForeignKeyConstraint exception.
ForeignKeyConstraint FK_ShippingOption_ShippingMethod requires the child key values (18ede71f-3df3-4d9e-a4cb-3d25881c1ec6) to exist in the parent table