This snippet allows you to set shipping costs that vary according to the weight of the items in the cart.
add_filter('woocommerce_package_rates', 'adjust_shipping_cost_based_on_weight', 10, 2);
function adjust_shipping_cost_based_on_weight($rates, $package) {
$weight = WC()->cart->get_cart_contents_weight();
if ($weight > 10) { // Change 10 to your desired weight limit
foreach ($rates as $rate_key => $rate) {
if ('flat_rate' === $rate->method_id) {
$rates[$rate_key]->cost += 10; // Increase cost by $10 for heavy items
}
}
}
return $rates;
}