Created
February 24, 2019 23:01
-
-
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
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 | |
// 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