基于“ Minimum cart item quantity for a specific product category in WooCommerce” 和“ Additional price based on cart item count in WooCommerce”
我正在尝试在结帐页面中对特定产品类别中的产品进行计数,我只需要一个代码即可对此类产品中的产品进行计数(如图片中所示),并且如果购物车中有2个来自“耳机”类别的产品,则添加2美元总价
此图片将解释所有内容:
这是我的代码尝试:
add_action( 'woocommerce_cart_calculate_fees', 'custom_packing_fee', 10, 1 );
function custom_packing_fee( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_cart_calculate_fees' ) >= 2 )
return;
$count = $cart->get_terms('')
if ( $count->count >= 9 ){
$fee = 15;
}
elseif( $count->count >= 6 && $count < 9 ){
$fee = 14;
}
elseif( $count>count >= 4 && $count < 6 ){
$fee = 13;
}
if ( isset($fee) && $fee > 0 ) {
$label = sprintf( __('Box fee (%d items)'), $count);
$cart->add_fee( $label, $fee, false );
}
但这不起作用。
答案 0 :(得分:1)
根据您的上一个问题代码,这是对来自不同产品类别的商品进行计数的方法。如您所见,该代码紧凑,优化且效率更高。
此外,您还需要了解钩子在使用时的工作原理:
add_action( $hook_name, $function_name_to_call, $priority, $args_count )
add_filter( $hook_name, $function_name_to_call, $priority, $args_count )
最后2个参数是可选的……默认情况下,优先级是 10
,而参数计数是:
0
以获取动作挂钩。1
以获取过滤器挂钩。在过滤器挂钩中,始终在函数末尾返回第一个函数参数(变量)。
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文件中。经过测试,可以正常工作。