Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save forsvunnet/b1519a178cec5675a1f921dbebb4cac3 to your computer and use it in GitHub Desktop.
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
<?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