4

In WooCommerce, I am trying to figured out how to add a "Handling Fee" to every order when no coupons or promo codes are applied to cart.

Here's my "Fee" or "Handling Charge" code:

    add_action( 'woocommerce_cart_calculate_fees','endo_handling_fee' );

function endo_handling_fee() {

global $woocommerce;

     if ( is_admin() && ! defined( 'DOING_AJAX' ) )
          return;

     $fee = 2.00;
     $woocommerce->cart->add_fee( 'Handling', $fee, true, 'standard' );
}

Any ideas?

Thanks

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
miic
  • 43
  • 5

1 Answers1

3

Here I get the array of cart applied coupons and if there is no coupons applied to cart, then a fee is applied to cart.

Here is that code:

add_action( 'woocommerce_cart_calculate_fees','conditional_handling_fee' );
function conditional_handling_fee() {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Get the applied coupons + the count (in cart)
    $applied_coupons_arr = WC()->cart->get_applied_coupons();
    $applied_coupons_count = count($applied_coupons_arr);

    $fee = 2.00;

    if( 0 == $applied_coupons_count )
        WC()->cart->add_fee( 'Handling - '.$applied_coupons_count, $fee, true, 'standard' );

}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

This code is tested and works.


Reference: WC_Cart class - get_applied_coupons() method

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thanks @LoicTheAztec for the suggestion, however this is not working for me, in my functions.php in my child theme, replacing the code I have above - the fee doesn't show up at all. – miic Feb 24 '17 at 02:35
  • Thanks for updating! But, I think I wasn't clear. What I need to do is every order will have a $2.00 fee and what I need is to allow any $ coupon to be able to applied to the whole order including applying to this "fee" at checkout. So basically a coupon will remove the fee charge not add it. Merci! – miic Feb 24 '17 at 16:46