WC_Shipping_Method class. This guide covers everything you need to build a shipping method that integrates with shipping zones.
The WC_Shipping_Method Class
All shipping methods extendWC_Shipping_Method, which provides methods for calculating rates, managing settings, and integrating with shipping zones.
Key Properties
string
required
Unique identifier for your shipping method
int
Instance ID when used in shipping zones
string
Title shown in admin
string
Description shown in admin
string
Shipping method title displayed to customers
string
Whether the method is enabled (
'yes' or 'no')array
Array of features this method supports:
'shipping-zones': Supports shipping zones'instance-settings': Supports per-instance settings'instance-settings-modal': Settings can load in a modal'settings': Supports standalone settings page
string
Whether shipping is taxable (
'taxable' or 'none')Creating a Shipping Method
1
Check for WooCommerce
Ensure WooCommerce is active before loading your class.
2
Define your shipping method class
Create a class that extends
WC_Shipping_Method.3
Register the shipping method
Add your method to the shipping methods list.
Initialization and Settings
Theinit() method sets up form fields and loads settings:
/plugins/woocommerce/includes/abstracts/abstract-wc-shipping-method.php:164-171 for the constructor.
Form Fields
Define settings for both standalone and instance-based configurations:Instance Form Fields
For shipping zones, define instance-specific settings:Calculating Shipping Rates
Thecalculate_shipping() method is where you calculate and add shipping rates:
/plugins/woocommerce/includes/abstracts/abstract-wc-shipping-method.php:320-393 for the complete implementation.
Adding Rates
Theadd_rate() method registers a shipping rate:
/plugins/woocommerce/includes/abstracts/abstract-wc-shipping-method.php:291-377 for rate handling.
Working with Shipping Classes
Shipping classes allow different products to have different shipping costs:/plugins/woocommerce/includes/abstracts/abstract-wc-shipping-method.php:401-417 for the find_shipping_classes implementation.
Sanitizing Input
Always sanitize user input, especially cost fields:Complete Example
Here’s a complete, simplified shipping method:Useful Methods
TheWC_Shipping_Method class provides these helpful methods:
See
/plugins/woocommerce/includes/abstracts/abstract-wc-shipping-method.php for the complete API.
Testing Your Shipping Method
1
Enable the method
Go to WooCommerce > Settings > Shipping > Shipping Zones.
2
Add to a zone
Select a zone and add your shipping method.
3
Configure settings
Set the title, cost, and any other options.
4
Test on checkout
Add products to cart and proceed to checkout to see your method.
Shipping methods are only displayed when products in the cart require shipping and the customer’s address matches the shipping zone.
Next Steps
Payment Gateway API
Create custom payment gateways
Settings API
Learn more about the Settings API