基于 WooCommerce 产品类别购物车项目计数的费用

Posted

技术标签:

【中文标题】基于 WooCommerce 产品类别购物车项目计数的费用【英文标题】:Fees based from WooCommerce product categories cart item count 【发布时间】:2019-12-19 11:00:27 【问题描述】:

基于 "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 );
    

但它不起作用。

【问题讨论】:

当你说 “我有一些规则,比如用户不能只购买 'consoles' 类别中的 1 个产品,它必须是 3 个产品”,它是 无法理解。此外,“耳机”中的 1 件产品和“耳机”中的 5 件产品加起来都是 2 美元(相同金额)。试着改写你的问题并说清楚。 对不起,忘记了那部分,我只需要一个代码来计算他们类别中的产品,如图所示,如果“耳机”类别中的 2 件产品在购物车中,则在总价中添加 2 美元 无论如何,请先更新/改写您的问题,让大家清楚。 *** 问题必须清晰易懂,因为问题和答案对其他人有用。 @LoicTheAztec 先生,我做到了,你能回答我的问题吗? 【参考方案1】:

根据您的上一个问题代码,以下是计算不同产品类别的商品的方法。如您所见,代码紧凑、优化且更高效。

你还需要了解钩子在使用时是如何工作的:

函数add_action( $hook_name, $function_name_to_call, $priority, $args_count ) 函数add_filter( $hook_name, $function_name_to_call, $priority, $args_count )

最后两个参数是可选的……默认情况下,priority10arguments count 是:

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 文件中。经过测试并且可以工作。

【讨论】:

以上是关于基于 WooCommerce 产品类别购物车项目计数的费用的主要内容,如果未能解决你的问题,请参考以下文章

在 WooCommerce 中最后对特定产品类别的购物车项目进行排序

Woocommerce 按产品类别对购物车产品进行排序

除某些 WooCommerce 产品类别外,自动将产品添加到购物车

在 WooCommerce 中将购物车商品限制为来自同一产品类别

自定义特定 WooCommerce 产品类别上的“添加到购物车”按钮

如何根据产品类别在 WooCommerce 下添加文本添加到购物车按钮