繁体   English   中英

WooCommerce 结账费用基于购物车商品数量(不包括类别)

[英]WooCommerce checkout fee based on cart item count with category excluded

如果购物车中的商品多于 5 或 10,我将尝试收取自定义结账费用,但我还必须从计数中排除类别“门票”和“代金券”。 我找到了很好的代码示例,但没有一个带有负面类别选择,我真的不想像这篇文章中那样为我的所有 20 多个类别手动设置规则,最好将它应用于除 2 个类别的数组之外的所有类别.

这是我的代码不起作用:

add_action( 'woocommerce_cart_calculate_fees','woocommerce_cart_extra_cost', 10, 1 );
function woocommerce_cart_extra_cost( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )  return;

// Below your category term ids, slugs or names to be excluded
$excluded_terms = array('tickets', 'vouchers'); 

$cart_item_count    = 0; // Initializing

// Loop through cart items 
foreach ( WC()->cart->get_cart() as $item ) {
    // Excluding some product category from the count
    if ( ! has_term( $excluded_terms, 'product_cat', $item['product_id'] ) ) {
        $items_count += $item['quantity'];
    }
}

// CONDITIONAL ITEMS QUANTITY FEE AMOUNT
if( $cart_item_count < 6 )
    $fee = 0;
elseif( $cart_item_count >= 6 && $cart_item_count < 11 )
    $fee = 3;
elseif( $cart_item_count >= 11 )
    $fee = 5;

if( $fee > 0 )
    $cart->add_fee( __( "Handling Fee", "woocommerce" ), $fee, true);
}

它出什么问题了? 任何代码更正将不胜感激。 谢谢你。

编辑:我最终使用了这个答案: WooCommerce 快速购物车费用

你能试试下面的代码吗,我已经稍微修改了你的代码:

add_action( 'woocommerce_cart_calculate_fees','woocommerce_cart_extra_cost', 10, 1 );
function woocommerce_cart_extra_cost( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )  return;

// Below your category term ids, slugs or names to be excluded
$excluded_terms = array('tickets', 'vouchers');
$fee = 0;
$cart_item_count = count( WC()->cart->get_cart() ); // Initializing

// Loop through cart items 
foreach ( WC()->cart->get_cart() as $item ) {
    // Excluding some product category from the count
    if ( has_term( $excluded_terms, 'product_cat', $item['product_id'] ) ) {
        $cart_item_count = $cart_item_count - 1;
    }
}

// CONDITIONAL ITEMS QUANTITY FEE AMOUNT
if( $cart_item_count < 2 )
    $fee = 6;
elseif( $cart_item_count >= 6 && $cart_item_count < 11 )
    $fee = 3;
elseif( $cart_item_count >= 11 )
    $fee = 5;

if( $fee > 0 )
    $cart->add_fee( __( "Handling Fee", "woocommerce" ), $fee, true);
}

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM