This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

This holiday season, maximize your sales through enhanced website performance and 24/7 caring support. SAVE 30% NOW

Create a WooCommerce Custom Shipping Method Plugin

Updated on  18th April

7 Min Read
Reading Time: 7 minutes

In this tutorial, I will  create a simple shipping method for WooCommerce, which will calculate the cost of the shipping.

WooCommerce Custom Shipping Method

Before I begin, I will need to define how this shipping method will calculate the cost and where it can ship. The cost will be determined by the weight and by the zone where the shipment is going.

  • Spain
  • United Kingdom
  • USA

WooCommerce Shipping Method API

When creating a shipping method, I will extend the class from the WooCommerce abstract class WC_Shipping_Method. The defined attributes in this class are:

$id: ID (slug, keyword) of the shipping. Required.

$number: Integer ID.

$method_title: Name of the shipping as shown in the admin.

$method_description: Short description of the shipping as shown in the admin (Optional).

$enabled: String Boolean (“yes” or “no”) that gives the information if the shipping is enabled and can be used or not.

$title: Used to display the shipping name on the store.

$availability: Defines if the shipping is available or not.

$countries: Array of countries this method is enabled for. Default value is an empty array.

$tax_status: Default value is taxable. If it is taxable then the tax will be charged.

$fee: Default value is 0. Fees for the method.

$minimum_fee: The minimum fee for the method. Default value is null.

$has_settings: Defines if this method has any settings. Default value is true.

$supports: Array containing features this method supports. Default value is an empty array.

$rates: Array of rates. This must be populated to register shipping costs. Default value is an empty array.

The defined methods in the WC_Shipping_Method are:

is_taxable(): Returns whether or not tax needs to be calculated on top of the shipping rate.

add_rate( $args = array() ): Pushes a shipping rate defined in the parameter $args into the attribute $rates.

has_settings(): Returns the value of the attribute $has_settings.

is_available(): Returns if the shipping is available. If there are countries set in the attribute $countries and the attribute $availability is set to values including, specific or excluding, it will return true or false if the country is available for shipping.

get_title(): Returns the title of this shipping.

get_fee( $fee, $total ): Returns the fee value for this shipping based on the parsed $fee and $total.

supports( $feature ): Returns if this shipping method supports a feature or not.

Since the class WC_Shipping_Method extends the class WC_Settings_API, many more attributes and methods are available. However, to keep things simple, I will not go into the details of these elements.

At this point, I need to define several other methods so that the shipping method could GET or SET settings and also calculate the actual cost of the shipping. These methods are:

  • init(): Creates the form fields and settings (can be named differently, as long as I use the methods inside it and call it in the __constructor method).
  • calculate_shipping( $package ): This method is used to calculate the cost for this particular shipping. Package is an array that contains all the products included in the shipment.

In the calculate_shipping method, I will add the rate with the method add_rate. This method accepts an array with the following options:

  • id: ID of the rate.
  • label: Label for the rate.
  • cost: Amount of the shipping. It can be a single value or an array with costs of each item in the cart.
  • taxes : It accepts an array of taxes or nothing so the tax is calculated by WooCommerce. It can even accept false so that the tax will not be calculated.

calc_tax: Accepts per_order or per_item. If you use per_item, an array of costs is required.

To register the shipping method, I will add this shipping method in the array of the registered method by passing the name of the class. I will access this array and send the modified array back using a WordPress filter that is defined inside the WooCommerce plugin. The filter is called woocommerce_shipping_methods.

Create Shipping Method as a New Plugin

I  will create this shipping method as a new plugin that extends the WooCommerce plugin.

Create a new folder Cloudways under wp-content/plugins. Also, create a file with the same name cloudways-shipping.php and add this code:

This shipping method is available only in the previously determined list of countries. This will be set before the method init inside the method __construct. Add the following code to the method __construct:

Now, go to WordPress Admin Panel and activate the plugin.

Activate Cloudways

Next, go to WooCommerce > Settings > Shipping > Cloudways Shipping.

Shipping method

I have also defined a weight restriction of upto 50 kg in this shipping method.

I calculated the cost by the total weight, and also added the cost by the shipping country. The last step here is to register the rate. For this add this last part:

If you try now to view your cart or go to the checkout page and select a country that is available for this shipping, you’ll get a shipping cost displayed with this custom shipping method.

Your order

In this function, I will look for the shipping method which has been chosen. If the method is Cloudways_Shipping_Method  then I will check for its weight limit and the total weight in the cart. If the weight of the cart exceeds the weight limit, I will notify the customer.

After the filter woocommerce_shipping_methods add this code:


The WooCommerce Shipping API enables you to create your own shipping method and integrate it within WooCommerce. Limitations and availability can be set within the shipping method when calculating the availability or the cost for the shipping method. However, you could also set these methods outside the shipping method by using WooCommerce actions.

If you need help with the code or would like to contribute to the discussion, do leave a comment below

Share your opinion in the comment section. COMMENT NOW

Owais Alam

is the WordPress Community Manager at Cloudways - A Managed WooCommerce Hosting Platform and a seasoned PHP developer. He loves to develop all sorts of websites on WordPress and is in love with WooCommerce in particular. You can email him at

Start Growing with Cloudways Today.

Our Clients Love us because we never compromise on these

Get Our Newsletter
Be the first to get the latest updates and tutorials.