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

Why Your Website Is Not Getting Customers (And How to Fix It)

WBL-DEV Blog

Why Your Website Is Not Getting Customers (And How to Fix It)

Is your website getting visitors but no enquiries? Learn why your website is not generating customers and how South African businesses can improve leads and conversions.

Read Article
Best WordPress Hosting for Core Web Vitals | Fast, SEO-Optimised Hosting

WBL-DEV Blog

Best WordPress Hosting for Core Web Vitals | Fast, SEO-Optimised Hosting

Discover the best WordPress hosting for Core Web Vitals. Improve LCP, INP, and CLS with fast, SEO-optimised hosting built for performance, security, and rankings.

Read Article
ECommerce Website Price South Africa: What Does an Online Store Cost?

WBL-DEV Blog

ECommerce Website Price South Africa: What Does an Online Store Cost?

Find out how much an eCommerce website costs in South Africa. Learn what affects pricing, what's included, and why investing in a custom online store delivers better long-term value.

Read Article
Website Migration Service (WordPress)

WBL-DEV Blog

Website Migration Service (WordPress)

Move your WordPress website safely with our professional website migration service. We handle files, databases, emails and DNS with minimal downtime.

Read Article
Why New Websites Take Time to Rank: Understanding the Google Sandbox Effect

WBL-DEV Blog

Why New Websites Take Time to Rank: Understanding the Google Sandbox Effect

Understand the Google Sandbox effect and why new websites take time to rank. Learn how Google evaluates trust, authority, and quality signals before giving new sites strong visibility in search results.

Read Article
How to Speed Up Your WordPress Website with LiteSpeed Hosting

WBL-DEV Blog

How to Speed Up Your WordPress Website with LiteSpeed Hosting

Learn how to speed up your WordPress website using LiteSpeed hosting. Discover the best LiteSpeed Cache settings, optimisation tips, and why fast hosting improves SEO and user experience.

Read Article