Home » Resources » Documentation » Get Involved » Writing a New Shipping Module

Writing a New Shipping Module

WP E-Commerce comes with a range of shipping options – but if you have special requirements then it’s also pretty simple to add your own bespoke shipping module to match your business.

This tutorial walks you through:

A full sample shipping module is included as a starting point for your own module – but if you’re still having problems check out the forums or more advice.

What is a shipping module?

There are two options for creating a shipping module:

  1. Place it into wp-content/plugins/wp-e-commerce/shipping
  2. Install it as a standard WordPress plugin

Option 2 requires a few extra lines of code, but it does mean that your module won’t get wiped out every time you upgrade WP E-Commerce and you do keep up-to-date don’t you??. It’s the approach were using in this tutorial.

How should it be structured?

So – where do we start from. The first thing is a Plugin header – this is just a standard WordPress plugin header:

<?php
/*
Plugin Name: Custom Shipping Method Skeleton
Plugin URI: http://www.leewillis.co.uk/wordpress-plugins/
Description: Sample Custom Shipping Method For WP E-Commerce
Version: 0.1
Author: Lee Willis
Author URI: http://www.leewillis.co.uk/
*/

All of our code will be wrapped in a class:
class my_shipping {

}

We also need a few lines of code to make sure that our class gets initiated and included in the list of available shipping methods:

function my_shipping_add($wpsc_shipping_modules) {
global $my_shipping;
$my_shipping = new my_shipping();
$wpsc_shipping_modules[$my_shipping->getInternalName()] = $my_shipping;
return $wpsc_shipping_modules;
}
add_filter('wpsc_shipping_modules', 'my_shipping_add');

What do the functions do?

Your class needs to implement some standard functions that WP E-Commerce expects to be able to call to identify the shipping module, provide configuration pages, and calculate shipping values.

Our sample module implements all of these:

A class initiation function

Any initial setup that your class needs. Our sample module just uses it to set the module name, and internal name.

getName()

This function just needs to return a description of your shipping module for display. It’s used both in the WordPress backend, and to display to your store’s customers.

getInternalName()

This function just needs to return an internal reference to your shipping module. It must be unique.

getForm()

This function should display whatever HTML you need to display to allow store administrators to customise the method for their store. This is displayed in the WordPress backend when the user configures your shipping method.

submit_form()

This function is called when a store administrator submits the customisation form. You should use it to store configuration information.

get_item_shipping( &$cart_item )

If there is a per-item shipping charge that applies irrespective of the chosen shipping method then it should be calculated and returned here. The value returned from this function is used as-is on the product pages. It is also included in the final cart & checkout figure along with the results from getQuote (below)

getQuote()

This function returns an Array of possible shipping choices, and associated costs. This is for the cart in general, per item charges (As returned from get_item_shipping (above)) will be added on as well.

Summary & Sample Module

So – that’s the brief summary. You can download the sample shipping module from here. It’s installable, you can select it and configure it – although it’s probably not one you’ll want to go live with! The code is pretty well commented – but if you’re not sure then feel free to ask for advice in the forums or post a comment in the corresponding blog post Writing a new Shipping Module.