Created
June 20, 2022 18:38
-
-
Save Nav-Appaiya/abd976b08188efd9fa1b126e50154190 to your computer and use it in GitHub Desktop.
disable shipping for specific products woocommerce php
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
function hide_shipping_methods( $available_shipping_methods, $package ) { | |
$shipping_classes = array( 'some-shipping-class-1', 'some-shipping-class-2' ); | |
$excluded_methods = array( 'free_shipping' ); | |
$shipping_class_exists = false; | |
foreach( $package['contents'] as $key => $value ) | |
if ( in_array( $value['data']->get_shipping_class(), $shipping_classes ) ) { | |
$shipping_class_exists = true; | |
break; | |
} | |
if ( $shipping_class_exists ) { | |
$methods_to_exclude = array(); | |
foreach( $available_shipping_methods as $method => $method_obj ) | |
if ( in_array( $method_obj->method_id, $excluded_methods ) ) | |
$methods_to_exclude[] = $method; | |
if ( $methods_to_exclude ) | |
foreach ( $methods_to_exclude as $method ) | |
unset( $available_shipping_methods[$method] ); | |
} | |
return $available_shipping_methods; | |
} | |
add_filter( 'woocommerce_package_rates', 'hide_shipping_methods', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment