In this how to you will learn how to add your own shipping price calculator.
First you need to create a default Django application (or use an existing one), where you can put in your plugin. If you do not know how to do this, please refer to the excellent Django tutorial.
The main part of the plugin consists of a class which must provide a certain API.
Create a class which inherits from lfs.plugins.ShippingMethodPriceCalculator:
from lfs.plugins import ShippingMethodPriceCalculator
class MyShippingMethodPriceCalculator(ShippingMethodPriceCalculator):
pass
This methods are called from LFS to display the shipping price or to calculate the total price of the cart or order.
def get_price_net(self):
# This doesn't make much sense, but the net price is always 11.0
return 11.0
def get_price_gross(self):
return 11.0 * ((100 + self.shipping_method.tax.rate) / 100)
Following all pieces are sticked together to the complete plugin:
from lfs.plugins import ShippingMethodPriceCalculator
class MyShippingMethodPriceCalculator(ShippingMethodPriceCalculator):
def get_price_net(self):
# This doesn't make much sense, but the net price is always 11.0
return 11.0
def get_price_gross(self):
return 11.0 * ((100 + self.shipping_method.tax.rate) / 100)
Now as the code is ready, you can easily plugin your shipping method price calculator:
Within the ShippingMethodPriceCalculator the current request and the shipping method are available as instance variables:
self.request
self.shipping_method
With the request you have access to the current cart (in case you need it):
from lfs.cart.utils import get_cart
cart = get_cart(self.request)
With the request you have access to the current customer (in case you need it):
from lfs.customer.utils import get_customer
customer = get_customer(self.request)