Shipping options


List of examples:

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 = TaxClassInfoProvider.GetTaxClassInfo("NewClass", SiteContext.CurrentSiteName);
if (taxClass != null)
{
    // Sets the shipping option tax class
    newOption.ShippingOptionTaxClassID = taxClass.TaxClassID;
}

// Saves the shipping option to the database
ShippingOptionInfoProvider.SetShippingOptionInfo(newOption);


> Back to list of examples

Updating a shipping option




// Gets the shipping option
ShippingOptionInfo updateOption = ShippingOptionInfoProvider.GetShippingOptionInfo("NewOption", SiteContext.CurrentSiteName);
if (updateOption != null)
{
    // Updates the shipping option properties
    updateOption.ShippingOptionDisplayName = updateOption.ShippingOptionDisplayName.ToLower();

    // Saves the changes to the database
    ShippingOptionInfoProvider.SetShippingOptionInfo(updateOption);
}


> Back to list of examples

Updating multiple shipping options




// Gets all shipping options on the current site whose code name starts with 'NewOption'
var options = ShippingOptionInfoProvider.GetShippingOptions()
                                        .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
    ShippingOptionInfoProvider.SetShippingOptionInfo(option);
}


> Back to list of examples

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 = ShippingOptionInfoProvider.GetShippingOptionInfo("NewOption", SiteContext.CurrentSiteName);
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
    ShippingCostInfoProvider.SetShippingCostInfo(cost);
}


> Back to list of examples

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 = ShippingOptionInfoProvider.GetShippingOptionInfo("NewOption", SiteContext.CurrentSiteName);
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
        ShippingCostInfoProvider.DeleteShippingCostInfo(deleteCost);
    }
}


> Back to list of examples

Deleting a shipping option




// Gets the shipping option
ShippingOptionInfo deleteOption = ShippingOptionInfoProvider.GetShippingOptionInfo("NewOption", SiteContext.CurrentSiteName);

if (deleteOption != null)
{
    // Deletes the shipping option
    ShippingOptionInfoProvider.DeleteShippingOptionInfo(deleteOption);
}


> Back to list of examples