How to show zero rate value of shipping class in c

2020-03-24 02:48发布

问题:

I need some help in WooCommerce Shipping method. How can I show the value of shipping class on the cart page. Let me explain little bit my problem.

I added a flat rate to charge shipping for some products i.e. chairs €7 which is working perfect and show on cart page as => Shipping: Flat rate: €7 but I've some chairs to ship free to my customers. I added new class like "Free Shipping" and set the value to 0.00 EUR on checkout it's no charging any cost which fine, but when client view cart and in shipping it only show the name of shipping method like Flat Rate without any cost because that was set to 0.00 which doesn't convince the customer and he think we've hidden cost.

Is there any way to show the zero value of shipping class or is this possible to show the class name rather than shipping method's title?

回答1:

You can add a filter, checking if the cost of your shipping method is zero (WooCommerce currently is doing nothing else in their function than printing the "flat rate" label, if the cost isn't above zero) and changing your label to whatever you'd like:

add_filter( 'woocommerce_cart_shipping_method_full_label', 'add_free_shipping_label', 10, 2 );
function add_free_shipping_label( $label, $method ) {
    if ( $method->cost == 0 ) {
        $label = 'Free shipping'; //not quite elegant hard coded string
    }
    return $label;
}

Based on https://stackoverflow.com/a/23581656/8264519 and works like a charm.



回答2:

Here is what I did.
1.Open \wp-content\plugins\woocommerce\includes\wc-cart-functions.php
2.Search for wc_cart_totals_shipping_method_label function
3.Replace if ( $method->cost > 0 ) with if ( $method->cost >= 0 ) and it should show the shipping cost even if it is set to 0.