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">$</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.
For a specific shipping rate Id, to change the shipping rate displayed label based on its cost use:
Code goes in functions.php file of your active child theme (or active theme). It should works.