简体   繁体   English

基于 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 "基于WooCommerce 中特定产品类别的最小购物车商品数量 WooCommerce 中基于购物车商品数量的附加价格

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我正在尝试计算结帐页面中特定产品类别中的产品,我只需要一个代码来计算其类别中的产品,如图片所示,如果购物车中有 2 个“耳机”类别的产品,则在总价中添加 2 美元

This image will explain everything:这张图片将解释一切:

在此处输入图片说明

Here is my code attempt:这是我的代码尝试:

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 );
    }

But it doesn't work.但它不起作用。

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:您还需要了解钩子在使用时如何工作:

The 2 last arguments are optional… By default the priority is 10 and the arguments count is:最后 2 个参数是可选的……默认情况下,优先级10参数计数为:

  • 0 for an action hook. 0表示动作挂钩。
  • 1 for a filter hook. 1用于过滤钩。

In a filter hook, the first function argument (variable) is always returned at the end of the function.在过滤器钩子中,第一个函数参数(变量)总是在函数末尾返回。


There are multiple possibilities:有多种可能:

1) Adding multiple fees for each product category count (item quantity count) : 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) Adding multiple fees for each product category count ( cart item count , not quantity) : 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) Adding a unique fee for all product categories count (item quantity count) : 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 );
    }
}

Code goes in functions.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 functions.php 文件中。 Tested and works.测试和工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为 WooCommerce 中特定产品类别的购物车项目自动添加产品 - Auto add a product for cart item from specific product categories in WooCommerce Woocommerce 中定义的产品类别中只允许购物车中的一件商品 - Allow only one item in cart from defined product categories in Woocommerce 禁用 Woocommerce 中特定类别的购物车项目的其他产品类别 - Disable other product categories for a cart item from specific category in Woocommerce 根据Woocommerce中购物车的数量增加产品价格的额外成本 - Add an extra cost to product price based on cart item count in Woocommerce 根据子产品类别限制 WooCommerce 购物车 - Limit WooCommerce cart based on child product categories Woocommerce根据购物车数量和购物车类别强制免费送货 - Woocommerce force free delivery based on cart count and cart categories Ajax上的JS警报添加到购物车中Woocommerce中多个产品类别的计数 - JS alert on ajax add to cart for multiple product categories count in Woocommerce 更改Woocommerce中特定产品类别的购物车项目价格 - Change cart item prices for specific product categories in Woocommerce WooCommerce 中特定产品类别的最小购物车商品数量 - Minimum cart item quantity for specific product categories in WooCommerce 显示特定产品类别的 WooCommerce 购物车项目简短描述 - Display WooCommerce cart item short description for a specific product categories
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM