skip to Main Content

WordPress Code Snippets


Easily Adjust WooCommerce Shipping Rates by Location

This snippet enables you to customize shipping costs dynamically based on the customer’s shipping address.

add_filter('woocommerce_package_rates', 'custom_shipping_rates_based_on_location', 10, 2);
function custom_shipping_rates_based_on_location($rates, $package) {
    if ($package['destination']['country'] === 'US') {
        foreach ($rates as $rate_key => $rate) {
            if ('flat_rate' === $rate->method_id) {
                $rates[$rate_key]->cost += 5; // Add $5 to flat rate shipping
            }
        }
    }
    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