Multiple cost center fields and cost center validation - 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 no validation of the cost center number, and there is only one input box, when you may require two or more cost centers or parts (eg division and department) to be collected.

This JavaScript works by hiding the default cost center text box and inserting custom text boxes, and then combining the custom values into the default uStore cost center text box. For example, if the customer enters CC2, 34223 and DD34 into the three custom text boxes, the resulting cost center number on the order will be CC2||34223||DD34.

In addition it provides a method for you to add validation of the cost center value entered by the customer.

<script type="text/javascript">
$(document).ready(function() {
  if ($(".ClearingConfigCell")) {
    //hide the default costcentre field and label
    $("#ctl00_cphMainContent_ctlClearingUserData10000_lblCostCenter").hide();
    $("#ctl00_cphMainContent_ctlClearingUserData10000_txtCostCenter").hide();

    //append your custom form fields
    $(".ClearingConfigCell").append("Caption 1: <input type='text' id='customField1' maxlength='5' value='' class='customCostCenterField'><br>");
    $(".ClearingConfigCell").append("Caption 2: <input type='text' id='customField2' value='' class='customCostCenterField'><br>");
    $(".ClearingConfigCell").append("Caption 3: <input type='text' id='customField3' value='' class='customCostCenterField'>"); 

    //add some validation for your custom fields if needed
    validateClearingCustomFields()

    //now when your custom fields are changed, set the default costcenter form field to be custom field values separated by pipes
    $(".customCostCenterField").keyup(function () {
      $("#ctl00_cphMainContent_ctlClearingUserData10000_txtCostCenter").val ( $("#customField1").val() + "||" + $("#customField2").val() + "||" + $("#customField3").val() );
      validateClearingCustomFields()
    });
  }
});

function validateClearingCustomFields() {
{
 if ($("#customField1").val() != "")
    $("#ctl00_cphMainContentFooter_btnCheckout").show();
  else
    $("#ctl00_cphMainContentFooter_btnCheckout").hide();
  }
}
</script>

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

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