Add a delivery cost
This article explains how to change the default delivery cost calculation in your orders by customising the pricing rules plugin.
A fixed delivery cost is implemented by default in the Litium project which takes the delivery method cost as a constant delivery cost. If you need a different behaviour, like calculating the delivery cost based on an ERP system or your own logic, you need to implement a calculation of the delivery cost.
The ideal scenario is to have a step-wise cost that changes based on the following parameters:
- Delivery provider: Distributor of goods, for example DHL, Posten, etc.
- Delivery method: for example By Air, Express post, Home delivery, etc.
- Amount of total articles weight.
- Shipment destination.
The ERP system, or the delivery processing system, will most probably make the above calculation, and for this example, we will simply assume a fake ERP system cost.
Adding a delivery cost from an ERP
We get the delivery cost from an ERP system, but note that the calculation of the delivery VAT percentage should be made according to the Swedish VAT rules. Swedish VAT rules specifies that delivery cost VAT should be proportional to the amount of VAT of the items bought. This rule can be different in other countries, and therefore you need to be careful when determining VAT for deliveries and whether the price from your ERP includes VAT or not.
- Add a new Visual Studio class library project into your solution. For this example we use the project Litium.Studio.Sample.
- Create a class that extends DeliveryCostCalculator which is the default implementation of IDeliveryCostCalculator.
using Litium.Foundation.Modules.ECommerce.Carriers;
using Litium.Foundation.Modules.ECommerce.Plugins.Deliveries;
using Litium.Foundation.Security;
namespace Litium.Studio.Sample.ECommerce.PricingRules
{
/// <summary>
/// Sample implementation of IDeliveryCostCalculator.
/// </summary>
public class SampleDeliveryCostCalculator : DeliveryCostCalculator
{
/// <summary>
/// Calculate the delivery cost connected to a single delivery.
/// </summary>
/// <param name="orderCarrier">The order carrier.</param>
/// <param name="deliveryCarrier">Carrier containing complete information about the delivery.
/// Update the delivery cost and other information in delivery carrier.</param>
/// <param name="token">Security token.</param>
public override void CalculateFromCarrier(OrderCarrier orderCarrier, DeliveryCarrier deliveryCarrier, SecurityToken token)
{
//initialize costs to zero.
deliveryCarrier.DeliveryCost = 0;
deliveryCarrier.DeliveryCostWithVAT = 0;
//send the orderCarrier and the deliveryCarrier which contains all necessary information about the order to the ERP system,
//and obtain the delivery cost.
var cost = 100; // var cost = CostFromERP(currency, deliveryItems, deliveryMethod);
var includeVat = true; //set based on your ERP calculation.
if (includeVat)
{
deliveryCarrier.DeliveryCostWithVAT = cost;
deliveryCarrier.KeepDeliveryCostWithVatConstant = true;
//deliveryCarrier.DeliveryCost will be calculated by VAT plugin
}
else
{
deliveryCarrier.DeliveryCost = cost;
deliveryCarrier.KeepDeliveryCostWithVatConstant = false;
//deliveryCarrier.DeliveryCostWithVAT will be calculated by VAT plugin
}
}
}
}
- Copy the .dll that contains the class into webroot/bin folder. Litium will detect it and start using your code instead of the default implementation.
Note: If you are using the default checkout flow plugin, there will be a default delivery created with the first delivery method available when an end customer starts shopping on your site. Hence this calcualtion will always be used, even though you might not have explicitly added a delivery in your checkout flow pages yet.