How to accept specific zip codes in shipping form in virtuemart 3 ( vm3 )

Started by Jesus Camarillo, May 12, 2015, 07:07:46 AM

Previous topic - Next topic

Jesus Camarillo

Hi! this is not a request for help, yet, I search a lot of posts during this day to be able to make possible a request from one client.

Her request: That only the shipping could be in El Paso Texas from certain zip codes (not all from El Paso).

I'm going to put my solution, if you can improve it (I am pretty sure of it) please add the code in a reply for VM community.

The file that is responsible of VALIDATING the form in the shipping form is located at:

sitedomain.tld/administrator/components/com_virtuemart/helpers/vmjsapi.php


Original fragment code was:

$jsRegfields = implode("','",$regfields);
      $js = "function myValidator(f, r) {

      var regfields = ['".$jsRegfields."'];
   
      var requ = '';
      if(r == true){
         requ = 'required';
      }

      for   (i = 0; i < regfields.length; i++) {
         var elem = jQuery('#'+regfields+'_field');         
         elem.attr('class', requ);
      }

and I change it to:

$jsRegfields = implode("','",$regfields);
      $js = "function myValidator(f, r) {

      var regfields = ['".$jsRegfields."'];
      

      var elemCountry = jQuery('#shipto_virtuemart_country_id');
      // 223 United States code
      //alert(elemCountry.val());      

      var elemCity = jQuery('#shipto_city_field');
      // El Paso returned value
      //alert(elemCity.val());   
      
      var elemZP = jQuery('#shipto_zip_field');

      var arr_zipcodes = [ '79901', '79902', '79903', '79904', 
               '79905', '79906', '79907', '79908',
               '79911', '79912', '79915', '79920',
               '79922', '79928', '79925',
               '79927', '79901', '79930', '79934',
               '79935', '79936', '79938' ];

      if (elemCountry.val()=='223') { // If is in United States
         if (jQuery.inArray( elemZP.val(), arr_zipcodes ) == -1 ) {
              alert('Wrong zip code! Please enter only for El Paso, Tx. in the valid Zip Codes');
              return false;
          }
      }
      
      var requ = '';
      if(r == true){
         requ = 'required';
      }

      for   (i = 0; i < regfields.length; i++) {
         var elem = jQuery('#'+regfields+'_field');         
         elem.attr('class', requ);
      }

Is very simple, you must select the fields via Jquery to see what value are we validating. In my case, country and zip codes (State doesn't matter since they were specific zip codes that are only from El Paso, Tx) Later, in an array, I put the zip codes and if the entry wasn't one of them (throws a -1 value) then it alerts the user, and returns false (which makes the user to remain in the form).

That is really all.

I want it to share as I bet the VM admins of this forum are having a lot of requests and I wanted to make my self useful.

:-)