skip to Main Content

WordPress Code Snippets


Adjust WooCommerce Shipping Costs Based on Weight

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;
}

I’m a WordPress developer with 10+ years of experience in WooCommerce and custom plugins. I combine technical expertise with design flair to help you create standout, user-friendly websites. Let’s transform your digital presence!

Best WordPress Hosting
Back To Top
Search