0

I'd like to be able to add a fixed $20 fee if a given coupon code (100% off) is applied to the cart. The fee should also not have taxes applied. I'm offering a try at home service for $20, so a certain coupon would discount the cart and all products by 100% but add a $20 fixed fee. Any help is much appreciated!

JC

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
jcooksey80
  • 11
  • 2
  • For your information fee lines in Woocommerce are always added by default below shipping lines and there is no other way when adding a fee to Woocommerce. You can change that behavior only if you customize the related templates *(which is another question)*. – LoicTheAztec Nov 12 '18 at 01:25
  • That would be great but the code your provided added $20 to the Shipping line not a Fee line. – jcooksey80 Nov 12 '18 at 01:25
  • Sorry but I am using [`WC_Cart` `add_fee()`](https://docs.woocommerce.com/wc-apidocs/class-WC_Cart.html#_add_fee) method, which adds a FEE and NOT a SHIPPING METHOD… There is no other ways or methods to add a fee. The display problem that you have is due to your theme, a customization that you have made or a third party plugin. But it's not due to my code. – LoicTheAztec Nov 12 '18 at 01:28

1 Answers1

1

The following code will apply a fee of $20 if a specific coupon code has been applied to cart:

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

    // HERE set your targeted coupon code (100 % off)
    $coupon_code = 'thatsforfree';

    // Check if our targeted coupon is applied
    if( in_array( wc_format_coupon_code( $coupon_code ), $cart->get_applied_coupons() ) ){
        $title = __('Fee', 'woocommerce'); // The fee title
        $cost  = 20; // The fee amount

        // Adding the fee (not taxable)
        $cart->add_fee( $title, $cost, false );
    }
}

Code goes in function.php file of your active child theme (active theme). Tested and works.

The display in cart and checkout pages (on Woocommerce storefront theme):

enter image description here

This code uses The Woocommerce FEE API using WC_Cart method add_fee() with the dedicated action hook woocommerce_cart_calculate_fees.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thanks so much for the quick response! The code is adding a $20 fee, however, it is adding it on the Shipping line as a Flat Rate. Is there a way to make this show as a separate line with the fee title? Thanks! – jcooksey80 Nov 11 '18 at 22:45
  • So there is no way to create a new line? This will give customers the impression they are paying for shipping, when shipping is to be free. A bit confusing. Any workarounds for a new line? Thanks! – jcooksey80 Nov 11 '18 at 22:53
  • I would accept the answer, but this code did not result in what I wanted. It added the fee in the shipping line. – jcooksey80 Nov 12 '18 at 01:13
  • @loictheaztec is there a way to add a css script to your PHP? Your solution works perfectly for me, but I'd like to use CSS to hide the default shipping options when your code runs. Thanks – Mr N Dynamite Nov 02 '19 at 17:18