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

asked on

Need Wordpress jQuery To Load After Ajax

I have a jquery that needs to be setup as a delegated event.  Or, in other words to load after ajax is finished.  This is beyond my abilities.  I did find this article but i don't know how to implement what they are suggesting since I am very new to jquery.  Here is the article - https://learn.jquery.com/events/event-delegation/

My site isn't live yet so I have restricted access to it.  If you need to see the site I will need your IP to whitelist you.  The more particulars and a screenshot of the site is on this question - https://www.experts-exchange.com/questions/29106720/Need-Jquery-Fixed-To-Post-Shipping-Message.html?anchorAnswerId=42609003#a42609003

I understand if you don't want to give your IP just give me the code to test and I will insert it.  Because this is wordpress I don't; want to hack the wordpress core files.

<script>
jQuery(function($) {
  // CHECK FOR UPS OPTION CHANGE
  $('body').on('change', '[name="shipping_method[0]"]', doPOCheck);

  // CHECK FOR ANY CHANGES TO ADDRESS OR SHIPPING CHECKBOX
  $('#billing_address_1,#billing_address_2,#shipping_address_1,#shipping_address_2,#ship-to-different-address-checkbox').change(doPOCheck);

  // THIS IS THE WORKHORSE  
  function doPOCheck ()
  {
    // Only proceed if we UPS is checked
    if ($('#shipping_method_0_132643').is(':checked') ) {

      // IF THE SHIPPING CHECKBOX IS CHECKED WE WANT THE SHIPPING
      // ADDRESSES OTHERWISE THE BILLING ADDRESSES
      if ($('#ship-to-different-address-checkbox').is(':checked')) {
        ad1 = $('#shipping_address_1').val();
        ad2 = $('#shipping_address_2').val();
      }
      else {
        ad1 = $('#billing_address_1').val();
        ad2 = $('#billing_address_2').val();
      }

      // DO THE PO CHECK
      if (hasPOBox(ad1, ad2)) {
        showPoError();
      }
      else {
        hidePoError();
      }
    }
	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="message-container container alert-color medium-text-center"><span class="message-icon icon-close"></span> <strong>UPS can not send to PO Box.</strong></div></li></ul></div>';
    if(!jQuery('.pobox-error').length) {
      $('#place_order').after(msg);
    }
  }

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

  // This now becomes generic for billing and shipping
  function hasPOBox(ad1, ad2)
  {
    ad1 = ad1.replace(/[^A-Z]/gi,'').substr(0,5).toLowerCase();
    ad2 = ad2.replace(/[^A-Z]/gi,'').substr(0,5).toLowerCase();

    return (ad1=='pobox' || ad2=='pobox');
 }
});
</script>

Open in new window


Essentially, this code looks at the shipping addresses and if someone has PO Box in their address and they select UPS as a shipping method it produces a message.  This is because UPS won't ship to a PO Box.
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

that did it, thanks so much for your help and sticking with the project.
You are welcome - glad it is finally sorted.