Why no longer running? Woocommerce

Well I did had this code on functions.php for a woocommerce in wordpress.
Basically is a checkbox so when checked or unchecked it adds or doesnt the percentage fee in adition to the normal tax (is a kind of special tax in Spain).
But since like a year it no longer works and I don’t know why. I tried to change variables names or even delete plugins who may interact with fees but… it just doesn’t work and few years ago it worked. Or maybe is probably the code itself.

The code is:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>
// Agregar un nuevo campo al formulario de pago
add_action( 'woocommerce_review_order_before_payment', 'agregar_checkbox_recargo_equivalencia' );
function agregar_checkbox_recargo_equivalencia() {
echo '<div id="recargo-equivalencia-checkbox"><h3>' . __('Recargo de Equivalencia', 'woocommerce') . '</h3>';
woocommerce_form_field( 'recargo_equivalencia1', array(
'type' => 'checkbox',
'class' => array('input-checkbox'),
'label' => __('Estoy acogido al recargo de equivalencia (+5,2%)', 'woocommerce'),
), WC()->session->get( 'recargo_equivalencia1' ));
echo '</div>';
}
// Guardar el estado del checkbox en la sesión
add_action( 'woocommerce_checkout_update_order_meta', 'guardar_checkbox_recargo_equivalencia' );
function guardar_checkbox_recargo_equivalencia( $order_id ) {
if ( !empty( $_POST['recargo_equivalencia1'] ) ) {
WC()->session->set( 'recargo_equivalencia1', '1' );
} else {
WC()->session->__unset( 'recargo_equivalencia1', '0');
}
}
// Aplicar el recargo de equivalencia al total de los productos si se ha marcado el checkbox
add_action( 'woocommerce_cart_calculate_fees', 'aplicar_recargo_equivalencia' );
function aplicar_recargo_equivalencia() {
if ( WC()->session->get( 'recargo_equivalencia1' ) === '1' ) {
$cart1 = WC()->cart->get_cart();
$total_productos1 = 0;
// Sumar el total de los productos en el carrito
foreach ( $cart1 as $cart_item1 ) {
$total_productos1 += $cart_item1['line_total'];
}
$recargo1 = $total_productos1 * 0.052; // 5.2% del total de productos
WC()->cart->add_fee( ___ ('RE', 'woocommerce'), $recargo1 );
}
}
// Cargar script de JavaScript para actualizar el resumen del pedido en finalizar compra
add_action( 'wp_footer', 'actualizar_resumen_pedido_js' );
function actualizar_resumen_pedido_js() {
// Solo cargamos el script en la página de finalizar compra
if ( is_checkout() && ! is_wc_endpoint_url() ) {
?>
<script type="text/javascript">
jQuery(document).ready(function($){
// Detectar cambios en el checkbox de recargo de equivalencia
$('input[name="recargo_equivalencia1"]').on('click', function(){
// Forzar la actualización del resumen del pedido
setTimeout(function(){
$(document.body).trigger('update_checkout');
}, 100);
});
});
</script>
<?php
}
}
</code>
<code> // Agregar un nuevo campo al formulario de pago add_action( 'woocommerce_review_order_before_payment', 'agregar_checkbox_recargo_equivalencia' ); function agregar_checkbox_recargo_equivalencia() { echo '<div id="recargo-equivalencia-checkbox"><h3>' . __('Recargo de Equivalencia', 'woocommerce') . '</h3>'; woocommerce_form_field( 'recargo_equivalencia1', array( 'type' => 'checkbox', 'class' => array('input-checkbox'), 'label' => __('Estoy acogido al recargo de equivalencia (+5,2%)', 'woocommerce'), ), WC()->session->get( 'recargo_equivalencia1' )); echo '</div>'; } // Guardar el estado del checkbox en la sesión add_action( 'woocommerce_checkout_update_order_meta', 'guardar_checkbox_recargo_equivalencia' ); function guardar_checkbox_recargo_equivalencia( $order_id ) { if ( !empty( $_POST['recargo_equivalencia1'] ) ) { WC()->session->set( 'recargo_equivalencia1', '1' ); } else { WC()->session->__unset( 'recargo_equivalencia1', '0'); } } // Aplicar el recargo de equivalencia al total de los productos si se ha marcado el checkbox add_action( 'woocommerce_cart_calculate_fees', 'aplicar_recargo_equivalencia' ); function aplicar_recargo_equivalencia() { if ( WC()->session->get( 'recargo_equivalencia1' ) === '1' ) { $cart1 = WC()->cart->get_cart(); $total_productos1 = 0; // Sumar el total de los productos en el carrito foreach ( $cart1 as $cart_item1 ) { $total_productos1 += $cart_item1['line_total']; } $recargo1 = $total_productos1 * 0.052; // 5.2% del total de productos WC()->cart->add_fee( ___ ('RE', 'woocommerce'), $recargo1 ); } } // Cargar script de JavaScript para actualizar el resumen del pedido en finalizar compra add_action( 'wp_footer', 'actualizar_resumen_pedido_js' ); function actualizar_resumen_pedido_js() { // Solo cargamos el script en la página de finalizar compra if ( is_checkout() && ! is_wc_endpoint_url() ) { ?> <script type="text/javascript"> jQuery(document).ready(function($){ // Detectar cambios en el checkbox de recargo de equivalencia $('input[name="recargo_equivalencia1"]').on('click', function(){ // Forzar la actualización del resumen del pedido setTimeout(function(){ $(document.body).trigger('update_checkout'); }, 100); }); }); </script> <?php } } </code>




