Change clearing method based on user group - xmpie-users/uStore-js GitHub Wiki

By default, uStore allows you to set one or more clearing (payment) methods on a store. For example, Credit Card, Cost Center and Invoice.

When multiple clearing methods are set on a store, the customer can select which payment method they want to use for each order they place. In most cases, store administrators will set one clearing method on each store to force the customer to use their preferred payment method.

Of course, it is easy to duplicate a store, and set one to use Credit Card clearing (maybe for public customers) and the other to use Invoice clearing (maybe for internal customers).

However, in some cases, the store administrator may want to manage only one store, but control which clearing method is available to the user based on their user group.

  1. You can then add the following JavaScript either to the same master page, or in the Add JavaScript to storefront dialog in uStore Admin:
<script type="text/javascript">
  var UserGroupToClearingMapping = {}; 
  //mapping[group user id] = clearing ids
  //in this example User Group with ID 1 can see clearings with ID 1 , 2 and 10000
  //while User Group with ID 2 can only see clearing with ID 1 and 2
  //User group ID's and Clearing ID's can be found in the uStore Admin 
  UserGroupToClearingMapping[1] = [1,2,10000];
  UserGroupToClearingMapping[4]  = [1,2];


  var UserGroupsByUserArray = xmp.sdk.storeFrontParams.currentUser.userGroups;

  if(typeof UserGroupsByUserArray !== 'undefined'){
    $(document).ready(function(){  
      var cls = $("input[id*='rdlClearingConfig']");
      if(cls.length > 0){
        $(cls).parent().hide();
        $.each(UserGroupToClearingMapping, function(groupId, clearings) {
          if($.inArray(parseInt(groupId), UserGroupsByUserArray.map(u => u.ID)) >= 0){
            $.each(clearings, function(index, value) {
              var elem = $(cls).filter("[value="+ value +"]");
              if(elem[0] != null) {
                $(elem).parent().show();
              }
            });
          }  
        });
        var checked = $(cls).filter(":checked");
        if(checked[0] != null){
          if(checked.parent().is(':visible') == false){
            var parent = $(cls).parent().filter(':visible')[0];
            if(parent != null){
              var input = $(parent).children("input[id*='rdlClearingConfig']");
              $(input).click();
            } else {
              $('td[class=ClearingConfigCell]').hide();
            }
          }
        }
        $("[id*=btnCheckout]").click(function(){
          var checked = $(cls).filter(":checked");
          if(checked[0] != null){
            if(checked.parent().is(':visible') == false){
              return false;
            }
          }
        });
      }
    });
  }
</script>
  1. Edit the UserGroupToClearingMapping object to set your user group ids and clearing group ids as mentioned in the comments section of the script.
⚠️ **GitHub.com Fallback** ⚠️