Añadir cuota o descuento en el total del pedido en el checkout de WC

Añadir cuota o descuento en el total del pedido en el checkout de WC

Añadir cuota o descuento en el total del pedido en el checkout de WC.

// Asignar descuento a cuota
add_action( "woocommerce_review_order_before_payment", "eb_refresh_checkout_on_payment_methods_change" );
function eb_add_checkout_fee_for_gateway() {

if ( is_admin() && ! defined( "DOING_AJAX" ) ) {
return;
}

$chosen_gateway = WC()->session->chosen_payment_method;

if ( $chosen_gateway == "bacs" ) {
global $woocommerce;
$percentage = -0.03;
$subtotal = $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total;
$total_sin_iva = $subtotal
$surcharge = $total_sin_iva * $percentage;
$woocommerce->cart->add_fee( "Descuento -3% (Transferencia bancaria)", $surcharge, true, "" );
}
}
add_action( "woocommerce_cart_calculate_fees", "eb_add_checkout_fee_for_gateway" );



// Recargar el checkout
add_action( "woocommerce_review_order_before_payment", "eb_refresh_checkout_on_payment_methods_change" );
function eb_refresh_checkout_on_payment_methods_change(){
?>
<script type="text/javascript">
(function($){
$( "form.checkout" ).on( "change", "input[name^="payment_method"]", function() {
$("body").trigger("update_checkout");
});
})(jQuery);
</script>
<?php
}
add_action( "woocommerce_review_order_before_payment", "eb_refresh_checkout_on_payment_methods_change" );