Last active
September 5, 2024 13:38
-
-
Save nicomollet/81bd851ad7a74342c4cc2dff6073360b to your computer and use it in GitHub Desktop.
WooCommerce: hide shipping when products marked as "local pickup only" are in the cart
This file contains hidden or 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 | |
| /** | |
| * WooCommerce: hide shipping when products marked as "local pickup only" are in the cart | |
| * Tested with WordPress 5.3.4 and WooCommerce 4.2.0 | |
| * | |
| * The shipping class "local-pickup-only" needs to be created first. | |
| * Then assign the products that are have "local pickup only" to this class | |
| * | |
| * @param array $rates | |
| * @param array $package | |
| * | |
| * @return array | |
| */ | |
| function custom_woocommerce_hide_shipping_on_local_pickup_required( $rates, $package ) | |
| { | |
| $shipping_class_local_pickup_only = 'local-pickup-only'; | |
| $local = []; | |
| foreach( $package['contents'] as $item ) | |
| { | |
| $product = $item['data']; | |
| $shipping_class = $product->get_shipping_class(); | |
| if( $shipping_class == $shipping_class_local_pickup_only ) | |
| { | |
| foreach( $rates as $rate_id => $rate ) | |
| { | |
| if( in_array( $rate->method_id, array( 'local_pickup', 'legacy_local_pickup' ) ) ) | |
| { | |
| $local[ $rate_id ] = $rate; | |
| break; | |
| } | |
| } | |
| } | |
| } | |
| return !empty( $local ) ? $local : $rates; | |
| } | |
| add_filter( 'woocommerce_package_rates', 'custom_woocommerce_hide_shipping_on_local_pickup_required', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment