Woocommerce free-shopping above a price is not reflecting at view cart

32 Views Asked by At

I have a created a website with wordpress and woocommerce.

I want to remove the shippig-rates if the customer buy more than €75.

So I have simple product called A and price is €39,- I can add this to the mini-cart and than view the cart page, I see the 39 and the shipping-rate.

but if i add with quantity one more the price is changing to 78, so the shipping price should be removed. But this is not removing the shipping-rate and stay with shipping rate, I have tried it with peace of code;

function my_hide_shipping_when_free_is_available( $rates ) {
    $free = array();
    foreach ( $rates as $rate_id => $rate ) {
        if ( 'gratis_verzending' === $rate->method_id && $rate->cost == 0 ) {
            $free[ $rate_id ] = $rate;
            break;
        }
    }

    // Output some debug information
    error_log( print_r( $rates, true ) );
    error_log( print_r( $free, true ) );

    return ! empty( $free ) ? $free : $rates;
}
add_filter( 'woocommerce_package_rates', 'my_hide_shipping_when_free_is_available', 100 );

This didn't work.

So I tried to force to get everything above €75,- bij adding the following code but still not working.

function my_force_free_shipping_above_75( $rates ) {
    // Get the cart subtotal
    $subtotal = WC()->cart->get_subtotal();

    // Check if the subtotal is greater than 75 euros
    if ( floatval( $subtotal ) > 75 ) {
        // Iterate over the rates and force free shipping
        foreach ( $rates as $rate_key => $rate ) {
            if ( 'gratis_verzending' === $rate_key ) {
                // Set the cost to 0 for free shipping
                $rates[ $rate_key ]['cost'] = 0;
            }
        }
    }

    return $rates;
}

// Hook the function to the 'woocommerce_package_rates' filter with a priority of 10
add_filter( 'woocommerce_package_rates', 'my_force_free_shipping_above_75', 10 );

Can anyone point me to the right direction? enter image description here enter image description here

0

There are 0 best solutions below