Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_filter('woocommerce_package_rates', 'wf_hide_shipping_method_based_on_shipping_class', 10, 2);
- function wf_hide_shipping_method_based_on_shipping_class($available_shipping_methods, $package)
- {
- $hide_when_shipping_class_exist = array(
- 92 => array(
- 'flat_rate:7'
- )
- );
- $shipping_class_in_cart = array();
- foreach(WC()->cart->cart_contents as $key => $values) {
- $shipping_class_in_cart[] = $values['data']->get_shipping_class_id();
- }
- foreach($hide_when_shipping_class_exist as $class_id => $methods) {
- if(in_array($class_id, $shipping_class_in_cart)){
- foreach($methods as & $current_method) {
- unset($available_shipping_methods[$current_method]);
- }
- }
- }
- return $available_shipping_methods;
- }
- add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_shipping_class', 10, 2 );
- function hide_shipping_method_based_on_shipping_class( $rates, $package )
- {
- if ( is_admin() && ! defined( 'DOING_AJAX' ) )
- return;
- // HERE define your shipping class to find
- $class = 92;
- // HERE define the shipping method to hide
- $method_key_id = 'flat_rate:7';
- // Checking in cart items
- foreach( $package['contents'] as $item ){
- // If we find the shipping class
- if( $item['data']->get_shipping_class_id() == $class ){
- unset($rates[$method_key_id]); // Remove the targeted method
- break; // Stop the loop
- }
- }
- return $rates;
- }
- add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_shipping_class', 10, 2 );
- function hide_shipping_method_based_on_shipping_class( $rates, $package )
- {
- if ( is_admin() && ! defined( 'DOING_AJAX' ) )
- return;
- // HERE define your shipping class to find
- $class = 92;
- // HERE define the shipping methods you want to hide
- $method_key_ids = array('flat_rate:7', 'local_pickup:3');
- // Checking in cart items
- foreach( $package['contents'] as $item ) {
- // If we find the shipping class
- if( $item['data']->get_shipping_class_id() == $class ){
- foreach( $method_key_ids as $method_key_id ){
- unset($rates[$method_key_id]); // Remove the targeted methods
- }
- break; // Stop the loop
- }
- }
- return $rates;
- }
- // UNSET A SHIPPING METHOD FOR PACKAGE BASED ON THE SHIPPING CLASS(es) OF ITS CONTENTS
- add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_shipping_class', 10, 2 );
- function hide_shipping_method_based_on_shipping_class( $rates, $package )
- {
- if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
- return;
- }
- foreach( $package['contents'] as $package_item ){ // Look at the shipping class of each item in package
- $product_id = $package_item['product_id']; // Grab product_id
- $_product = wc_get_product( $product_id ); // Get product info using that id
- if( $_product->get_shipping_class_id() != 371 ){ // If we DON'T find this shipping class ID
- unset($rates['wbs:9:dae98e94_free_ups_ground']); // Then remove this shipping method
- break; // Stop the loop, since we've already removed the shipping method from this package
- }
- }
- return $rates;
- }
- // UNSET A SHIPPING METHOD FOR PACKAGE BASED ON THE SHIPPING CLASS(es) OF ITS CONTENTS
- add_filter( 'woocommerce_package_rates', 'hide_shipping_method_based_on_shipping_class', 10, 2 );
- function hide_shipping_method_based_on_shipping_class( $rates, $package )
- {
- if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
- return;
- }
- foreach( $package['contents'] as $package_item ){ // Look at the shipping class of each item in package
- $product_id = $package_item['product_id']; // Grab product_id
- $_product = wc_get_product( $product_id ); // Get product info using that id
- if( $_product->get_shipping_class_id() == 92 ){ // If we DO find this shipping class ID
- unset($rates['flat_rate:7']); // Then remove this shipping method
- break; // Stop the loop, since we've already removed the shipping method from this package
- }
- }
- return $rates;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement