我同意Pat的意见:最简单的方法是在两个代码片段中的一个片段中重命名函数,在函数定义和add\\u过滤器行中更改名称。但是,如果您确实想将这两者结合到一个片段中,可以执行以下操作:
function businessbloomer_hide_free_shipping_for_shipping_class( $rates, $package ) {
$deluxe_tables_shipping_class_target = \'1173\';
$stag_and_doe_shipping_class_target = \'1750\';
$hide_local_pickup = false;
$hide_delivery_shipping = false;
foreach ( WC()->cart->get_cart_contents() as $key => $values ) {
$shipping_class_id = $values[ \'data\' ]->get_shipping_class_id();
if ( $deluxe_tables_shipping_class_target == $shipping_class_id ) {
$hide_local_pickup = true;
} elseif ( $stag_and_doe_shipping_class_target == $shipping_class_id ) {
$hide_delivery_shipping = true;
}
}
if ( $hide_local_pickup ) {
// This is Local Pick up shipping method with ID
unset( $rates[\'local_pickup:45\'] );
}
if ( $hide_delivery_shipping ) {
// This is Delivery shipping method with ID
unset( $rates[\'WB_Custom_WooCommerce_Shipping_Method52\'] );
}
return $rates;
}
add_filter( \'woocommerce_package_rates\',
\'businessbloomer_hide_free_shipping_for_shipping_class\', 10, 2 );
(您还可以将代码更改为在两个
== $shipping_class_id
行,但我不知道WooCommerce是否将该ID存储为字符串或数字,因此==更安全。)