Link to home
Start Free TrialLog in
Avatar of sharingsunshine
sharingsunshineFlag for United States of America

asked on

JQuery Not Firing On Woocommerce Footer

I have this code that worked a few months back on a test site.  I am now bringing my live site up slowly and wanting to put this jquery in the woocommerce footer.php location it worked on before at the test site.  I can see the code in the source code but it doesn't seem to be firing.

The server isn't left on till the site goes live so if you need to see it in action pm me and I can turn it on for you to see.

Here is the entire code in footer.php which is in the child theme.

This code is supposed to produce a message if a PO Box is used and UPS is selected for shipping on the checkout page.

<?php
/**
 * The template for displaying the footer.
 *
 * @package flatsome
 */

global $flatsome_opt;
?>

</main><!-- #main -->

<footer id="footer" class="footer-wrapper">

        <?php do_action('flatsome_footer'); ?>

</footer><!-- .footer-wrapper -->

</div><!-- #wrapper -->

<?php wp_footer(); ?>

<script>
jQuery(document).ready(function(){
        jQuery(document).on('click','#shipping_method_0_132643',function(){
                if(hasPobox()){
                        showPoError();
                }
        });

        jQuery(document).on('blur','#shipping_address_1',function(){
                if(hasPobox() && jQuery('#shipping_method_0_132643').is(':checked')){
                        showPoError();
                }else{
                        hidePoError();
                }
        });
 jQuery(document).on('blur','#shipping_address_2',function(){
                if(hasPobox() && jQuery('#shipping_method_0_132643').is(':checked')){
                        showPoError();
                }else{
                        hidePoError();
                }
        });

        function showPoError(){
                var msg ='<div class="pobox-error woocommerce-NoticeGroup woocommerce-NoticeGroup-checkout"><ul class="woocommerce-error message-wrapper" role="alert"><li><div class="messa$
                if(!jQuery('.pobox-error').length){
                        jQuery('#place_order').after(msg);
                }
        }

        function hidePoError(){
                jQuery('.pobox-error').remove();
        }

        function hasPobox(){
                var res = false;
            var pat1 = /^(([pP]{1}(.*?)(\s+)?[oO]{1}(.*?))+?(\s+)?([0-9]+))+?/i;
            var pat2 = /^([bB][oO]?[xX]?)(\s+)?([0-9]+)+?/i;
            var ad1 = jQuery('#shipping_address_1').val();
            var ad2 = jQuery('#shipping_address_2').val();
                if(pat1.test(ad1) || pat2.test(ad1) || pat1.test(ad2) || pat2.test(ad2)){
                res = true;
            }
            return res;
 }
});
</script>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sharingsunshine

ASKER

I am pm'ing you the link.
by the way, there are no errors that I see in the console.  Jquery is new to me so it is quite probable I am overlooking something simple.
Thanks for pointing out the onclick isn't referencing the correct shipping method.