1 回答
TA贡献1847条经验 获得超11个赞
您可以使用以下解释,并在代码中添加注释
此代码必须满足的条件是:
购物车中至少有 3 个订单项
1 个或多个产品属于“类别 1”类别
如果满足前两个条件,“distance_rate”和/或“table_rate”将取消设置
function hide_shipping_count_based( $rates, $package ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
// Count line items
$count = count( $package['contents'] );
// Set variable
$found = false;
// Set term (category)
$term = 'categorie-1';
// Check count
if( $count > 2 ) {
// Loop through line items
foreach( $package['contents'] as $line_item ) {
// Get product id
$product_id = $line_item['product_id'];
// Check for category
if ( has_term( $term, 'product_cat', $product_id ) ) {
$found = true;
break;
}
}
}
// True
if ( $found ) {
// Loop trough rates
foreach ( $rates as $rate_key => $rate ) {
// Targeting
if ( in_array( $rate->method_id, array( 'distance_rate', 'table_rate' ) ) ) {
unset( $rates[$rate_key] );
}
}
}
return $rates;
}
add_filter( 'woocommerce_package_rates', 'hide_shipping_count_based', 100, 2 );
- 1 回答
- 0 关注
- 112 浏览
添加回答
举报