---
title: Customer Groups
slug: template-js/customer-groups
docTags: 
createdAt: 2022-07-12T01:29:55.000Z
---



To accommodate variable ***pricing and/or visibility for specific customer group***, we will introduce two additional parameters to every API request.

- priceFieldSuffix
- visibilityGroupID

See: Modify Request [Overview](docId:_ndrEPmgAUhYltIlXkG-o) for details on preparing for Klevu JS overriding

See :  Klevu API Documentation for [Customer Groups](https://docs.klevu.com/apis/smart-search-examples#GI08Z)

# Indexing

:::hint{type="success"}
The additional currency pricing data must be supplied at the product level during catalog indexing.
:::



### XML FEED example

Below is a [FEED XML](https://github.com/klevu/feed/blob/8030b070f2b7d707791e65a2b22b2223d2c421cd/sample.xml#L518) example for specific group **visibility**

```xml
 <item>
 . . .
 
      <price>123.45 GBP</price>
      <sale_price>100 GBP</sale_price>
            
      <groups>
            <group>
                <id>vip_customer_group</id>
            </group>
      </groups>
. . .
```

Below is a [FEED XML](https://github.com/klevu/feed/blob/8030b070f2b7d707791e65a2b22b2223d2c421cd/sample.xml#L546) example for specific group visibility and/or **variable pricing**

```xml
 <item>
 . . .
 
      <price>123.45 GBP</price>
      <sale_price>100 GBP</sale_price>
            
      <groups>
            <group>
                <id>vip_customer_group</id>
                <price>49.99 GBP</price>
                <sale_price>49.99 GBP</sale_price>
            </group>
      </groups>
. . .
```

:::hint{type="info"}
If you intend to use multiple currencies in addition to group variable pricing and/or visibility, please review the Template JS [Multi-Currency](docId\:oliq0Hmh1p_A9KPKSZSqv) guide for more details
:::



# Template JS Override

The desired group visibility and/or prices are applied to all queries via overrides using the "all" `scope` value.

See: Modify Request [Overview](docId:_ndrEPmgAUhYltIlXkG-o) for details on preparing for Klevu JS overriding

### Example 1&#x20;

Exclude all records which do not belong to the group

:::CodeblockTabs
JavaScript

```typescript
<script type="text/javascript">

    // The selected customer group <id> 
    var klevu_loginCustomerGroup = 'vip_customer_group';


    klevu.modifyRequest("all", function(data, scope){
      klevu.each(data.request.current.recordQueries, function(key, query){
        klevu.setObjectPath(query, "settings.visibilityGroupID", klevu_loginCustomerGroup);
      });
    });
    
 
</script>
```

BigCommerce

```html
<script type=“text/javascript”>

  // the group id of the currently logged in customer
  var klevu_loginCustomerGroupId = '{{customer.customer_group_id}}' ? '{{customer.customer_group_id}}' : '';
  var klevu_loginCustomerGroup = klevu_loginCustomerGroupId ? 'groupid_' + klevu_loginCustomerGroupId : '';
  
  // only modify the requests when there is a valid customer group id
  if (klevu_loginCustomerGroupId) {
     klevu.modifyRequest('all', function(data, scope){
      klevu.each(data.request.current.recordQueries, function(key, query){
        klevu.setObjectPath(query, "settings.visibilityGroupID", klevu_loginCustomerGroup);
      });
     });
   }
   
</script>
```
:::

| **API Parameter** | **Description**                                      | **Format** |
| ----------------- | ---------------------------------------------------- | ---------- |
| visibilityGroupID | Exclude all records which do not belong to the group | \{GROUPID} |

See :  Klevu API Documentation for [Customer Groups](https://docs.klevu.com/apis/smart-search-examples#GI08Z)



### Example 2

Apply the specific customer group pricing for the appropriate currency

:::CodeblockTabs
JavaScript

```typescript
<script type="text/javascript">

/* IMPORTANT FOR BIGCOMMERCE APP USERS, INDEXING THROUGH KLEVU PLUGIN ONLY,
REFER TO BIGCOMMERCE TAB */
  
// the currency and group of the currently logged in customer
  var klevu_currency = 'GBP';
  var klevu_loginCustomerGroup = 'vip_customer_group';
 
   klevu.modifyRequest("all", function(data, scope){
      klevu.each(data.request.current.recordQueries, function(key, query){
        klevu.setObjectPath(query, "settings.priceFieldSuffix", klevu_currency + '-' + klevu_loginCustomerGroup);
      });
   });
 
</script>
```

BigCommerce

```javascript
<script type="text/javascript">

  // the currency and group of the currently logged in customer
  var klevu_currency = '{{currency_selector.active_currency_code}}';
  var klevu_loginCustomerGroupId = '{{customer.customer_group_id}}' ? '{{customer.customer_group_id}}' : '';
   
  // only modify the requests when there is a valid customer group id 
  if (klevu_loginCustomerGroupId) {
     klevu.modifyRequest('all', function(data, scope){
      klevu.each(data.request.current.recordQueries, function(key, query){
        klevu.setObjectPath(query, 'settings.priceFieldSuffix', klevu_currency + '-groupid--' + klevu_loginCustomerGroupId);
      });
     });
   }
 
</script>
```
:::

| **API Parameter** | **Description**                                    | **Format**             |
| ----------------- | -------------------------------------------------- | ---------------------- |
| priceFieldSuffix  | Fetch prices/currency specific to a customer group | \{CURRENCY}-\{GROUPID} |

See :  Klevu API Documentation for [Customer Groups](https://docs.klevu.com/apis/smart-search-examples#GI08Z)
