Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. /**
  2. * Hide all free shipping options when the given shipping class is in the cart
  3. * Code snippets should be added to the functions.php file of your child theme
  4. *
  5. * @return array
  6. */
  7. function hide_shipping_when_class_is_in_cart( $rates, $package ) {
  8. // shipping class IDs that need the method removed
  9. $shipping_classes = array('bulky-items');
  10. $if_exists = false;
  11.  
  12. foreach( $package['contents'] as $key => $values ) {
  13. if( in_array( $values[ 'data' ]->get_shipping_class(), $shipping_classes ) )
  14. $if_exists = true;
  15. }
  16.  
  17. if( $if_exists ) {
  18. foreach( $rates as $key => $option ) {
  19. if( $option->method_id == 'free_shipping' )
  20. unset( $rates[ $key ] );
  21. }
  22. }
  23.  
  24. return $rates;
  25. }
  26. add_filter( 'woocommerce_package_rates', 'hide_shipping_when_class_is_in_cart', 10, 2 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement