Giann Dall Giann Dall - 2 days ago 4
PHP Question

WooCommerce : Add fee for Cash on Delivery payment method (cod)

I am building a WooCommerce site and I need to apply a custom handling fee for a specific payment gateway.

I have this piece of code from here: How to Add Handling Fee to WooCommerce Checkout.

This is my code:

add_action( 'woocommerce_cart_calculate_fees','endo_handling_fee' );
function endo_handling_fee() {
global $woocommerce;

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

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


This function add a fee to all transactions

Is it possible to tweek this function and make it apply for Cash on Delivery payments only ?

I will welcome any alternative method as well. I know about the similar "Payment Gateway Based Fees" woo plugin, but I can't afford it.

Thanks.

Answer

This is not possible, sorry…

Explanations:

  • The problem is that Cash On Delivery (cod) payment method is only available in the next step after cart: On Checkout page.

  • It's not possible to set or to get any payment method on cart page, because you can't know before checkout, what payment method is going to be choosed by the costumer.

This function (your code) can't be tweaked as you would like.

For that purpose it will be necessary that the costumer chose the payment method on cart page, what is absolutely not the WooCommerce behavior.

Comments