The products in my website are handled by one of that 2 shipping plugins: Printful Integration for WooCommerce and Printify for WooCommerce Shipping. when there is mixed items from each shipping plugin. Those plugins split each one the shipping package in two when there is mixed items (which is a a conflict and a problem).
So I have added a shipping class 'printful'
(which id is 548
) to the products that are handled by the Printful plugin, and tried to adjust Hide shipping method for specific shipping classes in woocommerce answer code by @LoicTheAzec (cheers), to only remove the shipping method from a specific duplicated shipping packages with ids 2 and 3 due to the conflict between the shipping plugins…
Here is my actual code:
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;
// HERE define your shipping class to find
$class = 548; //CAMDEN HARBOR CHART MUG is in shipping class
// HERE define the shipping methods you want to hide
$method_key_ids = array('printify_shipping_s', 'printify_shipping_e');
// 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_id() == $class ){
foreach( $method_key_ids as $method_key_id ){
unset($rates[$method_key_id]); // Remove the targeted methods
}
break; // Stop the loop
}
}
return $rates;
}
But it's not working and I still get 4 shipping packages instead of two:
Any help is appreciated.
shipping_method_2_printify_shipping_s
as its just an html tag id… What you are targeting isprintify_shipping_s
orprintify_shipping_e
for the package ID 2. But forwoocommerce_package_rates
filter hook there is no way to target a specific package ID, so the code from my answer will not be useful in your case. I dont have the answer to your question and I can't help. – LoicTheAztec