A simple, powerful and independent e-commerce platform.
Sell anything with ease.

Apply weight-based shipping options in the checkout process

This code can be used to add weight-based shipping options to the checkout process. The various shipping options are first defined in settings as a flat rate in the usual way. You will then need to find the ID for each method that you want to manage. Right-click on the method heading and choose inspect element. You should see something like:
href="admin.php?page=wc-settings&tab=shipping&instance_id=4"

This tells you the ID for that method is 4. Do this for every method you need to manage. You can then write php conditions to unset various methods based on the total weight in the cart. In the example below we are only offering methods 4 and 5 for weights below 2kg.

		/**
 * Apply weight-based shipping options in the checkout process
 */

add_filter('woocommerce_package_rates', 'cc_weight_based_shipping_options', 9999, 2);

function cc_weight_based_shipping_options($rates, $package) {

  if (WC()->cart->get_cart_contents_weight() > 2000) {

    unset($rates['flat_rate:4'], $rates['flat_rate:5']);

  }

  return $rates;

}
	
View Raw Code ID: 9937