// Agregar un nuevo campo al formulario de pago
add_action( 'woocommerce_review_order_before_payment', 'agregar_checkbox_recargo_equivalencia' );
function agregar_checkbox_recargo_equivalencia() {
    echo '<div id="recargo-equivalencia-checkbox"><h3>' . __('Recargo de Equivalencia', 'woocommerce') . '</h3>';
    woocommerce_form_field( 'recargo_equivalencia1', array(
        'type'      => 'checkbox',
        'class'     => array('input-checkbox'),
        'label'     => __('Estoy acogido al recargo de equivalencia (+5,2%)', 'woocommerce'),
    ), WC()->session->get( 'recargo_equivalencia1' ));
    echo '</div>';
}

// Guardar el estado del checkbox en la sesión
add_action( 'woocommerce_checkout_update_order_meta', 'guardar_checkbox_recargo_equivalencia' );
function guardar_checkbox_recargo_equivalencia( $order_id ) {
    if ( !empty( $_POST['recargo_equivalencia1'] ) ) {
        WC()->session->set( 'recargo_equivalencia1', '1' );
    } else {
        WC()->session->__unset( 'recargo_equivalencia1', '0');
    }
}

// Aplicar el recargo de equivalencia al total de los productos si se ha marcado el checkbox
add_action( 'woocommerce_cart_calculate_fees', 'aplicar_recargo_equivalencia' );
function aplicar_recargo_equivalencia() {
    if ( WC()->session->get( 'recargo_equivalencia1' ) === '1' ) {
        $cart1 = WC()->cart->get_cart();
        $total_productos1 = 0;

        // Sumar el total de los productos en el carrito
        foreach ( $cart1 as $cart_item1 ) {
            $total_productos1 += $cart_item1['line_total'];
        }

        $recargo1 = $total_productos1 * 0.052; // 5.2% del total de productos
        WC()->cart->add_fee( ___ ('RE', 'woocommerce'), $recargo1 );

    }
}

// Cargar script de JavaScript para actualizar el resumen del pedido en finalizar compra
add_action( 'wp_footer', 'actualizar_resumen_pedido_js' );
function actualizar_resumen_pedido_js() {
    // Solo cargamos el script en la página de finalizar compra
    if ( is_checkout() && ! is_wc_endpoint_url() ) {
        ?>
        <script type="text/javascript">
            jQuery(document).ready(function($){
                // Detectar cambios en el checkbox de recargo de equivalencia
                $('input[name="recargo_equivalencia1"]').on('click', function(){
                    // Forzar la actualización del resumen del pedido
                    setTimeout(function(){
                        $(document.body).trigger('update_checkout');
                    }, 100);
                });
            });
        </script>
        <?php
    }
}





I did try change variables, unistall plugins who may interact, try to re-refresh codes, change to booleans, change the code.
It simply doesn’t work or always add the fee. Probably is something with the checkbox.

New contributor

Desguace Cuantico is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật