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:
For example:
Create a PHP file inside it called:
Step 2: Add the Plugin Header
Activate the plugin from the WordPress admin panel.
Step 3: Register the Shipping Method
Add the following code:
This creates a new shipping method that charges R99.
Step 4: Tell WooCommerce About Your Shipping Method
WooCommerce now knows that your shipping class exists.
Step 5: Enable the Shipping Method
Go to:
Edit a shipping zone.
Click:
You should now see:
Select it and save.
Step 6: Test It
Visit your shop.
Add a product to the cart.
Proceed to checkout.
You should see:
Making the Shipping Cost Dynamic
The real power of WooCommerce comes from calculating shipping yourself.
For example, based on the cart total:
Now orders over R1,000 receive free shipping.
Charge by Weight
WooCommerce provides all the cart contents inside $package.
Example:
Charge by Province
The destination address is also available.
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.
Perfect for oversized products.
Hide the Shipping Method
If you don't want the shipping method available in certain situations:
Or:
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:
This allows WooCommerce to display live shipping quotes from your courier provider.
Best Practices
When developing custom WooCommerce shipping methods:
- Keep your code inside a custom plugin instead of your theme.
- Sanitize and validate all user input.
- Test multiple shipping zones and checkout scenarios.
- Avoid editing WooCommerce core files.
- Cache external API responses where possible to improve performance.
- 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
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.
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.
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.
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.
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.
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.