6

I would like to ask how to add a custom fee to the woocommerce subscription recurring total?

Found this on the web:

function woo_add_cart_fee() {
  global $woocommerce;
  $woocommerce->cart->add_fee( __('Custom', 'woocommerce'), 5 );
}

add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );

However that certain function is just for the regular product. NOT SUBSCRIPTION — it doesn't add the fee to the recurring totals.

Styx
  • 9,863
  • 8
  • 43
  • 53

3 Answers3

0

As of right now, WooCommerce subscriptions doesn't support adding fees to recurring totals.

Nicholas Koskowski
  • 793
  • 1
  • 4
  • 23
0

This may be too late to be of any use to you, but you might find this useful: https://docs.woocommerce.com/document/subscriptions/develop/recurring-cart-fees/

I believe this example would be relevant to your question:

In some cases, you may require the fee to only apply to the on-going recurring payment for any subscriptions. To achieve this, you need to ... add a condition to check whether the cart being passed into your callback is a recurring cart, or the standard WooCommerce cart.

Recurring carts have a $recurring_cart_key property we can use to determine if the cart is a recurring cart.

add_filter( 'woocommerce_cart_calculate_fees', 'add_recurring_postage_fees', 10, 1 );

function add_recurring_postage_fees( $cart ) {
   // Check to see if the recurring_cart_key is set 
   if ( ! empty( $cart->recurring_cart_key ) ) {
       $cart->add_fee( 'Postage', 5 );
    }
}
codebird
  • 357
  • 7
  • 17
0

If someone comes by this question, you can solve it in the following way:

add_filter( 'woocommerce_subscriptions_is_recurring_fee', '__return_true' );

add_filter( 'woocommerce_cart_calculate_fees', 'add_fees', 10 );

function add_fees($cart) {
    $total_fee = $cart->get_subtotal();
    WC()->cart->add_fee( 'Fee', $total_fee * 0.2 );
}

or

add_filter( 'woocommerce_subscriptions_is_recurring_fee', '__return_true' );

add_filter( 'woocommerce_cart_calculate_fees', 'add_fees', 10 );

function add_fees($cart) {
    WC()->cart->add_fee( 'Fee', '10' );
}
Biboo
  • 1