如何根据项目数量和特定类别禁用送货方式
How to disable shipping method based on item count and a certain category
我一直在寻找一种有条件地禁用两种送货方式的方法
- table 率
- 距离率
基于项目数。
我指的不是数量,而是购物车中有多少种不同的产品。 IE 2 Lamps and 3 tables in the cart would be an item count of 2 and the combined quantity of 5.
我还想确保此规则仅对特定类别有效。
我试过了:
function hide_shipping_count_based( $rates, $package ) {
// Set count variable
$cart_count = 0;
// Calculate cart's total
foreach( WC()->cart->cart_contents as $key => $value) {
$cart_count ++;
}
// only if the weight is over 150lbs find / remove specific carrier
if( $cart_count > 2 ) {
// loop through all of the available rates
unset( $rates[ 'distance_rate' ] );
unset( $rates[ 'table_rate' ] );
}
return $rates;
}
add_filter( 'woocommerce_package_rates', 'hide_shipping_count_based', 10, 2 );
您可以使用下面的解释,并在代码中添加注释
此代码必须满足的条件是:
- 购物车中至少有 3 个订单项
- 1 个或多个产品属于 'categorie-1' 类别
- 'distance_rate' and/or '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 );
我一直在寻找一种有条件地禁用两种送货方式的方法
- table 率
- 距离率
基于项目数。
我指的不是数量,而是购物车中有多少种不同的产品。 IE 2 Lamps and 3 tables in the cart would be an item count of 2 and the combined quantity of 5.
我还想确保此规则仅对特定类别有效。
我试过了:
function hide_shipping_count_based( $rates, $package ) {
// Set count variable
$cart_count = 0;
// Calculate cart's total
foreach( WC()->cart->cart_contents as $key => $value) {
$cart_count ++;
}
// only if the weight is over 150lbs find / remove specific carrier
if( $cart_count > 2 ) {
// loop through all of the available rates
unset( $rates[ 'distance_rate' ] );
unset( $rates[ 'table_rate' ] );
}
return $rates;
}
add_filter( 'woocommerce_package_rates', 'hide_shipping_count_based', 10, 2 );
您可以使用下面的解释,并在代码中添加注释
此代码必须满足的条件是:
- 购物车中至少有 3 个订单项
- 1 个或多个产品属于 'categorie-1' 类别
- 'distance_rate' and/or '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 );