Home > Enterprise >  Woocommerce "update_checkout" not working
Woocommerce "update_checkout" not working

Time:09-13

My update_cart trigger is not working at all...when i add "blur" in function instead of "change" then it trigger okay in chrome,opera,ie..but dont trigger on firefox and safari at all...i tried almost everything...i have plugin that add discount 5% on bacs payment..so i had to calculate subtotal before shipping cost...for example...subtotal of products - discount = then if its under 1000 shipping is not free..but on cheque payment method it doesnt need to give discount and that works all okay..but when i want to update_checkout on change it wont work..

add_filter( 'woocommerce_shipping_packages', function( $packages ) {
    $session_data = WC()->session->get_session_data();
    //error_log(print_r($session_data,true));
    $cart_totals = maybe_unserialize( $session_data['cart_totals'] );
    $payment_method = 'bacs';
    $chosen_payment_method = WC()->session->get('chosen_payment_method');
    $sub_total_after_item_discount = $cart_totals['subtotal']   $cart_totals['fee_total'];
    
    if( $payment_method == $chosen_payment_method && $sub_total_after_item_discount < 1000){
        $free_shipping_id = 'free_shipping:7';
        unset($packages[0]['rates'][ $free_shipping_id ]);
    } elseif ($payment_method == $chosen_payment_method && $sub_total_after_item_discount > 1000) {
        $flat_rate = 'flat_rate:6';
        unset($packages[0]['rates'][ $flat_rate ]);
    }
    return $packages;
} );

add_filter( 'woocommerce_shipping_packages', function( $packages ) {
    $session_data = WC()->session->get_session_data();
    //error_log(print_r($session_data,true));
    $cart_totals = maybe_unserialize( $session_data['cart_totals'] );
    $payment_method = 'cheque';
    $chosen_payment_method = WC()->session->get('chosen_payment_method');
    $sub_total_after_item_discount = $cart_totals['total']   $cart_totals['fee_total'];
    
    if( $payment_method == $chosen_payment_method && $sub_total_after_item_discount < 1000){
        $free_shipping_id = 'free_shipping:7';
        unset($packages[0]['rates'][ $free_shipping_id ]);
    } elseif ($payment_method == $chosen_payment_method && $sub_total_after_item_discount > 1000) {
        $flat_rate = 'flat_rate:6';
        unset($packages[0]['rates'][ $flat_rate ]);
    }
    return $packages;
} );

And this is my update_trigger function

add_action( 'woocommerce_review_order_before_payment', 'osvjezi_metodu_placanja' );
function osvjezi_metodu_placanja(){
    // jQuery kod
   ?>
   <script type="text/javascript">
        (function($){
            $( 'form.checkout' ).on( 'change', 'input[name^="payment_method"]', function() {
                $('body').trigger('update_checkout');
                //console.log('blur', true);});
            });
        })(jQuery);
    </script>
   <?php
}

CodePudding user response:

If you click on your first payment method, the page will refresh, and then your data will be reset. You simply inspect the value of your first input field and set it to the IF condition. i have added the screenshot, you can follow it

add_action( 'woocommerce_review_order_before_payment', 'osvjezi_metodu_placanja' );
function osvjezi_metodu_placanja(){
    // jQuery kod
   ?>
   <script type="text/javascript">
        (function($){
            $( 'form.checkout' ).on( 'change', 'input[name^="payment_method"]', function() {
                $('body').trigger('update_checkout');
                var currentVal = $(this).val();
                // You first inspect the payment method and set the value in the if condition. 'bacs'
                // https://prnt.sc/FQ95JyoN8mXA
                if(currentVal == 'set value'){
                    location.reload(true);
                }
                console.log(currentVal);
            });
        })(jQuery);
    </script>
   <?php
}
  • Related