Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.17 KB | None | 0 0
  1. add_filter('woocommerce_package_rates', 'wf_hide_shipping_method_based_on_shipping_class', 10, 2);
  2.  
  3. function wf_hide_shipping_method_based_on_shipping_class($available_shipping_methods, $package)
  4. {
  5. $hide_when_shipping_class_exist = array(
  6. 92 => array(
  7. 'flat_rate:7'
  8. )
  9. );
  10.  
  11. $shipping_class_in_cart = array();
  12. foreach(WC()->cart->cart_contents as $key => $values) {
  13. $shipping_class_in_cart[] = $values['data']->get_shipping_class_id();
  14. }
  15.  
  16. foreach($hide_when_shipping_class_exist as $class_id => $methods) {
  17. if(in_array($class_id, $shipping_class_in_cart)){
  18. foreach($methods as & $current_method) {
  19. unset($available_shipping_methods[$current_method]);
  20. }
  21. }
  22. }
  23.  
  24. return $available_shipping_methods;
  25. }
  26.  
  27. add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_shipping_class', 10, 2 );
  28. function hide_shipping_method_based_on_shipping_class( $rates, $package )
  29. {
  30. if ( is_admin() && ! defined( 'DOING_AJAX' ) )
  31. return;
  32.  
  33. // HERE define your shipping class to find
  34. $class = 92;
  35.  
  36. // HERE define the shipping method to hide
  37. $method_key_id = 'flat_rate:7';
  38.  
  39. // Checking in cart items
  40. foreach( $package['contents'] as $item ){
  41. // If we find the shipping class
  42. if( $item['data']->get_shipping_class_id() == $class ){
  43. unset($rates[$method_key_id]); // Remove the targeted method
  44. break; // Stop the loop
  45. }
  46. }
  47. return $rates;
  48. }
  49.  
  50. add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_shipping_class', 10, 2 );
  51. function hide_shipping_method_based_on_shipping_class( $rates, $package )
  52. {
  53. if ( is_admin() && ! defined( 'DOING_AJAX' ) )
  54. return;
  55.  
  56. // HERE define your shipping class to find
  57. $class = 92;
  58.  
  59. // HERE define the shipping methods you want to hide
  60. $method_key_ids = array('flat_rate:7', 'local_pickup:3');
  61.  
  62. // Checking in cart items
  63. foreach( $package['contents'] as $item ) {
  64. // If we find the shipping class
  65. if( $item['data']->get_shipping_class_id() == $class ){
  66. foreach( $method_key_ids as $method_key_id ){
  67. unset($rates[$method_key_id]); // Remove the targeted methods
  68. }
  69. break; // Stop the loop
  70. }
  71. }
  72. return $rates;
  73. }
  74.  
  75. // UNSET A SHIPPING METHOD FOR PACKAGE BASED ON THE SHIPPING CLASS(es) OF ITS CONTENTS
  76. add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_shipping_class', 10, 2 );
  77. function hide_shipping_method_based_on_shipping_class( $rates, $package )
  78. {
  79. if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
  80. return;
  81. }
  82.  
  83. foreach( $package['contents'] as $package_item ){ // Look at the shipping class of each item in package
  84.  
  85. $product_id = $package_item['product_id']; // Grab product_id
  86. $_product = wc_get_product( $product_id ); // Get product info using that id
  87.  
  88. if( $_product->get_shipping_class_id() != 371 ){ // If we DON'T find this shipping class ID
  89. unset($rates['wbs:9:dae98e94_free_ups_ground']); // Then remove this shipping method
  90. break; // Stop the loop, since we've already removed the shipping method from this package
  91. }
  92. }
  93. return $rates;
  94. }
  95.  
  96. // UNSET A SHIPPING METHOD FOR PACKAGE BASED ON THE SHIPPING CLASS(es) OF ITS CONTENTS
  97. add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_shipping_class', 10, 2 );
  98. function hide_shipping_method_based_on_shipping_class( $rates, $package )
  99. {
  100. if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
  101. return;
  102. }
  103.  
  104. foreach( $package['contents'] as $package_item ){ // Look at the shipping class of each item in package
  105.  
  106. $product_id = $package_item['product_id']; // Grab product_id
  107. $_product = wc_get_product( $product_id ); // Get product info using that id
  108.  
  109. if( $_product->get_shipping_class_id() == 92 ){ // If we DO find this shipping class ID
  110. unset($rates['flat_rate:7']); // Then remove this shipping method
  111. break; // Stop the loop, since we've already removed the shipping method from this package
  112. }
  113. }
  114. return $rates;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement