Change Shipping method label name depending on its cost in WooCommerce

2.9k views Asked by At

Prefacing this question, I know that there might be other ways to accomplish the goal, but I'm trying to understand how Woocommerce works a bit better, so the situation is I have a plugin that can change the cost of shipping depending on the products in the cart, but I want to change the name of the shipping fee depending on the cost.

From what I found in the HTML the current structure of the shipping method looks like this:

<td data-title="Shipping">
<ul id="shipping_method" class="shipping__list woocommerce-shipping-methods">
    <li class="shipping__list_item">
        <input type="hidden" name="shipping_method[0]" data-index="0" id="shipping_method_0_flat_rate24" value="flat_rate:24" class="shipping_method" /><label class="shipping__list_label" for="shipping_method_0_flat_rate24">Flat Rate Shipping Fee: <span class="woocommerce-Price-amount amount"><bdi><span class="woocommerce-Price-currencySymbol">&#36;</span>10.00</bdi></span></label>                                
    </li>
</ul>

I'm attempting to create a function for the functions.php file of my Wordpress theme, I looked up some code on google and found a similar function involving "$available_shipping_methods" but I wasn't too sure how to use that variable so what I currently have is this:

add_filter('woocommerce_package_rates', 'wc_shipping_rate_rename', 100, 2)

function wc_shipping_rate_rename($rates, $package){
    //Checking if the shipping rate exists
    if ( isset( $rates['flat_rate:24'] ) ) {
        //Getting the rate
        $ship_cost = $rates['flat_rate:24']->cost;
        if ( $ship_cost == 10) {
            $rates['flat_rate:24'] -> 'Subsidized shipping fee:';
        }
        if ( $ship_cost == 100) {
            $rates['flat_rate:24'] -> 'Flat rate shipping fee:';
        }
    }
    return $rates;
}

The goal would be change the part in the HTML that says "Flat Rate Shipping Fee:" depending on the cost of the shipping fee. Any help would be really appreciated.

1

There are 1 answers

0
LoicTheAztec On BEST ANSWER

For a specific shipping rate Id, to change the shipping rate displayed label based on its cost use:

add_filter('woocommerce_package_rates', 'custom_shipping_rate_label_based_on_cost', 100, 2)
function custom_shipping_rate_label_based_on_cost( $rates, $package ){
    // Here your targeted shipping rate Id
    $targeted_rate_id = 'flat_rate:24';
    
    // Loop through available shipping rates
    foreach ( $rates as $rate_key => $rate ) {
        // Targetting specific rate Id
        if( $targeted_rate_id === $rate_key ) {
            $rate_cost = $rate->cost;
            
            if ( $rate_cost < 100 ) {
                $rate_label = __('Subsidized shipping fee');
            } 
            elseif ( $rate_cost >= 100 ) {
                $rate_label = __('Flat rate shipping fee');
            }
            
            if ( isset($rate_label) ) {
                $rates[$rate_key]->label = $rate_label;
            }
        }
    }
    return $rates;
}

Code goes in functions.php file of your active child theme (or active theme). It should works.

Clearing shipping caches:

  • You will need to empty your cart, to clear cached shipping data
  • Or In shipping settings, you can disable / save any shipping method, then enable back / save.