来自WooCommerce产品类别购物车项目计数的费用 [英] Fees based from WooCommerce product categories cart item count
问题描述
基于"和"
Based on "Minimum cart item quantity for a specific product category in WooCommerce" and "Additional price based on cart item count in WooCommerce"
我正在尝试在结帐页面中对特定产品类别中的产品进行计数,我只需要一个代码即可对其类别中的产品进行计数(如图片中所示),并且如果购物车中有2个来自耳机"类别的产品,则添加2美元总价
I am trying to count the products from specific product categories in checkout page, i just need a code that count the products from their category like in picture and if 2 products from 'headphone' category was in the cart then add 2 $ to total price
此图片将解释所有内容:
This image will explain everything:
这是我的代码尝试:
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 );
}
但这是行不通的.
推荐答案
根据您的上一个问题代码,这是对来自不同产品类别的商品进行计数的方法.如您所见,该代码紧凑,优化并且效率更高.
Based on your last question code, here is the way to count items from different product categories. As you will see, the code is compact, optimized and more efficient.
此外,您还需要了解钩子在使用时的工作原理:
Also you need to understand how hooks work when using:
- 功能
add_action( $hook_name, $function_name_to_call, $priority, $args_count )
- 功能
add_filter( $hook_name, $function_name_to_call, $priority, $args_count )
最后2个参数是可选的...默认情况下,优先级是 10
,而自变量计数是:
The 2 last arguments are optional… By default the priority is 10
and the arguments count is:
-
0
以获取动作挂钩. -
1
用于过滤器挂钩.
在过滤器挂钩中,始终在函数末尾返回第一个函数参数(变量).
In a filter hook, the first function argument (variable) is always returned at the end of the function.
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)为每个产品类别计数添加多个费用 (购物车计数,而不是数量):
2) Adding multiple fees for each product category count (cart item count, not quantity):
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文件中.经过测试,可以正常工作.
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
这篇关于来自WooCommerce产品类别购物车项目计数的费用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!