从应答代码中,如果购物车中有另一项没有该配送类ID,并希望根据产品配送类再次显示flat_rate:2,该怎么办?
发布于 2020-09-07 21:57:36
您将使用以下方法:
add_filter( 'woocommerce_package_rates', 'custom_hide_shipping_methods', 10, 2 );
function custom_hide_shipping_methods( $rates, $package ) {
$found = $others = false; // Initializing
$shipping_class_id = 513; // <== ID OF YOUR SHIPPING_CLASS
$shipping_rate_id = 'flat_rate:2'; // <== Targeted shipping rate ID
// Checking cart items for current package
foreach( $package['contents'] as $key => $cart_item ) {
$product = $cart_item['data']; // The WC_Product Object
if( $product->get_shipping_class_id() == $shipping_class_id ) {
$found = true;
} else {
$others = true;
}
}
if( $found && ! $others && isset($rates[$shipping_rate_id]) ) {
unset($rates[$shipping_rate_id]); // Removing specific shipping method
}
return $rates;
}代码在您的活动子主题(或活动主题)的functions.php文件中。应该行得通。
刷新运输缓存:
在传送区域设置中的
,您已经完成了,您可以测试它。
https://stackoverflow.com/questions/63775928
复制相似问题