Django-Oscar - WeightBased Shipping - Part 9
OSCAR_DASHBOARD_NAVIGATION += [
     {
         'label': 'Shipping',
         'children': [
             {
                 'label': 'Shipping',
                 'url_name': 'dashboard:shipping-method-list',
             },
          ]
     },
]



This code creates a tab named Shipping in the dashboard
localhost:8000/dashboard

 


Creating a Shipping Method
 

  1. Click the Shipping tab and the Shipping option in the dropdown.
  2. Click the button  Create new shipping charge 
  3. Choose a Name and a Description.

    Example Name:
         UPS
    Example Description: 
         UPS Parcel Delivery  
         More information about shipping


    The Shipping method title and description will look like:


     
  4. Choose a Default Weight for a product. This value will be the weight of a product in case you forget to define it.
  5. Select to what countries this shipping method applies.
  6. Save

     

 

Adding weight bands

 

Define Upper Limit and Charge

The first Upper Limit you define will have the bottom value of zero.
So if you define an Upper Limit of 1Kg, this means that the interval will be from 0Kg to 1Kg and the Charge defined will apply to that interval.

The subsequent Upper Limits will always have the previous Upper Limit as the bottom value.
Which means that if you add another weight band with an Upper Limit of 2Kg,
it will automatically set the interval of 1Kg to 2Kg with correspondent Charge.

 

Creating the weight attribute

 

Continuing based on the Books product type created previously:

Dashboard → Catalog → Product Types → Books → Product attributes

Name: weight
Code: weight
Type: float
Required: True

Create a new attribute with the above specifications. Click Save

Keep in mind that the previously created products will not have the attribute weight defined.
For this reason the Default Weight will take over until you define the correct weight of the product.


Keep in mind that the weight attribute is in kilograms.

 

Shipping Charge logic

 

from oscar.apps.shipping.repository import Repository
from oscar.apps.shipping.models import WeightBased
from .methods import *

class Repository(Repository):

    def get_available_shipping_methods(self, basket, user=None, shipping_addr=None, request=None, **kwargs):

        if shipping_addr: # /checkout/shipping-method/ - Checkout Step 2

            weightbased_set =  WeightBased.objects.filter(countries=shipping_addr.country.code)

            if weightbased_set:

                methods = (list(weightbased_set))

            else:

                methods = [Standard()]

        else: # /basket/ - Basket page

            methods = [Standard()]

        return methods 




Optionally we can estimate the shipping charge at the basket page.
But this requires the address.
So in this case it was forced to show the weight bands for Portugal. 
 

from oscar.apps.shipping.repository import Repository
from oscar.apps.shipping.models import WeightBased
from .methods import *

class Repository(Repository):

    def get_available_shipping_methods(self, basket, user=None, shipping_addr=None, request=None, **kwargs):

        ...
        else: # /basket/ - Basket page

            weightbased_set =  WeightBased.objects.filter(countries="PT")

            if weightbased_set:
                
                methods = (list(weightbased_set))

        return methods