Cost Center Clearing with Dropdown List - xmpie-users/uStore-js GitHub Wiki

uStore provides a cost center clearing method for customers to use when ordering. This allows the customer to enter a cost center number at checkout and you can use this information in uStore reports when billing the customer later.

By default there is only one input box, when you may simple require a dropdown or select list of predefined codes

This JavaScript works by hiding the default cost center text box and inserting a select list that is populated by an Array of CostCentre Codes. Additional this examples shows how to append a Custom User Field (Custom5) to the CostCentre Clearing using the xmp JavaScript SDK.

<script type="text/javascript">
if(window.location.pathname.indexOf("CheckoutPaymentSubmission.aspx") > 0){
   $( document ).ready(function() {
      var codes = [ "Please Select Code", "one", "two", "three", "four", "five" ];  

      var cc = $("[name*='txtCostCenter']");
      if(cc.length > 0){
      cc.hide();
      cc.parent().append("<select id='myCCselect'/>");
      $.each( codes, function( i, val ) {
         $('#myCCselect').append($('<option>', {value: val,text : val }));
      });

      $( "#myCCselect" ).change(function() {
         if($(this).val() ==="Please Select Code"){ 
            cc.val("");
         } else {
            if(xmp.sdk.storeFrontParams.currentUser.custom5 !=="****"){
               cc.val(xmp.sdk.storeFrontParams.currentUser.custom5 +"-"+$(this).val());
            }
         }
         //console.log(cc.val());
      });
      }
   });
}
</script>

For information on how to add JavaScript to your store: How to add JavaScript to a storefront.

⚠️ **GitHub.com Fallback** ⚠️