The issue is that PHP does not allow the use of the same function name more than once.
The best way to solve this is to rewrite snippets to use anonymous functions:
add_filter( 'woocommerce_package_rates', function ( $rates, $package ) {
$shipping_class_target = '1173'; // shipping class ID (to find it, see screenshot below)
$in_cart = false;
foreach ( WC()->cart->get_cart_contents() as $key => $values ) {
if ( $values[ 'data' ]->get_shipping_class_id() == $shipping_class_target ) {
$in_cart = true;
break;
}
}
if ( $in_cart ) {
unset( $rates['local_pickup:45'] ); // shipping method with ID (to find it, see screenshot below)
}
return $rates;
}, 10, 2 );
In this particular case, you can actually combine both snippets instead of having two different ones that do almost the same thing:
add_filter( 'woocommerce_package_rates', function ( $rates, $package ) {
$shipping_class_targets = [ '1173', '1750' ]; // list of shipping class IDs (to find it, see screenshot below)
$in_cart = false;
foreach ( WC()->cart->get_cart_contents() as $key => $values ) {
if ( in_array( $values[ 'data' ]->get_shipping_class_id(), $shipping_class_targets ) ) {
$in_cart = true;
break;
}
}
if ( $in_cart ) {
unset( $rates['local_pickup:45'] ); // shipping method with ID (to find it, see screenshot below)
}
return $rates;
}, 10, 2 );
I really appreciate your response! Unfortunately, I tried it, and it did not work. I am not sure if I pasted those snippets correctly before, so let me do it again just to be sure. And then maybe we can see what it needs to be changed to. Also, was I supposed to put both of those snippets into one that you posted above, or just the second on?
Thank you in advance!
add_filter( 'woocommerce_package_rates', 'businessbloomer_hide_free_shipping_for_shipping_class', 10, 2 );
function businessbloomer_hide_free_shipping_for_shipping_class( $rates, $package ) {
$shipping_class_target = '1750'; // This is Stag & Doe shipping class ID
$in_cart = false;
foreach ( WC()->cart->get_cart_contents() as $key => $values ) {
if ( $values[ 'data' ]->get_shipping_class_id() == $shipping_class_target ) {
$in_cart = true;
break;
}
}
if ( $in_cart ) {
unset( $rates['WB_Custom_WooCommerce_Shipping_Method52'] ); // This is Delivery shipping method with ID
}
return $rates;
}
add_filter( 'woocommerce_package_rates', 'businessbloomer_hide_free_shipping_for_shipping_class', 10, 2 );
function businessbloomer_hide_free_shipping_for_shipping_class( $rates, $package ) {
$shipping_class_target = '1173'; // This is Deluxe Tables shipping class ID
$in_cart = false;
foreach ( WC()->cart->get_cart_contents() as $key => $values ) {
if ( $values[ 'data' ]->get_shipping_class_id() == $shipping_class_target ) {
$in_cart = true;
break;
}
}
if ( $in_cart ) {
unset( $rates['local_pickup:45'] ); // This is Local Pick up shipping method with ID
}
return $rates;
}
Hi @jackofallspades,
I would combine them like this:
add_filter( 'woocommerce_package_rates', function ( $rates, $package ) {
foreach ( WC()->cart->get_cart_contents() as $key => $values ) {
$shipping_id = $values['data']->get_shipping_class_id();
// This is Stag & Doe shipping class ID
if ( 1750 === $shipping_id ) {
// This is Delivery shipping method with ID
unset( $rates['WB_Custom_WooCommerce_Shipping_Method52'] );
break;
}
// This is Deluxe Tables shipping class ID
if ( 1173 === $shipping_id ) {
// This is Local Pick up shipping method with ID
unset( $rates['local_pickup:45'] );
break;
}
}
return $rates;
}, 10, 2 );
You are a genius! Do you know how long I have been trying to sort this out?!! Thank you so much!!!!!!!
Glad to hear that it’s working!