private
void
AddShippingMethods(SiteInitializer initializer)
{
using
(var shippingManager = ShippingManager.GetManager())
{
IList<ShippingCarrierProviderSettings> shippingCarrierProviders = shippingManager.GetShippingCarrierSettings().Where(sc => sc.IsActive ==
true
).ToList();
foreach
(var shippingCarrier
in
shippingCarrierProviders)
{
ShippingMethod shippingMethod = shippingManager.CreateShippingMethod();
shippingMethod.Area =
"{\"Area\":\"2\",\"Countries\":\"\"}"
;
// International
shippingMethod.IsActive = shippingCarrier.IsActive;
shippingMethod.LastModified = DateTime.UtcNow;
shippingMethod.Name = shippingCarrier.Name;
shippingMethod.ShippingCarrierId = GetShippingCarrierId(shippingCarrier.Name);
shippingMethod.ShippingMethodSettings = GetShippingMethodSettings(shippingCarrier, shippingCarrier.Name, shippingManager);
shippingMethod.ShippingMethodType = ShippingMethodType.Standard;
shippingMethod.ShippingPrice =
""
;
shippingMethod.SortOrder = 1;
shippingMethod.Title = shippingCarrier.Title;
shippingManager.SaveChanges();
}
}
}
private
Guid GetShippingCarrierId(
string
shippingCarrierName)
{
Guid shippingCarrierId = Guid.Empty;
switch
(shippingCarrierName)
{
case
FedExProvider.Name:
shippingCarrierId =
new
Guid(EcommerceConstants.FedExShippingMethodId);
break
;
case
UPSProvider.Name:
shippingCarrierId =
new
Guid(EcommerceConstants.UPSShippingMethodId);
break
;
case
UspsProvider.Name:
shippingCarrierId =
new
Guid(EcommerceConstants.USPSShippingMethodId);
break
;
default
:
break
;
}
return
shippingCarrierId;
}
private
string
GetShippingMethodSettings(ShippingCarrierProviderSettings carrierProviderSettings,
string
shippingCarrierName, ShippingManager shippingManager)
{
Settings settings =
new
Settings()
{
Code = carrierProviderSettings.Code,
Description = carrierProviderSettings.Description,
Url = carrierProviderSettings.Url,
Timeout = carrierProviderSettings.Timeout,
UserName = carrierProviderSettings.UserName,
Password = carrierProviderSettings.Password,
LicenseKey = carrierProviderSettings.LicenseKey,
MeterID = carrierProviderSettings.MeterID,
AccountNumber = carrierProviderSettings.AccountNumber,
ClassificationCode = carrierProviderSettings.ClassificationCode,
ServiceSettings = GetShippingMethodServiceSettings(shippingCarrierName, shippingManager)
};
JavaScriptSerializer serializer =
new
JavaScriptSerializer();
string
settingsAsJson = serializer.Serialize(settings);
return
settingsAsJson;
}
private
ServiceSettings[] GetShippingMethodServiceSettings(
string
shippingCarrierName, ShippingManager shippingManager)
{
IList<ShippingServiceSettings> shippingServiceSettings = shippingManager.GetShippingServiceSettings(shippingCarrierName);
List<ServiceSettings> serviceSettings =
new
List<ServiceSettings>();
foreach
(var shippingService
in
shippingServiceSettings)
{
ServiceSettings settings =
new
ServiceSettings()
{
IsActive = shippingService.IsActive,
Name = shippingService.Name,
Title = shippingService.Title,
Code = shippingService.Code,
Description = shippingService.Description,
Surcharge = shippingService.Surcharge,
MaxWeight = shippingService.MaxWeight,
SortOrder = shippingService.SortOrder
};
serviceSettings.Add(settings);
}
return
serviceSettings.ToArray();
}