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:
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;
}
7 Comments