Shipping Method API
The WooCommerce Shipping Method API enables you to create custom shipping methods that integrate with shipping zones and provide dynamic rate calculations for your store.Overview
Shipping methods in WooCommerce extend theWC_Shipping_Method class and can provide rates based on:
- Cart contents (weight, dimensions, value)
- Customer location (shipping zones)
- Product shipping classes
- Custom business logic
Since WooCommerce 2.6, shipping methods use shipping zones and instances, allowing merchants to configure different settings for the same shipping method in different regions.
Creating a Shipping Method Plugin
Step 1: Plugin Setup
Create your plugin file:Step 2: Create the Shipping Method Class
Createclass-wc-shipping-custom.php:
Shipping Method Properties
string
required
Unique identifier for your shipping method
int
Instance ID when used within a shipping zone
string
required
Title shown in the admin shipping settings
string
Description shown in the admin
array
Features supported by the method:
shipping-zones- Works with shipping zonesinstance-settings- Has per-instance settingsinstance-settings-modal- Settings can be edited in a modalsettings- Has global settings page
string
default:"yes"
Whether the method is enabled
string
Title displayed to customers at checkout
string
default:"taxable"
Tax status:
taxable or noneCalculating Shipping Rates
Thecalculate_shipping() method is called when WooCommerce needs shipping rates for a cart.
Basic Rate Calculation
Weight-Based Calculation
Cart Value-Based Calculation
Working with Shipping Classes
Shipping classes allow different products to have different shipping costs.Adding Shipping Class Support
Calculate with Shipping Classes
Advanced Features
Multiple Rates
Offer multiple shipping options from one method:Real-time API Integration
Conditional Availability
Shipping Zones Integration
1
Navigate to Shipping Zones
Go to WooCommerce > Settings > Shipping and create or edit a shipping zone.
2
Add Shipping Method
Click “Add shipping method” and select your custom method from the dropdown.
3
Configure Instance Settings
Click “Edit” to configure the method’s settings for that specific zone.
4
Set Method Order
Drag and drop to reorder methods within the zone.
Each instance of your shipping method in a zone can have different settings. For example, different costs for different regions.
Package Information
The$package array passed to calculate_shipping() contains:
Testing Your Shipping Method
1
Add to Shipping Zone
Add your method to a test shipping zone.
2
Create Test Orders
- Add products to cart
- Enter different shipping addresses
- Verify rates calculate correctly
3
Test Edge Cases
- Empty cart
- Very heavy orders
- International addresses
- Free shipping thresholds
4
Debug Output
Use WooCommerce logging:
Best Practices
- Performance: Cache external API calls to avoid slow checkout
- Validation: Always validate and sanitize settings input
- Error Handling: Gracefully handle API failures
- Backwards Compatibility: Support both instance and global settings if needed
- Logging: Use WooCommerce’s logger for debugging