Woocommerce Change shipping method title and cost manually

Woocommerce Change shipping method title and cost manually
11Feb, 2020

If we want to change the shipping method title and cost in the checkout of our store based on the shipping class the product has. then this post is helpful. so please follow these below steps:

First of all, we need to add shipping class in our website admin panel of wooCommerce > shipping > Shipping classes
That we are defining in screenshots:

After we create shipping class then we need to define that class in our product. that we defining in the screenshot:

Woocommerce Change shipping method title and cost manually

Finally we need to add a code to our theme’s functions.php file. This code is below.


add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_shipping_class', 10, 2 );
function hide_shipping_method_based_on_shipping_class( $rates, $package )
{
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$class_slug = 'free-shipping';
foreach( WC()->cart->get_cart() as $cart_item ){
if( $cart_item['data']->get_shipping_class() == $class_slug ){
foreach( $rates as $rate_key => $rate ){
$rates[$rate_key]->cost = 0;
$rates[$rate_key]->label = __( 'Offerts', 'woocommerce' );
}
}
}
return $rates;
}

2 Comments

  • AffiliateLabz February 16, 2020 @ 6:59 am

    Great post! Super high-quality! Keep it up! 🙂

  • JamesjaB April 28, 2020 @ 2:12 am

    Thanks, this site is extremely practical.

Leave a Reply

Your email address will not be published. Required fields are marked *

X