隐藏WooCommerce中特定运输类别的所有统一费率运输方法

我正在设置一个运送网站,但我收集了所有运送区域中的物品,并且可以在所有运送区域中将物品发送出去。

因此,我为所有区域设置了运输课程。

我正在使用“在woocommerce中隐藏特定运输类别的运输方法”答案代码,这就是我所需要的。

但是,除了输入每个flat_rate id之外,还没有一种方法可以定位所有“统一费率”送货方式,因此,当我添加其他“统一费率”送货设置时,它将可以使用,而无需更改代码。

我希望你明白我的追求。任何帮助表示赞赏。


千巷猫影
浏览 210回答 1
1回答

米琪卡哇伊

要将其用于所有“统一费率”送货方式和某些其他定义的送货方式,请改用以下内容: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 (others than "flat rate")    $method_key_ids = array('local_pickup:3');    $found = false;    // Checking in cart items    foreach( $package['contents'] as $cart_item ){        // If we find the shipping class        if( $cart_item['data']->get_shipping_class_id() == $class ){            $found = true;            break; // Stop the loop        }    }    if( ! $found )         return $rates;    // Loop through shipping methods    foreach( $rates as $rate_key => $rate ) {        // Targetting "Flat rate" and other defined shipping mehods        if( 'flat_rate' === $rate->method_id || in_array($rate->id, $method_key_ids) ) {            unset($rates[$rate_key]);        }    }       return $rates;}代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试和工作。刷新运输缓存:( 必填)该代码已保存在活动主题的function.php文件中。购物车是空的在运送区域设置中,禁用/保存任何运送方法,然后启用返回/保存。
打开App,查看更多内容
随时随地看视频慕课网APP