1

I am trying to make custom checkout fees if there is more items in cart than 5 or 10, but I also have to exclude categories "tickets" and "vouchers" from count. I found good code examples but none with a negative category selection and I really don't want to set the rules manually for all my 20+ categories like in this post, it would be best to apply it for all EXCEPT an array of 2 categories.

Here is my code that doesn't work:

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

What's wrong with it? Any code corrections will be greatly appreciated. Thank you.

Edit: I ended up using this answer: WooCommerce Quick cart fee

  • Does this answer your question? [WooCommerce Quick cart fee](https://stackoverflow.com/questions/46084005/woocommerce-quick-cart-fee) – tripleee Nov 16 '22 at 07:57

1 Answers1

0

Can you please try below code, I have revised your code little bit:

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);
}
Kairav Thakar
  • 921
  • 1
  • 7
  • Thank you for the suggestion! Unfortunately it doesn't work yet. 1) it displays cart item count as an echo on top of every page instead of only using it for calculations, 2) I see that echo change when I add more items but not when I add multiple items of same type, not what im going for, i need to count 100 different books the same as 100 copies of the same book. Thank you for your help. Do you have any additional suggestions? – user1381985 Nov 09 '22 at 06:34
  • Can you please let me know you want to count the quantity for the products OR only the products counts without quantity? Also, I would like you to suggest please remove the echo and do the testing that if you getting handling fee as an option in the cart and the checkout page. – Kairav Thakar Nov 09 '22 at 06:38
  • Sure, so I'd like to count quantity for the products. If I sell 10 different books I want to count 10 copies of book A the same as 10 different books 1 of each. As for the echo I confirm it does not display on page after I commented out "echo $cart_item_count;", fee still works however. Thank you for this suggestion. We are getting close! – user1381985 Nov 09 '22 at 09:37
  • Then I find here, that you need to put all the if else condition in the foreach loop and you will get your Handling fee. – Kairav Thakar Nov 10 '22 at 07:23
  • How exactly? I tried but it doesn't work, maybe I'm applying your instructions incorrectly... If possible please edit your code to reflect the requested changes. – user1381985 Nov 12 '22 at 12:36