Learn how to build a fully custom WooCommerce shipping system using PHP, including dynamic rates, location-based pricing, and real-world logic.
Most WooCommerce stores rely on default shipping zones, but real businesses often require custom shipping logic like:
- State-based pricing
- Dynamic delivery charges
- Free shipping thresholds
- API-based shipping
In this guide, you’ll learn how to build your own shipping system from scratch.
Step 1: Create Custom Shipping Method
WooCommerce allows custom shipping via extending its class:
class Custom_Shipping_Method extends WC_Shipping_Method {
public function __construct() {
$this->id = 'custom_shipping';
$this->method_title = 'Custom Shipping';
$this->enabled = "yes";
}
public function calculate_shipping($package = array()) {
$state = $package['destination']['state'];
if ($state == 'Punjab') {
$cost = 200;
} else {
$cost = 300;
}
$this->add_rate(array(
'id' => $this->id,
'label' => 'Custom Shipping',
'cost' => $cost
));
}
}
Step 2: Register the Method
add_action('woocommerce_shipping_init', function() {
require_once 'custom-shipping.php';
});
add_filter('woocommerce_shipping_methods', function($methods) {
$methods['custom_shipping'] = 'Custom_Shipping_Method';
return $methods;
});
Real-World Use Case
This is exactly how:
- Food delivery systems work
- Location-based pricing works
- Your own “Dynamic Shipping Plugin” works
Final Thoughts
Custom shipping is a high-demand skill. Businesses often need this but plugins fail to handle edge cases.