To add a fee when the Cash On Delivery (COD) payment method is selected in WooCommerce, you can use a custom PHP code snippet. You'll need to add this code to your theme's functions.php file
or
preferably use a custom plugin or a plugin like "Code Snippets" to avoid losing changes during theme updates. Here's a sample code snippet for adding a fee when COD is selected:
PHP Code Here
//cod fee
add_action( 'woocommerce_cart_calculate_fees', 'cod_add_checkout_fee_for_gateway' );
function cod_add_checkout_fee_for_gateway() {
$chosen_gateway = WC()->session->get( 'chosen_payment_method' );
if ( $chosen_gateway == 'cod' ) {
WC()->cart->add_fee( ' Utánvétes fizetés Fee', 500 );
}
}
add_action( 'woocommerce_after_checkout_form', 'cod_refresh_checkout_on_payment_methods_change' );
function cod_refresh_checkout_on_payment_methods_change(){
wc_enqueue_js( "
$( 'form.checkout' ).on( 'change', 'input[name^=\'payment_method\']', function() {
$('body').trigger('update_checkout');
});
");
}
Please make sure to test this code on a staging or development site before applying it to your live site to ensure it works as expected and doesn't interfere with other functionalities. Additionally, remember to clear any caching mechanisms you might have in place to see the changes reflected immediately.

0 Comments