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 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.
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.
WBL-DEV Blog
What Is Website Hosting and How Much Does It Cost in South Africa?
What is website hosting and how much does it cost in South Africa? Learn how web hosting works, what you get for your money, and why hosting is essential for every website.
WBL-DEV Blog
How to Secure Your WordPress Site and Remove Malware Quickly
Practical, step-by-step guide to securing WordPress and removing malware: detection, containment, cleanup, hardening, and recovery strategies to protect your site and restore trust.
WBL-DEV Blog
Best Web Hosting Companies in South Africa for Small Businesses
Looking for reliable web hosting in South Africa? Compare the best hosting companies for small businesses, focusing on website speed, uptime, support, security and performance.
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.