How to Dynamically Hide Flat Rate Shipping in WooCommerce Based on Minimum Order Amount

In WooCommerce, setting up free shipping for orders meeting a minimum amount is a common practice to encourage customers to spend more. Similarly, you might have already configured flat rate shipping as an alternative option. However, you may want to hide the flat rate shipping option dynamically once free shipping becomes available. In this tutorial, we’ll guide you through achieving this functionality in WordPress without the need for plugins. We assume you have already configured free shipping and flat rate options via WooCommerce settings.

Before proceeding with any customizations in WordPress, it’s essential to set up a child theme. A child theme acts as a safe and efficient way to make modifications without affecting the parent theme. If you haven’t set up a child theme yet, follow this tutorial on How to Create a Child Theme for Customization. It will guide you through the process and ensure that your customizations remain intact even after theme updates.

Understanding the Requirement

Before diving into the implementation, ensure you have configured both free shipping and flat rate shipping options in your WooCommerce settings. Our goal is to hide the flat rate shipping option dynamically when the total order amount meets or exceeds the minimum threshold for free shipping.

Locating functions.php and Add Custom Function

Navigate to your WordPress child-theme directory. Within the child-theme directory, locate the functions.php file. This file contains functions and code snippets that extend the functionality of your WordPress site. Open the functions.php file in a text editor or the WordPress theme editor. Add the following PHP code snippet to define a custom function named custom_shipping_logic:

/**
 * Custom function to dynamically hide the flat rate shipping option in WooCommerce
 * based on the minimum order amount required for free shipping.
 */
function custom_shipping_logic( $rates, $package ) {
    // Check if free shipping is available
    $has_free_shipping = isset( $rates['free_shipping:1'] );

    // If free shipping is available, hide the flat rate shipping option
    if ( $has_free_shipping ) {
        // Loop through each shipping rate
        foreach ( $rates as $rate_id => $rate ) {
            // Check if the method ID contains 'flat_rate'
            if ( strpos( $rate->method_id, 'flat_rate' ) !== false ) {
                // Remove the flat rate shipping option
                unset( $rates[ $rate_id ] );
                // Exit the loop after removing the first flat rate option
                break;
            }
        }
    }

    return $rates;
}

// Hook into the WooCommerce package rates filter
add_filter( 'woocommerce_package_rates', 'custom_shipping_logic', 10, 2 );

Code language: PHP (php)

Explanation

This code creates a custom function custom_shipping_logic hooked into the woocommerce_package_rates filter. The function receives two parameters: $rates, an array containing shipping rates, and $package, an object representing the package being shipped. Within the function, we first check if free shipping is available by verifying the presence of the ‘free_shipping:1’ rate in the $rates array. If free shipping is available, we loop through each shipping rate and check if its method ID contains the string ‘flat_rate’. If a flat rate shipping option is found, we remove it from the $rates array using the unset() function, effectively hiding it from the available shipping options. We then exit the loop after removing the first flat rate option, assuming there should only be one.

Save the Changes and Test the Functionality

Save the functions.php file after adding the custom function. Ensure that there are no syntax errors in the code. Next, visit your WooCommerce store and add products to the cart. Confirm that the total order amount meets or exceeds the minimum threshold for free shipping. Finally, verify that the flat rate shipping option is hidden when free shipping is available.

That’s it! In this tutorial, you learned how to dynamically hide the flat rate shipping option in WooCommerce based on the minimum order amount required for free shipping. By implementing this functionality, you can provide a seamless and intuitive shipping experience for your customers, encouraging them to make larger purchases. Experiment with different minimum order amounts and shipping configurations to optimize your WooCommerce store’s shipping strategy.

Leave your feedback and help us improve šŸ¶

We hope you found this article helpful! If you have any questions, feedback, or spot any errors, please let us know in the comments. Your input is valuable and helps us improve. If you liked this article, please consider sharing it with others. And if you really enjoyed it, you can show your support by buying us a cup of coffee ā˜•ļø or donating via PayPal šŸ’°.

More free knowledge, because why not?

Your thoughts matter, leave a reply šŸ’¬

Your email address will not be published. Required fields are marked *