WBL-DEV Blog

How to Add a Custom Shipping Method to WooCommerce (PHP Tutorial)

Published • 2026-06-28
How to Add a Custom Shipping Method to WooCommerce (PHP Tutorial)

One of WooCommerce's greatest strengths is its flexibility. If the built-in shipping options don't meet your requirements, you can create your own shipping method using PHP. In this tutorial, we'll build a basic custom shipping method that adds a new shipping option called Custom Courier and charges a fixed shipping fee.


Although this example is simple, the same structure can be expanded to calculate shipping based on weight, postcode, cart total, product categories, or even external courier APIs.


Step 1: Create a Plugin

Create a new folder inside:

wp-content/plugins/

For example:

custom-woocommerce-shipping

Create a PHP file inside it called:

custom-woocommerce-shipping.php


Step 2: Add the Plugin Header


<?php
/**
* Plugin Name: Custom WooCommerce Shipping
* Description: Adds a custom shipping method.
* Version: 1.0
* Author: Your Name
*/

if (!defined('ABSPATH')) {
exit;
}


Activate the plugin from the WordPress admin panel.


Step 3: Register the Shipping Method

Add the following code:


add_action('woocommerce_shipping_init', 'custom_shipping_method_init');

function custom_shipping_method_init() {

class WC_Custom_Shipping extends WC_Shipping_Method {

public function __construct() {

$this->id = 'custom_shipping';
$this->method_title = 'Custom Courier';
$this->method_description = 'Example custom shipping method';

$this->enabled = "yes";
$this->title = "Custom Courier";

$this->init();
}

public function init() {

$this->init_form_fields();
$this->init_settings();

add_action(
'woocommerce_update_options_shipping_' . $this->id,
array($this, 'process_admin_options')
);
}

public function calculate_shipping($package = array()) {

$rate = array(
'id' => $this->id,
'label' => $this->title,
'cost' => 99,
);

$this->add_rate($rate);
}
}
}


This creates a new shipping method that charges R99.


Step 4: Tell WooCommerce About Your Shipping Method


add_filter('woocommerce_shipping_methods', 'add_custom_shipping_method');

function add_custom_shipping_method($methods) {

$methods['custom_shipping'] = 'WC_Custom_Shipping';

return $methods;
}


WooCommerce now knows that your shipping class exists.


Step 5: Enable the Shipping Method

Go to:


WooCommerce
Settings
Shipping
Shipping Zones


Edit a shipping zone.

Click:

Add Shipping Method


You should now see:

Custom Courier


Select it and save.


Step 6: Test It

Visit your shop.

Add a product to the cart.

Proceed to checkout.

You should see:


Custom Courier

R99

Making the Shipping Cost Dynamic

The real power of WooCommerce comes from calculating shipping yourself.

For example, based on the cart total:


public function calculate_shipping($package = array()) {

$total = WC()->cart->subtotal;

if ($total > 1000) {
$cost = 0;
} else {
$cost = 95;
}

$rate = array(
'id' => $this->id,
'label' => $this->title,
'cost' => $cost,
);

$this->add_rate($rate);
}

Now orders over R1,000 receive free shipping.


Charge by Weight

WooCommerce provides all the cart contents inside $package.

Example:


$weight = 0;

foreach ($package['contents'] as $item) {

$product = $item['data'];

$weight += $product->get_weight() * $item['quantity'];
}

if ($weight < 5) {
$cost = 85;
} elseif ($weight < 20) {
$cost = 140;
} else {
$cost = 250;
}

Charge by Province

The destination address is also available.


$state = $package['destination']['state'];

switch ($state) {

case 'WC':
$cost = 95;
break;

case 'GP':
$cost = 80;
break;

default:
$cost = 150;
}

This makes it easy to charge different rates depending on the customer's province.


Charge by Product Category

You can inspect every product in the cart.


foreach ($package['contents'] as $item) {

$product_id = $item['product_id'];

if (has_term('Furniture', 'product_cat', $product_id)) {

$cost = 350;

break;
}
}

Perfect for oversized products.


Hide the Shipping Method

If you don't want the shipping method available in certain situations:


if ($package['destination']['country'] !== 'ZA') {
return;
}

Or:


if (WC()->cart->subtotal < 500) {
return;
}

No shipping option will be shown if the conditions aren't met.


Integrating a Courier API

Instead of returning a fixed cost, you can call a courier API.

For example:


$response = wp_remote_post(
'https://example-courier.co.za/api/rates',
array(
'body' => array(
'postcode' => $package['destination']['postcode'],
'weight' => $weight,
)
)
);

$body = json_decode(
wp_remote_retrieve_body($response),
true
);

$cost = $body['price'];

This allows WooCommerce to display live shipping quotes from your courier provider.


Best Practices


When developing custom WooCommerce shipping methods:


  1. Keep your code inside a custom plugin instead of your theme.
  2. Sanitize and validate all user input.
  3. Test multiple shipping zones and checkout scenarios.
  4. Avoid editing WooCommerce core files.
  5. Cache external API responses where possible to improve performance.
  6. Follow WooCommerce coding standards to make future maintenance easier.


Creating a custom WooCommerce shipping method with PHP gives you complete control over how shipping costs are calculated. Whether you need flat-rate delivery, weight-based pricing, province-specific rates, or live courier quotes, WooCommerce's shipping API provides a flexible foundation for building solutions tailored to your business.


As your store grows, you can expand the same shipping class to handle increasingly complex pricing rules while keeping your code organised and maintainable.

Related Articles

What Is a JSON File? A Beginner’s Guide With JavaScript Examples

WBL-DEV Blog

What Is a JSON File? A Beginner’s Guide With JavaScript Examples

Learn what a JSON file is, how JSON syntax works, and how to read, create, edit, and send JSON data using JavaScript with practical code examples.

Read Article
Website Design Horror Story: 7 Lessons Businesses Should Learn Before Hiring a Web Developer

WBL-DEV Blog

Website Design Horror Story: 7 Lessons Businesses Should Learn Before Hiring a Web Developer

A website project gone wrong highlights important lessons about ownership, access, security, communication and choosing the right web developer.

Read Article
How Some Developers Hold Websites Hostage and Why WBL-DEV Does It Differently

WBL-DEV Blog

How Some Developers Hold Websites Hostage and Why WBL-DEV Does It Differently

Some developers lock clients out of their own websites, hosting, and accounts. Learn the warning signs and why WBL-DEV gives you full access, clear ownership, and ongoing support.

Read Article
Why Website Speed Matters for SEO and Customer Experience

WBL-DEV Blog

Why Website Speed Matters for SEO and Customer Experience

Learn why website speed is essential for SEO, user experience and conversions. Discover practical ways a fast WordPress website helps businesses attract more customers.

Read Article
How Much Does a 5-Page WordPress Website Cost in the USA? ($2,000–$3,500)

WBL-DEV Blog

How Much Does a 5-Page WordPress Website Cost in the USA? ($2,000–$3,500)

Find out how much a professional 5-page WordPress website costs in the USA. Typical pricing ranges from $2,000 to $3,500 depending on design, functionality, and business requirements.

Read Article
Why WordPress Still Wins: The Quiet Advantage Most Small Businesses Overlook

WBL-DEV Blog

Why WordPress Still Wins: The Quiet Advantage Most Small Businesses Overlook

A practical look at why WordPress remains the most reliable website platform for small businesses. Learn its real strengths in flexibility, SEO, and long-term control.

Read Article