Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.
Plugin — Shipping Rate Provider
Purpose
Create, modify, and review GrandNode shipping rate calculation plugins that implement IShippingRateCalculationProvider.
When To Use
Use this skill when building a new shipping rate plugin, changing rate calculation logic, adding a custom shipping form, or reviewing an existing shipping provider for correctness.
When Not To Use
Do not use this skill for shipping method domain data (managed via IShippingMethodService in the Admin), for free-shipping logic, or for general plugin infrastructure; combine with plugin-module when foundational plugin setup is needed.
Inputs Required
- Repository root.
- Closest existing shipping plugin to use as template.
- Rate calculation approach: fixed rate, weight-based, real-time API.
- Whether a custom checkout form step is required.
- Required admin configuration settings.
Instructions
Mandatory Rules
Provider Interface
- Implement
IShippingRateCalculationProviderfromGrand.Business.Core.Interfaces.Checkout.Shipping. This extendsIProviderwhich requiresConfigurationUrl,SystemName,FriendlyName,Priority,LimitedToStores, andLimitedToGroups. - Implement all
IShippingRateCalculationProvidermembers:
| Member | Requirement |
|---|---|
GetShippingOptions(request) |
Required. Build and return a GetShippingOptionResponse with one ShippingOption per available method. Add errors to response.AddError(msg) instead of throwing. |
GetFixedRate(request) |
Return a known rate when a single rate can be computed before checkout, or null when rates vary. Used for cart estimate display. |
HideShipmentMethods(cart) |
Return true to suppress this provider during checkout for this cart (e.g., no shippable items). Return false otherwise. |
ValidateShippingForm(option, data) |
Return a list of validation error strings for any custom form submitted during the shipping step. Return empty list when no custom form is used. |
GetControllerRouteName() |
Return the public route name of a storefront controller that renders a custom shipping form, or empty string when no form is needed. |
ShippingRateCalculationType |
ShippingRateCalculationType.Off for pre-configured rates; ShippingRateCalculationType.Real for live API rates. |
ShipmentTracker |
Return null if shipment tracking is not supported; return an IShipmentTracker implementation otherwise. |
- Resolve
FriendlyNamethroughITranslationService.GetResource(Defaults.FriendlyName)so it is localizable. - Resolve
Priorityfrom the plugin settingsDisplayOrderproperty. - Use
IShippingMethodService.GetAllShippingMethods(restrictByCountryId, customer, storeId)to iterate over admin-configured methods and build aShippingOptionfor each. - Set
ShippingOption.NameandShippingOption.DescriptionusingshippingMethod.GetTranslation(x => x.Name, languageId). - Convert rates to the working currency using
ICurrencyService.ConvertFromPrimaryStoreCurrency(rate, workingCurrency).
Project Structure
- Use
Microsoft.NET.Sdk.Razoras the project SDK even when no Razor views are present, for consistency with existing shipping plugins. - Set both
<OutputPath>and<OutDir>to..\..\Web\Grand.Web\Plugins\{SystemName}\. - Mark all GrandNode project references as
Private="false"so they are not copied to the plugin output folder. - Include
logo.jpgwith<CopyToOutputDirectory>Always</CopyToOutputDirectory>.
Manifest, Defaults, Settings
- Define the assembly-level
[assembly: PluginInfo(...)]inManifest.cswithGroup = "Shipping rate". - Define a
{Plugin}Defaultsclass withProviderSystemName,FriendlyName(resource key), andConfigurationUrlconstants. - Define
{Plugin}Settings : ISettingswith at minimum aDisplayOrderproperty. Add any plugin-specific rate settings here.
Startup and Install
- Register the provider in
StartupApplication.ConfigureServices:
Register any additional plugin services (repositories, calculation services) in the same method.services.AddScoped<IShippingRateCalculationProvider, YourShippingProvider>(); - Implement
Install()in the plugin class: save default settings withISettingService.SaveSettingand register localization keys withIPluginTranslateResource.AddOrUpdatePluginTranslateResource. Callbase.Install()last. - Implement
Uninstall(): delete settings withISettingService.DeleteSetting<T>()and remove resource keys. Callbase.Uninstall()last.
Admin Configuration
- Create an admin controller with
[AuthorizeAdmin],[Area("Admin")], and[PermissionAuthorize(PermissionSystemName.ShippingSettings)]. - Use
IAdminStoreService.GetActiveStore()andISettingService.LoadSetting<T>(storeScope)/SaveSetting<T>(settings, storeScope)/ClearCache()for store-scoped settings.
Recommendations
- Prefer
Shipping.FixedRateShippingas a template for simple pre-configured rates andShipping.ByWeightfor weight-based calculation. - Return a meaningful error string via
response.AddError(...)rather than returning an empty response when the provider cannot compute rates. - Prefer returning
nullfromGetFixedRatewhen different methods have different rates — this prevents displaying a misleading single price in cart estimates.
Key Contracts
GetShippingOptionResponse / ShippingOption
// ShippingOption fields relevant to populate:
public string Name { get; set; }
public string Description { get; set; }
public double Rate { get; set; }
public string ShippingRateProviderSystemName { get; set; } // set automatically by framework
// GetShippingOptionResponse helpers:
response.AddError("message"); // records a non-fatal error
response.ShippingOptions.Add(option);
GetShippingOptionRequest (key fields)
Customer Customer { get; }
IList<PackageItem> Items { get; } // wraps ShoppingCartItem
Address ShippingAddress { get; }
ShippingRateCalculationType enum
Off = 0 // rate configured via admin; no live API call
Real = 10 // live calculation via external service
File Locations
| Concern | Path |
|---|---|
| IShippingRateCalculationProvider | src/Business/Grand.Business.Core/Interfaces/Checkout/Shipping/IShippingRateCalculationProvider.cs |
| IShippingMethodService | src/Business/Grand.Business.Core/Interfaces/Checkout/Shipping/IShippingMethodService.cs |
| IShipmentTracker | src/Business/Grand.Business.Core/Interfaces/Checkout/Shipping/IShipmentTracker.cs |
| GetShippingOptionRequest/Response | src/Business/Grand.Business.Core/Utilities/Checkout/ |
| ShippingOption | src/Core/Grand.Domain/Shipping/ShippingOption.cs |
| ShippingRateCalculationType | src/Business/Grand.Business.Core/Enums/Checkout/ShippingRateCalculationType.cs |
| Example — fixed rate | src/Plugins/Shipping.FixedRateShipping/ |
| Example — by weight | src/Plugins/Shipping.ByWeight/ |
| Example — shipping point | src/Plugins/Shipping.ShippingPoint/ |
Validation Checklist
- All
IShippingRateCalculationProviderandIProvidermembers implemented. -
GetShippingOptionsreturns at least one option or an error, never throws. - Rates converted to working currency via
ICurrencyService. -
IShippingMethodService.GetAllShippingMethodsused with country and store scope. - Output path set to
Grand.Web/Plugins/{SystemName}/. - Provider registered with
AddScoped<IShippingRateCalculationProvider, ...>. -
Installsaves settings and resource keys;Uninstallcleans them up. - Admin controller uses
[PermissionAuthorize(PermissionSystemName.ShippingSettings)].
Examples
Example 1: Fixed Rate per Method
Pattern from Shipping.FixedRateShipping: load all ShippingMethod records, look up a per-method setting key ("ShippingRateComputationMethod.FixedRate.Rate.ShippingMethodId{id}"), create one ShippingOption per method with the stored rate, convert currency.
Example 2: Flat Rate Plugin
Create a single ShippingOption with a hard-coded or settings-driven flat rate, returning it for all carts regardless of method or weight.
Example 3: API-Based Real-Time Rate
Set ShippingRateCalculationType = Real. In GetShippingOptions, call an external carrier API, map each returned service to a ShippingOption, and add errors if the API is unavailable.