I have an error when adding extra fees (add_fee) in the cart of Wordpress with Woocommerce?

61 Views Asked by At

What I'm wanting to do is the following: I have two types of subscriptions on the site for customers, that if they buy them they can have discounts on products. These subscriptions cost ARS$5,000 and ARS$10,000 respectively. What I want to do is that if a customer adds a product of, for example, $1500 to the cart, they are given the option through a modal window to be able to buy one of the two subscriptions by subtracting the amount of the product. In other words, they could pay an extra $3,500 for the $5,000 or $8,500 for the $10,000.

I managed to make the code in the cart page so that this happens. The extra fee is added if one of the two options is chosen, but when, for example, they go to checkout, the fee disappears, or when they change the shipping or payment method.

I am looking for a way for this extra fee to remain fixed whether the shipping or payment method is changed, or if it is passed from the cart to the checkout.

I show you the code I made:

add_action( "woocommerce_cart_calculate_fees", "add_fee_extra_para_completar_suscripcion_en_carrito", 10, 1 );

function add_fee_extra_para_completar_suscripcion_en_carrito( $cart ) {        

        global $woocommerce;        

        $TOTAL_COMPRA = intval(WC()->cart->get_subtotal());  
    
        $SUMA_CLASICA = 5000 - $TOTAL_COMPRA;   
        $SUMA_PREMIUM = 10000 - $TOTAL_COMPRA;  

        $URL_COMPRA_SUSC_CANCEL = 'https://cherimoya.com.ar/cart/?compra-susc=cancel';  
        $URL_COMPRA_SUSC_CLASICA_OK = 'https://cherimoya.com.ar/cart/?compra-susc=clasica-ok';  
        $URL_COMPRA_SUSC_PREMIUM_OK = 'https://cherimoya.com.ar/cart/?compra-susc=premium-ok';  
        $URL_CARRITO = 'https://cherimoya.com.ar/cart/';
        $URL_ACTUAL = get_permalink();

        $COMPRA_SUSC = $_REQUEST['compra-susc'] ?? '';  


        if (is_page('cart') || is_page('checkout')) {

            if ($TOTAL_COMPRA < 5000) {  

                if ( !usuario_rol( 'Premium' ) && !usuario_rol( 'Clasico' ) ) {     
    
                    if ($COMPRA_SUSC == '') {
                    ?>
                    <div id="modalExtraParaSusc" class="modal fade show">                                               <!--background-color: #202020;-->
                        <div class="modal-dialog">
                            <div class="modal-content" style="background-color: #202020; border-radius: 8px; color: white; box-shadow: 0px 0px 10px;">
                                <div class="modal-header" style="display: flex; flex-direction: column; padding: 28px 36px 0px 36px; border: 0;">                           
                                    <p style="flex-direction: row; padding-bottom: 20px; display: inline-block; text-align: center;">Puede completar su compra para la obtención de una de nuestras dos suscripciones:</p>                                                                
                                </div>
                                <div class="modal-body" style="padding:0;">
                                    <div style="padding: 0px 50px;">
                                        <table cellspacing="0">
                                            <thead>
                                                <tr>
                                                    <th class="product-name" colspan="2" style="border-width:3px;">Total del carrito</th>
                                                </tr>
                                            </thead>
                                        </table>
                                        <table cellspacing="0" class="shop_table shop_table_responsive">
                                            <tbody>
                                                <tr class="cart-subtotal">
                                                    <th>Subtotal</th>
                                                    <td data-title="Subtotal"><span class="woocommerce-Price-amount amount"><bdi style="color: white;"><span class="woocommerce-Price-currencySymbol" style="color: white;">$</span> <?php echo $TOTAL_COMPRA; ?> </bdi></span></td>
                                                </tr>                                               
                                            </tbody>
                                        </table>
                                    </div>                                
                                </div>
                                <div class="modal-footer" style="display: flex; flex-direction: column; border: 0;">
                                    <form action="https://cherimoya.com.ar/cart/" style="padding: 0px 50px;">                                
                                        <label style="color: white;">
                                            <input type="radio" name="compra-susc" value="clasica-ok"/>
                                            Resto para suscripción Clásica de $5000: $ <?php echo $SUMA_CLASICA; ?> 
                                        </label>
                                        <label style="color: white;">
                                            <input type="radio" name="compra-susc" value="premium-ok"/>
                                            Resto para suscripción Premium de $10000: $ <?php echo $SUMA_PREMIUM; ?> 
                                        </label>  
                                        <!--    
                                        <table cellspacing="0" class="shop_table shop_table_responsive">
                                            <tbody>                                             
                                                <tr class="order-total">
                                                    <th>Total</th>
                                                    <td data-title="Total"><strong><span class="woocommerce-Price-amount amount"><bdi style="color: white;"><span class="woocommerce-Price-currencySymbol" style="color: white;">$</span>375,00</bdi></span></strong> </td>
                                                </tr>   
                                            </tbody>
                                        </table>   
                                        -->
                                        <p class="enlace-ver-susc" style="font-size: 1.2em; margin-top: 50px;">Ver nuestras <a href="https://cherimoya.com.ar/suscripciones" style="color: #c207b2;">Suscripciones</a> </p>  
                                        <div style="display: flex; justify-content: space-between; margin-top: 30px;">  
                                            <button type="submit" class="btn" style="width: 200px; height: 40px; padding: 0px 12px 4px 12px; background-color: #9ce52e !important;    color: #1f2614;">
                                                Aceptar                                               
                                            </button>
                                            <button type="button" class="btn btn-default" data-dismiss="modal" style="width: 200px; padding: 0; margin-right: 0;">
                                                <a href="https://cherimoya.com.ar/cart/?compra-susc=cancel" style="display: block; height: 38px; border: 2px solid #9ce52e !important; background-color: #202020; color: #9ce52e !important;">Cancelar</a>                                         
                                            </button>                                                                            
                                        </div>                                   
                                    </form>   
                                </div>
                            </div>
                        </div>
                    </div>
    
                    <style>
                        #modalExtraParaSusc {
                            display: block;
                            position: fixed;
                            top: 0;
                            right: 0;
                            bottom: 0;
                            left: 0;
                            z-index: 1050;                 
                            outline: 0;
                            transition: opacity .15s linear;
                            background-color: rgb(0 0 0 / 60%);
                            overflow-x: hidden;
                            overflow-y: auto;
                        }
    
                        #modalExtraParaSusc > div {
                            margin-top: calc((100vh - 536px) /2) !important;
                            max-width: 500px;
                            margin: 1.75rem auto;
                            position: relative;
                            width: auto;
                            /*pointer-events: none;*/
                        }
                    </style>
                    <?php
                } 

                
                
                if ($COMPRA_SUSC == 'clasica-ok') {  
                    $additional_fee_name = "Resto para suscripción Clásica";
                    $extra_fee = $SUMA_CLASICA;
                    $additional_fee_taxable = true;

                    $addedFee = false;

                    // first check to make sure it isn’t already there
                    foreach ( $cart->get_fees() as $_fee ) {                            
                        if ($_fee->id == sanitize_title($additional_fee_name) ) {
                            $_fee->amount = (float) esc_attr( $extra_fee );
                            $_fee->taxable = $additional_fee_taxable;
                            $addedFee = true;
                        }                                            
                    }
                    if (!$addedFee) {                       
                        $cart->add_fee( __($additional_fee_name, "woocommerce"),
                        $extra_fee, $additional_fee_taxable );                               
                    }
                }

                if ($COMPRA_SUSC == 'premium-ok') {                       
                    $additional_fee_name = "Resto para suscripción Premium";
                    $extra_fee = $SUMA_PREMIUM;
                    $additional_fee_taxable = true;

                    $addedFee = false;

                    // first check to make sure it isn’t already there
                    foreach ( $cart->get_fees() as $_fee ) {                                                
                        if ($_fee->id == sanitize_title($additional_fee_name) ) {
                            $_fee->amount = (float) esc_attr( $extra_fee );
                            $_fee->taxable = $additional_fee_taxable;
                            $addedFee = true;
                        }                                             
                    }
                    if (!$addedFee) {                     
                        $cart->add_fee( __($additional_fee_name, "woocommerce"), $extra_fee, $additional_fee_taxable );                               
                    }
                }    
            }  
        } 
           
}

I await answers. Thank you

0

There are 0 best solutions below