Hide shipping method only for specific Woocommerce product categories

Question

I tryed the following code, but it wouldnt take my categories but hides flat rate all of my products:

add_action( 'woocommerce_package_rates','show_hide_local_pickup_shipping_methods', 10, 2 );
function show_hide_local_pickup_shipping_methods( $rates, $package ) {

    // HERE BELOW your product categories in the array
    $categories = array( 't-shirts', 'hat' );

    $term_found = false;

    // Loop through cart items
    foreach( $package['contents'] as $cart_item ){
        if( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ) {
            $term_found = true;
            break;
        }
    }

    // Loop through shipping methods
    foreach( $rates as $rate_key => $rate ) {
        if( 'flat_rate' === $rate->method_id && ! $term_found ) {
            unset($rates[$rate_key]);
        }
    }
    return $rates;
}

0
, , , , Pieter Reijnen 3 years 2020-04-03T12:52:05-05:00 0 Answers 94 views 0

Leave an answer

Browse
Browse