Asked  7 Months ago    Answers:  5   Viewed   54 times

I am trying to get it so that if a customer were to add a coupon code (Any of them) the Free Shipping option would go away and the flat rate fee would be implemented. - You would think this would be an easy thing to implement, there would be 100's of plugins and ways described to do this, but I have not found any. I do not want to pay $89 for the plugin to do this one thing

A side bonus would be if they are using a coupon but are spending over $249 they can still qualify for Free shipping. I read some where how to do this, but it requires me to get the POST ID, which with the latest WooCommerce is not possible like it was before, I do not know the shipping ID so I am at a lost Here is the code

add_filter( 'woocommerce_shipping_packages', function( $packages ) {
$applied_coupons = WC()->session->get( 'applied_coupons', array() );
if ( ! empty( $applied_coupons ) ) {
$free_shipping_id = 'free_shipping:2';
unset($packages[0]['rates'][ $free_shipping_id ]);
}
return $packages;
} );

Thanks

Edited

add_filter( 'woocommerce_package_rates', 'coupons_removes_free_shipping', 
10, 2 );
function coupons_removes_free_shipping( $rates, $package ){
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
    return $rates;

$min_total      = 250; // Minimal subtotal allowing free shipping

// Get needed cart totals
$total_excl_tax = WC()->cart->get_total();
$discount_excl_tax = WC()->cart->get_discount_total();


// Calculating the discounted subtotal including taxes
$discounted_subtotal_incl_taxes = $total_excl_tax - $discount_excl_tax;

$applied_coupons   = WC()->cart->get_applied_coupons();

if( sizeof($applied_coupons) > 0 &&  $discounted_subtotal_incl_taxes < $min_total ) {
    foreach ( $rates as $rate_key => $rate ){
        // Targeting "Free shipping"
        if( 'free_shipping' === $rate->method_id  ){
            unset($rates[$rate_key]);
        }
    }
}
return $rates;

}

 Answers

46

The below code will enable "Free shipping" for applied coupons only if cart subtotal reaches a minimal amount (discounted including taxes):

add_filter( 'woocommerce_package_rates', 'coupons_removes_free_shipping', 10, 2 );
function coupons_removes_free_shipping( $rates, $package ){
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return $rates;

    $min_subtotal      = 250; // Minimal subtotal allowing free shipping
    
    // Get needed cart subtotals
    $subtotal_excl_tax = WC()->cart->get_subtotal();
    $subtotal_incl_tax = $subtotal_excl_tax + WC()->cart->get_subtotal_tax();
    $discount_excl_tax = WC()->cart->get_discount_total();
    $discount_incl_tax = $discount_total + WC()->cart->get_discount_tax();
    
    // Calculating the discounted subtotal including taxes
    $discounted_subtotal_incl_taxes = $subtotal_incl_tax - $discount_incl_tax;
    
    $applied_coupons   = WC()->cart->get_applied_coupons();

    if( sizeof($applied_coupons) > 0 && $discounted_subtotal_incl_taxes < $min_subtotal ){
        foreach ( $rates as $rate_key => $rate ){
            // Targeting "Free shipping"
            if( 'free_shipping' === $rate->method_id  ){
                unset($rates[$rate_key]);
            }
        }
    }
    return $rates;
}

Code goes in function.php file of your active child theme (or active theme). Tested and work.


Original answer:

The below code will remove "Free shipping" shipping methods when any coupon is applied without any settings need. There is some mistakes in your actual code. Try the following:

add_filter( 'woocommerce_package_rates', 'coupons_removes_free_shipping', 10, 2 );
function coupons_removes_free_shipping( $rates, $package ){
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return $rates;

    $applied_coupons = WC()->cart->get_applied_coupons();

    if( sizeof($applied_coupons) > 0 ){
        // Loop through shipping rates
        foreach ( $rates as $rate_key => $rate ){
            // Targeting "Free shipping" only
            if( 'free_shipping' === $rate->method_id  ){
                unset($rates[$rate_key]); // Removing current method
            }
        }
    }
    return $rates;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

Wednesday, March 31, 2021
 
axiomer
answered 7 Months ago
70

Updated:

As your question is a bit unclear, you could just need to add [qty]*2.5 in the Flat rate shipping method cost (for each shipping zone) in your wooCommerce shipping settings.

But it will not work if you have 2 different items in cart like: item1 (qty 1) + item2 (qty 1)

So this answer will do it in all cases:

1) First you will need to set a "Flat rate" shipping method for each Shipping Zones which cost will be set to 2.5 (in your WooCommerce shipping settings).

2) Adding this code that will calculate for each cart items (based on the total quantity of items) the new updated shipping cost:

