Shipping options
List of examples:
- Creating a new shipping option
- Updating a shipping option
- Updating multiple shipping options
- Adding a new shipping cost to a shipping option
- Removing a shipping cost from a shipping option
- Deleting a shipping option
Creating a new shipping option
// Creates a new shipping option object
ShippingOptionInfo newOption = new ShippingOptionInfo();
// Sets the shipping option properties
newOption.ShippingOptionDisplayName = "New option";
newOption.ShippingOptionName = "NewOption";
newOption.ShippingOptionSiteID = SiteContext.CurrentSiteID;
newOption.ShippingOptionEnabled = true;
// Gets the tax class
TaxClassInfo taxClass = TaxClassInfo.Provider.Get("NewClass", SiteContext.CurrentSiteID);
if (taxClass != null)
{
// Sets the shipping option tax class
newOption.ShippingOptionTaxClassID = taxClass.TaxClassID;
}
// Saves the shipping option to the database
ShippingOptionInfo.Provider.Set(newOption);
Updating a shipping option
// Gets the shipping option
ShippingOptionInfo updateOption = ShippingOptionInfo.Provider.Get("NewOption", SiteContext.CurrentSiteID);
if (updateOption != null)
{
// Updates the shipping option properties
updateOption.ShippingOptionDisplayName = updateOption.ShippingOptionDisplayName.ToLower();
// Saves the changes to the database
ShippingOptionInfo.Provider.Set(updateOption);
}
Updating multiple shipping options
// Gets all shipping options on the current site whose code name starts with 'NewOption'
var options = ShippingOptionInfo.Provider.Get()
.OnSite(SiteContext.CurrentSiteID)
.WhereStartsWith("ShippingOptionName", "NewOption");
// Loops through the shipping options
foreach (ShippingOptionInfo option in options)
{
// Updates the shipping option properties
option.ShippingOptionDisplayName = option.ShippingOptionDisplayName.ToLower();
// Saves the changes to the database
ShippingOptionInfo.Provider.Set(option);
}
Adding a new shipping cost to a shipping option
/*
* Note: The example only works correctly for shipping options that use shipping costs
* stored in the COM_ShippingCost database table (includes all options based on the Default carrier).
* You may need a different approach for shipping options of custom carriers.
*/
// Gets the shipping option
ShippingOptionInfo option = ShippingOptionInfo.Provider.Get("NewOption", SiteContext.CurrentSiteID);
if (option != null)
{
// Creates a new shipping cost object
ShippingCostInfo cost = new ShippingCostInfo();
// Sets the shipping cost properties
cost.ShippingCostMinWeight = 10;
cost.ShippingCostValue = 9.9M;
// Assigns the shipping option to which the shipping cost applies
cost.ShippingCostShippingOptionID = option.ShippingOptionID;
// Saves the shipping cost to the database
ShippingCostInfo.Provider.Set(cost);
}
Removing a shipping cost from a shipping option
/*
* Note: The example only works correctly for shipping options that use shipping costs
* stored in the COM_ShippingCost database table (includes all options based on the Default carrier).
* You may need a different approach for shipping options of custom carriers.
*/
// Gets the shipping option
ShippingOptionInfo option = ShippingOptionInfo.Provider.Get("NewOption", SiteContext.CurrentSiteID);
if (option != null)
{
// Gets the cost assigned to the shipping option for a specified weight (10)
ShippingCostInfo deleteCost = ShippingCostInfoProvider.GetShippingCostInfo(option.ShippingOptionID, 10);
if (deleteCost != null)
{
// Deletes the shipping cost
ShippingCostInfo.Provider.Delete(deleteCost);
}
}
Deleting a shipping option
// Gets the shipping option
ShippingOptionInfo deleteOption = ShippingOptionInfo.Provider.Get("NewOption", SiteContext.CurrentSiteID);
if (deleteOption != null)
{
// Deletes the shipping option
ShippingOptionInfo.Provider.Delete(deleteOption);
}