Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save PierreNodles/1e9426e0c8569bc759671046ff4418ab to your computer and use it in GitHub Desktop.
Save PierreNodles/1e9426e0c8569bc759671046ff4418ab to your computer and use it in GitHub Desktop.
Force local pick up if a product containing a specific shipping class is in the cart
<?php
// Replace 'shipping-class' with the specific shipping class you want to force the local pick up upon
// To make it more readable, replace $shippingClass with your class name, eg : $hugeProducts
function my_hide_shipping_when_local_is_available( $rates ) {
$cart_items = WC()->cart->get_cart();
$shippingClass = false;
foreach ( $cart_items as $cart_item ) {
$product = $cart_item['data'];
$class = $product->get_shipping_class();
if ( 'shipping-class' == $class ) {
$shippingClass = true;
}
}
$local = array();
foreach ( $rates as $rate_id => $rate ) {
if ('local_pickup' === $rate->method_id ) {
$local[ $rate_id ] = $rate;
}
}
if ( !empty($local) && ($shippingClass == true) ) {
return $local;
} else {
return $rates;
}
}
add_filter( 'woocommerce_package_rates', 'my_hide_shipping_when_local_is_available', 100 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment