Carlos Gomez
Carlos Gomez

Reputation: 75

Hide shipping methods by product category and if not

I am trying to put together a function to hide a shipping method if there is a finished product category in the cart and if that category is not in the cart then hide another one. This is the function that I have been able to put together, but it doesn't work. Can you help me please?

function product_category_find_id_in_cart() {
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ){

    if ( has_term( 'hangers', 'product_cat', $cart_item['product_id'] ) ) {

    unset( $rates['free_shipping1'] );

    }
    else {
      unset( $rates['flat_rate:6'] );
   }

   return $rates;

}
}

I need it to also work for various shipping methods, not just to hide one. Thank you!

Thanks to @ 7uc1f3r I have a new code that hides the shipping method if the product belongs to a category, but I need that if the product does not belong to that category other shipping methods are hidden. This would be the new formula, thanks to @LoicTheAztec:

add_filter( 'woocommerce_package_rates', 'specific_products_shipping_methods', 10, 2 );
function specific_products_shipping_methods( $rates, $package ) {
    // HERE set the product category in the array (ID, slug or name)
    $terms = array( 'hangers' ); 
    $taxonomy = 'product_cat'; 

    // HERE set the shipping methods to be removed (like "fat_rate:5")
    $method_instances_ids = array('1');  

    $found = false;

    // Loop through cart items checking for defined product IDs
    foreach( $package['contents'] as $cart_item ) {
        if ( has_term( $terms, $taxonomy, $cart_item['product_id'] ) ){
            $found = true;
            break;
        }
    }

    if ( ! $found ) return $rates; // If not found we exit

    // Loop through your active shipping methods
    foreach( $rates as $rate_id => $rate ) {
        // Remove all other shipping methods other than your defined shipping method
        if ( in_array( $rate_id, $method_instances_ids ) ){
            unset( $rates[$rate_id] );
        }
    }    

    return $rates;
}

Upvotes: 1

Views: 1442

Answers (1)

Carlos Gomez
Carlos Gomez

Reputation: 75

I found it!!

add_filter( 'woocommerce_package_rates', 'specific_products_shipping_methods', 10, 2 );
function specific_products_shipping_methods( $rates, $package ) {
    // HERE set the product category in the array (ID, slug or name)
    $terms = array( 'hangers' ); 
    $taxonomy = 'product_cat'; 

    // HERE set the shipping methods to be removed (like "fat_rate:5")
    $method_instances_ids = array('flat_rate:12','flat_rate:13','flat_rate:14');  

    $found = false;

    // Loop through cart items checking for defined product IDs
    foreach( $package['contents'] as $cart_item ) {
        if ( has_term( $terms, $taxonomy, $cart_item['product_id'] ) ){
            $found = true;
            break;
        }
    }

    if ( ! $found ) { unset( $rates['flat_rate:6'],$rates['flat_rate:7'],$rates['flat_rate:8'],$rates['flat_rate:9'],$rates['flat_rate:10'],$rates['flat_rate:11'] ); return $rates;} // If not found we exit
    else {
    // Loop through your active shipping methods
    foreach( $rates as $rate_id => $rate ) {
        // Remove all other shipping methods other than your defined shipping method
        if ( in_array( $rate_id, $method_instances_ids ) ){
            unset( $rates[$rate_id] );
        }
    }    

    return $rates;
}}

Upvotes: 1

Related Questions