Modify Free Shipping Threshold in WooCommerce Easily

How to change free shipping threshold in woocommerce; Modify free shipping amount woocommerce; Set free shipping limit woocommerce; Adjust free shipping threshold wordpress; Exclude product from free shipping threshold woocommerce; Customize free shipping threshold woocommerce; Woocommerce free shipping threshold settings; Change free shipping criteria woocommerce; Woocommerce free shipping threshold plugin; How to set free shipping over certain amount woocommerce;

Explanation

Want to tweak the free shipping settings in WooCommerce? Here's a simple way to do it!

Change the Free Shipping Threshold:

  • This code lets you set a new minimum purchase amount for free shipping. Just change the $new_threshold value to whatever amount you prefer. For example, if you want free shipping for orders over $100, set $new_threshold = 100;.
  • The code checks your cart total and removes the free shipping option if the total is below your set threshold.

Exclude Certain Products from Free Shipping:

  • If you have specific products that shouldn't qualify for free shipping, you can exclude them by adding their product IDs to the $excluded_product_ids array.
  • Make sure to replace 123, 456 with the actual IDs of the products you want to exclude.
  • The code assigns these products to a different shipping class, ensuring they don't get free shipping.

With these tweaks, you can easily control which orders qualify for free shipping and exclude specific products as needed. Just copy the code into your theme's functions.php file, and you're good to go!

Code

<?php
// Hook into WooCommerce to modify the free shipping threshold
add_filter('woocommerce_package_rates', 'wp_dudecom_modify_free_shipping_threshold', 10, 2);

/**
 * Modify the free shipping threshold in WooCommerce.
 *
 * @param array $rates Array of available shipping rates.
 * @param array $package The package array containing cart details.
 * @return array Modified array of shipping rates.
 */
function wp_dudecom_modify_free_shipping_threshold($rates, $package) {
    // Define the new free shipping threshold amount
    $new_threshold = 100; // Set your desired threshold amount here

    // Loop through each shipping rate
    foreach ($rates as $rate_id => $rate) {
        // Check if the rate is free shipping
        if ('free_shipping' === $rate->method_id) {
            // Get the cart total
            $cart_total = WC()->cart->get_displayed_subtotal();

            // Check if the cart total meets the new threshold
            if ($cart_total < $new_threshold) {
                // Remove free shipping if the cart total is below the threshold
                unset($rates[$rate_id]);
            }
        }
    }

    return $rates;
}

// Hook to exclude specific products from free shipping
add_filter('woocommerce_cart_shipping_packages', 'wp_dudecom_exclude_product_from_free_shipping');

/**
 * Exclude specific products from free shipping.
 *
 * @param array $packages Array of shipping packages.
 * @return array Modified array of shipping packages.
 */
function wp_dudecom_exclude_product_from_free_shipping($packages) {
    // Define product IDs to exclude from free shipping
    $excluded_product_ids = array(123, 456); // Replace with your product IDs

    foreach ($packages as &$package) {
        foreach ($package['contents'] as $key => $item) {
            // Check if the product is in the excluded list
            if (in_array($item['product_id'], $excluded_product_ids)) {
                // Set the shipping class to a non-free shipping class
                $package['contents'][$key]['data']->set_shipping_class_id(0); // Replace 0 with the ID of a non-free shipping class
            }
        }
    }

    return $packages;
}
?>

Instructions

File Location: Add the code to your theme's functions.php file or a custom plugin file.

Prerequisites:

  • Ensure WooCommerce is installed and activated on your WordPress site.
  • Have access to your WordPress theme files or the ability to create a custom plugin.

Implementation Steps:

  1. Access Your WordPress Dashboard: Log in to your WordPress admin panel.
  2. Navigate to Theme Editor: Go to Appearance > Theme Editor. If you prefer using a custom plugin, navigate to Plugins > Add New and create a new plugin.
  3. Open functions.php: In the Theme Editor, locate and open the functions.php file of your active theme. If using a plugin, open your plugin file.
  4. Copy and Paste the Code: Insert the provided code snippet into the functions.php file or your plugin file.
  5. Modify the Free Shipping Threshold: Change the $new_threshold value to your desired amount for free shipping eligibility.
  6. Exclude Specific Products: Update the $excluded_product_ids array with the product IDs you wish to exclude from free shipping.
  7. Save Changes: Click the Update File button to save your changes.
  8. Test Your Changes: Add products to your cart and verify that the free shipping threshold and exclusions work as expected.

By following these steps, you can easily adjust the free shipping threshold and exclude specific products from free shipping in WooCommerce. If you need further assistance or want to explore more advanced functionalities, consider reaching out to wp-dude.com for expert help.