Created
January 26, 2024 11:03
-
-
Save forsvunnet/b1519a178cec5675a1f921dbebb4cac3 to your computer and use it in GitHub Desktop.
Add a filter for bring shipping rates to exclude mail box services if a shipping class is present in the cart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Filter the bring shipping rates. | |
*/ | |
add_filter('bring_shipping_rates', function( $rates ) { | |
$shipping_classes = [ | |
'no-mailbox', | |
]; | |
// Look for items with a shipping class that doesn not allow mail. | |
$items = array_filter( | |
WC()->cart->get_cart(), | |
fn($item) => !empty($item['data']) && | |
in_array($item['data']->get_shipping_class(), $shipping_classes) | |
); | |
if (empty($items)) { | |
return $rates; | |
} | |
$remove_rates = [ | |
'3584', | |
'3570', | |
]; | |
return array_filter( | |
$rates, | |
fn($rate) => empty($rate['bring_product']) || | |
! in_array($rate['bring_product'], $remove_rates) | |
); | |
}, 999 ); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment