WooCommerce Free Shipping Issue: Multiple Radio Buttons Dilemma on Orders Over free shipping price

38 views Asked by At

I have set up a WordPress site with WooCommerce.

My goal is to offer free shipping for orders over €75.

On its own, it works fine, but I'm encountering a small issue. If I have a product that costs €30 in my cart and then add more products, bringing the total above €75, suddenly two radio buttons for free shipping appear, instead of just showing one for free shipping. If I then increase the quantity again, it goes back to displaying the correct output.

This is my code:


add_filter('woocommerce_package_rates', 'update_shipping_rates_based_on_total', 10, 2);

function update_shipping_rates_based_on_total($rates, $package) {
    $price_limit = 75;

    // Check if the cart total is €75 or above
    if (WC()->cart->get_cart_contents_total() >= $price_limit) {
        // Set cost for free shipping
        foreach ($rates as $rate_id => $rate) {
            $rates[$rate_id]->cost = 0;
            $rates[$rate_id]->label = 'Gratis verzending';
        }
    }

    return $rates;
}

// Clear shipping rates cache when the cart is updated
add_action('woocommerce_after_cart', 'clear_shipping_rates_cache');

function clear_shipping_rates_cache() {
    WC()->shipping()->reset_shipping();
}

Here are the images to make it clearer:
enter image description hereenter image description hereenter image description here

0

There are 0 answers