以下遗嘱only show cart中特定产品类别的“本地提货”装运方法:
add_action( \'woocommerce_package_rates\',\'show_hide_local_pickup_shipping_methods\', 10, 2 );
function show_hide_local_pickup_shipping_methods( $rates, $package ) {
// HERE BELOW your product categories in the array
$categories = array( \'t-shirts\', \'hat\' );
$term_found = false;
// Loop through cart items
foreach( $package[\'contents\'] as $cart_item ){
if( has_term( $categories, \'product_cat\', $cart_item[\'product_id\'] ) ) {
$term_found = true;
break;
}
}
// Loop through shipping methods
foreach( $rates as $rate_key => $rate ) {
if( \'local_pickup\' === $rate->method_id && ! $term_found ) {
unset($rates[$rate_key]);
}
}
return $rates;
}
代码进入函数。活动子主题(或活动主题)的php文件。已测试并正常工作。
Note: 您可能需要刷新配送方式、清空购物车并转到配送设置/然后禁用/保存并重新启用/保存任何配送方式。