Woocommerce: Hide shipping method for a specific shipping class [duplicate]

  php, shipping, woocommerce, wordpress

I have a problem implementing this code in my WooCommerce website.
I want to hide specific shipping methods for the shipping class with the slug:’large-product’.
The shipping method that I need to hide is ‘flat_rate:13’ (I got it from inspecting the radio element in the browser):

<input type="radio" name="shipping_method[0]" data-index="0"
id="shipping_method_0_flat_rate13" value="flat_rate:13" class="shipping_method">

This is a code snippet I found that should do the work but for some reason It doesn’t work.

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' ) )

    // HERE define your shipping class SLUG
    $class_slug = 'large-product';

    // HERE define the shipping method to hide
    $method_key_id = 'flat_rate:13';

    // Checking in cart items
    foreach( WC()->cart->get_cart() as $cart_item ){
        // If we find the shipping class
        if( $cart_item['data']->get_shipping_class() == $class_slug ){
            unset($rates[$method_key_id]); // Remove the targeted method
            break; // Stop the loop
    return $rates;

I inserted this code at the bottom of the functions.php file.
Hope someone can help, thanks in advance.

Source: Ask PHP