add_filter( 'woocommerce_package_rates', 'custom_flat_rate_cost_calculation', 10, 2 );
function custom_flat_rate_cost_calculation( $rates, $package )
{
    // The cart count (total items in cart)
    $cart_count = WC()->cart->get_cart_contents_count();
    $taxes = array();

    // If there is more than 1 cart item
    if( $cart_count > 1 ){
        // Iterating through each shipping rate
        foreach($rates as $rate_key => $rate_values){
            // Targeting "Flat Rate" shipping method
            if ( 'flat_rate' === $rate_values->method_id ) {
                // Set the new calculated rate cost
                $rates[$rate_id]->cost = number_format($rates[$rate_id]->cost * $cumulated_active_quantity, 2);
                // Taxes rate cost (if enabled)
                foreach ($rates[$rate_id]->taxes as $key => $tax){
                    if( $rates[$rate_id]->taxes[$key] > 0 ){ // set the new tax cost
                        $taxes[$key] = number_format( $rates[$rate_id]->taxes[$key] * $cumulated_active_quantity, 2 );
                        $has_taxes = true;
                    } else {
                        $has_taxes = false;
                    }
                }
                if( $has_taxes )
                    $rates[$rate_id]->taxes = $taxes; 
            }
        }
    }
    return $rates;
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

This code is tested on WooCommerce 3+ and works

You will need to refresh shipping zones caches: disabling the Flat rate, then save. And enabling back this rate and save.

Friday, May 28, 2021
 
Jubair
answered 5 Months ago
29

In your code, you are not getting the selected shipping zone, you are just looping through each existing shipping zone, so you are always getting the last one. That's why it doesn't work.

In the following you will get the correct chosen shipping zone name:

add_filter('woocommerce_order_button_html', 'disable_place_order_button_html' );
function disable_place_order_button_html( $button ) {
    // HERE define your targeted shipping zone
    $targeted_zone_name = "Zone 6";

    // Get the chosen shipping method (if it exist)
    $chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods' );
    $chosen_shipping_method  = reset($chosen_shipping_methods);
    $chosen_shipping_method  = explode(':', $chosen_shipping_method );
    $chosen_shipping_zone    = WC_Shipping_Zones::get_zone_by( 'instance_id', end($chosen_shipping_method) );

    // If the targeted shipping zone is found, disable the button
    if( $targeted_zone_name == $chosen_shipping_zone->get_zone_name() ) {
        $style  = 'style="background:Silver !important; color:white !important; cursor: not-allowed !important; text-align:center;"';
        $text   = apply_filters( 'woocommerce_order_button_text', __( 'Place order', 'woocommerce' ) );
        $button = '<a class="button" '.$style.'>' . $text . '</a>';
    }
    return $button;
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

Saturday, May 29, 2021
 
KingCrunch
answered 5 Months ago
62

First you need to inspect the shipping methods radio buttons, to find out the shipping method ID corresponding value to "LATAM"…

enter image description here

To make it work for a specific shipping method ID you will use the following:

add_filter('woocommerce_order_button_html', 'disable_place_order_button_html' );
function disable_place_order_button_html( $button ) {
    // HERE define your targeted shipping method id
    $targeted_shipping_method = "flat_rate:14";

    // Get the chosen shipping method (if it exist)
    $chosen_shipping_methods = WC()->session->get('chosen_shipping_methods');
    
    // If the targeted shipping method is selected, we disable the button
    if( in_array( $targeted_shipping_method, $chosen_shipping_methods ) ) {
        $style  = 'style="background:Silver !important; color:white !important; cursor: not-allowed !important; text-align:center;"';
        $text   = apply_filters( 'woocommerce_order_button_text', __( 'Place order', 'woocommerce' ) );
        $button = '<a class="button" '.$style.'>' . $text . '</a>';
    }
    return $button;
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

Saturday, May 29, 2021
 
Grzegorz
answered 5 Months ago
77

First you will need to make your shipping settings as in the below screen, for "Flat rate" shipping method and only one shipping class named "Mailbox" (setting the desired amounts for "Mailbox" or No shipping class):

enter image description here

So some of your products will have the "Mailbox" shipping class and all others no shipping class. The products without shipping class (No shipping class) will be your "package.

The following code will remove the cart items shipping class, if there is more than one item with the "Mailbox" shipping class:

// Updating cart item price
add_action( 'woocommerce_before_calculate_totals', 'change_change_shipping_class', 30, 1 );
function change_change_shipping_class( $cart ) {
    if ( ( is_admin() && ! defined( 'DOING_AJAX' ) ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    // HERE define your shipping class SLUG
    $mailbox_shipping_class = 'mailbox';

    $mailbox_count = 0;

    // 1st cart item Loop: Counting "mailbox" shipping classes cart items
    foreach ( $cart->get_cart() as $cart_item ) {
        // Set the new price
        if( $cart_item['data']->get_shipping_class() == $mailbox_shipping_class ) {
            $mailbox_count += $cart_item['quantity'];
        }
    }

    // If there is more than one item we continue
    if( $mailbox_count <= 1 )
        return; // Exit

    // 2nd cart item Loop: Reset the cart items with shipping class "mailbox"
    foreach ( $cart->get_cart() as $cart_item ) {
        if(  $cart_item['data']->get_shipping_class() == $mailbox_shipping_class ){
            $cart_item['data']->set_shipping_class_id('0');
        }
    }
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

Thursday, August 26, 2021
 
Mike
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share