A custom WooCommerce shipping method with weight tiers
WooCommerce’s built-in methods (flat rate, free shipping, local pickup) cover simple shops. A courier that charges by weight brackets, adds a surcharge for bulky items and does not deliver some product categories needs a shipping method of its own. It is one class that extends WC_Shipping_Method, registered with a filter, and it can then be added to any shipping zone like the built-in methods.
Problem: A shop ships with a regional courier: up to 2 kg costs 4.90, up to 10 kg 7.90, up to 30 kg 12.90, plus 5.00 for each bulky item, and furniture cannot be shipped this way at all. With flat rate and shipping classes, the weight brackets cannot be expressed.
Solution: Create a class Ha_Courier_Shipping extending WC_Shipping_Method with supports for shipping zones and instance settings, define the brackets and the surcharge as instance fields, compute the rate from the package weight and items in calculate_shipping(), return no rate for packages with excluded categories, and register it with woocommerce_shipping_methods.
The method
add_action( 'woocommerce_shipping_init', function (): void {
class Ha_Courier_Shipping extends WC_Shipping_Method {
public function __construct( $instance_id = 0 ) {
$this->id = 'ha_courier';
$this->instance_id = absint( $instance_id );
$this->method_title = __( 'Regional courier', 'textdomain' );
$this->method_description = __( 'Weight brackets with a surcharge for bulky items.', 'textdomain' );
$this->supports = array( 'shipping-zones', 'instance-settings', 'instance-settings-modal' );
$this->instance_form_fields = array(
'title' => array( 'title' => __( 'Title', 'textdomain' ), 'type' => 'text', 'default' => __( 'Courier', 'textdomain' ) ),
'brackets' => array( 'title' => __( 'Brackets (kg:price, one per line)', 'textdomain' ), 'type' => 'textarea', 'default' => "2:4.90\n10:7.90\n30:12.90" ),
'surcharge' => array( 'title' => __( 'Surcharge per bulky item', 'textdomain' ), 'type' => 'price', 'default' => '5.00' ),
);
$this->title = $this->get_option( 'title' );
add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
}
public function calculate_shipping( $package = array() ) {
$weight = 0.0;
$bulky = 0;
foreach ( $package['contents'] as $item ) {
$product = $item['data'];
if ( has_term( 'furniture', 'product_cat', $product->get_parent_id() ?: $product->get_id() ) ) {
return; // no rate: this method is not offered for the package
}
$weight += (float) $product->get_weight() * $item['quantity'];
if ( 'bulky' === $product->get_shipping_class() ) {
$bulky += $item['quantity'];
}
}
$weight = (float) wc_get_weight( $weight, 'kg' );
$cost = null;
foreach ( preg_split( '/\R/', (string) $this->get_option( 'brackets' ) ) as $line ) {
[ $max, $price ] = array_map( 'floatval', explode( ':', $line ) + array( 0, 0 ) );
if ( $max > 0 && $weight <= $max ) {
$cost = $price;
break;
}
}
if ( null === $cost ) {
return; // heavier than the largest bracket
}
$this->add_rate( array(
'id' => $this->get_rate_id(),
'label' => $this->title,
'cost' => $cost + $bulky * (float) $this->get_option( 'surcharge' ),
'package' => $package,
) );
}
}
} );
add_filter( 'woocommerce_shipping_methods', function ( array $methods ): array {
$methods['ha_courier'] = 'Ha_Courier_Shipping';
return $methods;
} );
How it fits into zones
supports with shipping-zones and instance-settings makes the method appear in WooCommerce → Settings → Shipping → zone → “Add shipping method”, and each zone keeps its own instance with its own brackets. get_rate_id() includes the instance ID, so two zones with this method do not collide.
Weights are converted with wc_get_weight() from the store’s unit, so the brackets stay in kilograms even if products are entered in grams. Returning without add_rate() means the method is not available for this package; if no other method is available either, the customer sees “No shipping options”, which is correct for furniture with this courier.
Caching of rates
WooCommerce caches shipping rates per package in the session. During development, changes to the code are not visible until the cart changes; the “Enable debug mode” option under Shipping options disables the cache. Taxes on the rate follow the method’s tax status setting, which WC_Shipping_Method provides as an instance field when added to instance_form_fields (tax_status).
NOTE: Shipping methods work for the classic checkout and the Cart and Checkout blocks alike, because both use the same shipping calculation. Order-related hooks around the shipped order: order notes from a warehouse webhook.