根据您上一个问题代码,这里是计算不同产品类别的项目的方法。正如您将看到的,代码紧凑、优化且更高效。
您还需要了解钩子在使用时是如何工作的:
最后两个参数是可选的……默认情况下,优先级是10
,参数计数是:
在过滤器挂钩中,第一个函数参数(变量)总是在函数末尾返回。
有多种可能性:
1)为每个产品类别计数(项目数量计数)添加多个费用:
add_action( 'woocommerce_cart_calculate_fees', 'custom_packing_fees' );
function custom_packing_fees( $cart ) {
if ( is_admin() && !defined('DOING_AJAX') )
return;
if ( did_action('woocommerce_cart_calculate_fees') >= 2 )
return;
// Initializing data (settings)
$data = [
['name' => __('Cupcake'), 'threshold' => 4, 'fee' => 15, 'count' => 0],
['name' => __('Cake'), 'threshold' => 3, 'fee' => 11, 'count' => 0],
['name' => __('Macaron'), 'threshold' => 6, 'fee' => 12, 'count' => 0],
];
$fee_text = __('"%s" box fee (%d items)');
// Loop through cart items (counting product categories)
foreach ( $cart->get_cart() as $item ) {
// Loop through product categories
foreach ( $data as $key => $values ) {
if ( has_term( $values['name'], 'product_cat', $item['product_id'] ) ) {
// Increase the product category count (based on quantity)
$data[$key]['count'] += (int) $item['quantity'];
}
}
}
// Loop through product categories counts
foreach ( $data as $key => $values ) {
// Add a fee for each product category (when the count threshold value is reached)
if( $values['count'] >= $values['threshold'] ) {
$cart->add_fee( sprintf( $fee_text, $values['name'], $values['count'] ), $values['fee'], false );
}
}
}
2)为每个产品类别计数添加多项费用 (购物车项目计数,而不是数量):
add_action( 'woocommerce_cart_calculate_fees', 'custom_packing_fees' );
function custom_packing_fees( $cart ) {
if ( is_admin() && !defined('DOING_AJAX') )
return;
if ( did_action('woocommerce_cart_calculate_fees') >= 2 )
return;
// Initializing data (settings)
$data = [
['name' => __('Cupcake'), 'threshold' => 4, 'fee' => 15, 'count' => 0],
['name' => __('Cake'), 'threshold' => 3, 'fee' => 11, 'count' => 0],
['name' => __('Macaron'), 'threshold' => 6, 'fee' => 12, 'count' => 0],
];
$fee_text = __('"%s" box fee (%d items)');
// Loop through cart items (counting product categories)
foreach ( $cart->get_cart() as $item ) {
// Loop through product categories
foreach ( $data as $key => $values ) {
if ( has_term( $values['name'], 'product_cat', $item['product_id'] ) ) {
// Increase the product category count (based on cart item count)
$data[$key]['count'] += 1;
}
}
}
// Loop through product categories counts
foreach ( $data as $key => $values ) {
// Add a fee for each product category (when the count threshold value is reached)
if( $values['count'] >= $values['threshold'] ) {
$cart->add_fee( sprintf( $fee_text, $values['name'], $values['count'] ), $values['fee'], false );
}
}
}
3)为所有产品类别计数(项目数量计数)添加唯一费用:
add_action( 'woocommerce_cart_calculate_fees', 'custom_packing_fees' );
function custom_packing_fees( $cart ) {
if ( is_admin() && !defined('DOING_AJAX') )
return;
if ( did_action('woocommerce_cart_calculate_fees') >= 2 )
return;
// Initializing data (settings)
$data = [
['name' => __('Cupcake'), 'threshold' => 4, 'fee' => 15, 'count' => 0],
['name' => __('Cake'), 'threshold' => 3, 'fee' => 11, 'count' => 0],
['name' => __('Macaron'), 'threshold' => 6, 'fee' => 12, 'count' => 0],
];
$fee_text = __('Box fee (%d items)');
$fee_amount = 0;
$total_count = 0;
// Loop through cart items (counting product categories)
foreach ( $cart->get_cart() as $item ) {
// Loop through product categories
foreach ( $data as $key => $values ) {
if ( has_term( $values['name'], 'product_cat', $item['product_id'] ) ) {
// Increase the product category count (based on quantity)
$data[$key]['count'] += (int) $item['quantity'];
}
}
}
// Loop through product categories counts
foreach ( $data as $key => $values ) {
// Calculate the fee amount for all product categories (when the count threshold value is reached)
if( $values['count'] >= $values['threshold'] ) {
$fee_amount += $values['fee'];
$total_count += $values['count'];
}
}
// The unique fee merged
if ( $fee_amount > 0 ) {
$cart->add_fee( sprintf( $fee_text, $total_count ), $fee_amount, false );
}
}
代码在您的活动子主题(或活动主题)的functions.php 文件中。测试和工作。