In order to setup shipping by weight in WooCommerce, make sure the following steps are completed:
- Create the number of “flat rate” shipping options needed and note their IDs
- Add weight information to all the products
Then, paste the following code in the active theme’s functions.php:
/* WooCommerce shipping options based on weight */ add_filter( 'woocommerce_package_rates', 'bbloomer_woocommerce_tiered_shipping', 9999, 2 ); function bbloomer_woocommerce_tiered_shipping( $rates, $package ) { if ( WC()->cart->get_cart_contents_weight() < 1 ) { if ( isset( $rates['flat_rate:5'] ) ) unset( $rates['flat_rate:6'], $rates['flat_rate:8'] ); } elseif ( WC()->cart->get_cart_contents_weight() < 5 ) { if ( isset( $rates['flat_rate:5'] ) ) unset( $rates['flat_rate:5'], $rates['flat_rate:8'] ); } else { if ( isset( $rates['flat_rate:5'] ) ) unset( $rates['flat_rate:5'], $rates['flat_rate:6'] ); } return $rates; }
- Edit the IDs and weight data (in KG) as needed
- Edit code if needed (for example, to add remove several shipping options)
- Go to WooCommerce > Status > Tools and delete transients and expired data
Source: https://businessbloomer.com/woocommerce-shipping-weight-without-plugin/