Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Method Signature

CityStateZip [] getCityState(String zip);

Description

Adds the specified item(s) to the cart. The minimum amount of information that must be specified on the CartItem object is itemId and quantity. If you want to pass in options then you'll also need to populate those fields. The global cart variable is automatically included in the call to the server and updated after the call is complete.

Parameters

CartItem[] items - The array of items to add to the cart.

Result

String[] -All the errors that occurred while trying to add the items to the cart. If this array is empty then the addition was successful. If this array contains any values then you'll want to display the errors to the customer. This could include things like out of stock conditions, invalid item ids, etc. Even if the errors array contained something the global cart variable is updated.Takes a zip and does a city and state lookup with the United States Postal Service.

Result

A CityStateZip object.

Discussion

  1. Make the remote call
  2. Check to make sure the result is not null. If it is, a system error occurred.
  3. Check CityStateZip.validZip flag. If false, the zip was invalid.
  4. If zip was valid, city and state will be populated and may be used.

Examples

Code Block
languagexml
themeDJango
titleCheckout API v1.1
linenumberstrue
collapsetrue
<html>
<head>
  <script type='text/javascript' src='js/jquery-1.4.2.min.js'></script>
  <script type='text/javascript' src='js/jquery.json-2.2.min.js'></script>
  <script type='text/javascript' src='js/checkoutapi-1.1.js'></script>

  <script type='text/javascript'>

    window.onload = function(){
      log('initializing checkout');
      initializeCheckoutAPI("DEMO", 'secure.ultracart.com', 'http://www.lipovox.com:3000/proxy.php');
      log('getting cart instance');
      getCartInstance();
      log('attaching lookup function to zip element blur event');
      document.getElementById('zip').addEventListener('blur', lookup);

    };

    function lookup(){
      var zip = document.getElementById('zip');
      var city = document.getElementById('city');
      var state = document.getElementById('state');

      log('lookup(' + zip.value + '):');

      //Method 1: Asynchronously
      getCityState(zip.value, {async:true, onComplete:function(result){
        if(result == null){
          log('result of remote call was null. something went wrong on the server.');
        } else if(result.validZip){
          log('    SUCCESS! ' + result.city + ' ' + result.state);
          city.value = result.city;
          state.value = result.state;
        } else {
          log('    Invalid Zip Code');
          if(result.error){
            log('    System Error: ' + result.error); // handle this gracefully or just ignore...
          }
        }
      }});


      //Method 2: Synchronous
      /*
      var result = getCityState(zip.value);
      if(result == null){
        log('result of remote call was null. something went wrong on the server.');
      } else if(result.validZip){
        log('    SUCCESS! ' + result.city + ' ' + result.state);
        city.value = result.city;
        state.value = result.state;
      } else {
        log('    Invalid Zip Code');
        if(result.error){
          log('    System Error: ' + result.error); // handle this gracefully or just ignore...
        }
      }
      */

    }

    function log(msg){
      var div = document.getElementById('log');
      var txtNode = document.createTextNode(msg);
      var br = document.createElement('br');
      div.appendChild(txtNode);
      div.appendChild(br);
    }

  </script>
</head>
<body>

<table>
<tr><td>Zip Code:</td><td><input type='text' id='zip'/></td></tr>
<tr><td>City:</td><td><input type='text' id='city'/></td></tr>
<tr><td>State:</td><td><input type='text' id='state' /></td></tr>
</table>
<div id='log' style='margin-top:20px;'>
<div style='font-weight:bold'>Log Events Will Appear Below:</div>
</div>




</body>
</html>

JS API v1.2

JS API v2.0