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

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
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
WordPress vs Webflow: Which Is Better for South African Small Businesses?

WBL-DEV Blog

WordPress vs Webflow: Which Is Better for South African Small Businesses?

WordPress or Webflow? Compare costs, control, hosting, and POPIA considerations to pick the best website platform for your South African small business.

Read Article
Why Is My WordPress Website Not Working? Common Problems and Solutions

WBL-DEV Blog

Why Is My WordPress Website Not Working? Common Problems and Solutions

Is your WordPress website not working properly? Learn what can cause WordPress errors, broken pages, plugin conflicts and other common problems.

Read Article
Why “Affordable” WordPress Websites Are a Bad Investment for Small Business Owners

WBL-DEV Blog

Why “Affordable” WordPress Websites Are a Bad Investment for Small Business Owners

“Affordable” WordPress websites often cost small businesses more in the long run. Discover the hidden risks of cheap themes, limited customization, and why custom development is the smarter investment.

Read Article
Custom WordPress Website vs Website Builder: Which Is Better for Your Business?

WBL-DEV Blog

Custom WordPress Website vs Website Builder: Which Is Better for Your Business?

Should you choose a custom WordPress website or a website builder? Compare flexibility, SEO, performance, ownership and long-term value before making your decision.

Read Article