How to Control Category Visibility by Customer Group Using BigCommerce for WordPress

Karen White
BigCommerce Developer Blog
5 min readJul 31, 2019

--

Written by Karen White and Nate Stewart

BigCommerce customer groups allow you to segment your shoppers and apply rules to control which products customers can view and which prices and discounts are applied. It’s a feature that adds a lot of flexibility to a storefront, with applications across B2B, customer loyalty programs, and “members only” retailers.

If you’re using BigCommerce for WordPress to power your storefront, a customer’s group ID and pricing are surfaced on WordPress automatically, but controlling which categories are visible to specific groups requires a little bit of customization. In this post, we’ll show you how you can create a simple child plugin for BigCommerce for WordPress that filters category visibility on the WordPress storefront according to the customer group category visibility rules that you set up in BigCommerce.

We’ll unpack the code section by section to explain how everything works, but if you want to go straight to the code, you can download the child plugin at the end of this post.

While you could take this tutorial at face value, we encourage you to get creative! Once you have access to a customer’s group ID in WordPress, you have the potential to create other kinds of application logic around customer segmentation. You might tailor banner ads to a certain group or enque a different stylesheet to create unique storefront experiences for different groups of shoppers. Consider this tutorial a starting point to help you create your own personalized shopping experiences on WordPress.

Creating a Child Plugin

Before we begin, a quick note on plugin customization best practices. To make sure that customizations play nicely with updates, it’s important to package your customizations as a child plugin that runs alongside the parent plugin instead of modifying the parent plugin files directly. That way, you can be sure that you won’t lose your work when you upgrade to a new plugin version.

In this example, we’ll be using filters built into the plugin and WordPress core to modify plugin functionality without directly editing the template files. You can run the example child plugin by uploading the zip file (linked at the end of this post) to the plugin area in your WordPress admin.

Setting a Default Customer Group

The default customer group is the group that’s assigned to guest shoppers. The ability to set a customer group for non-logged in users is useful, because you can create different experiences for guest vs registered users, like making your site content “members only.” We can tell that a customer is a guest if their customer group ID is 0.

The first thing we want to do is create a function to assign a customer to the default group, if we detect that the shopper is a guest. For the value of the constant, we can either supply -1 to not set a default group for guest shoppers, 0 to make the default group the first customer group in the list pulled from BigCommerce, or we can plug in the ID of a specific group that should be assigned to guests.

Here, we’re saying that the customer group ID for the default group is 6; if we detect that the customer group ID of the current user is 0, meaning they are not logged in, we return the value of the default customer group ID.

// Set to -1 to not set a default customer group for visibility
// (using 0 will pull in the first customer group from BC)
const DEFAULT_CUSTOMER_GROUP_ID = 6;
function set_default_customer_group($group, $customer) {
if ($customer->get_customer_id() === 0) {
return DEFAULT_CUSTOMER_GROUP_ID;
}
}
add_filter('bigcommerce/customer/group_id', 'set_default_customer_group', 10, 2);

Filter Categories Based on Group Permissions

Next, we want to filter the categories listed in the main navigation menu, so that we show only the categories that the customer’s group has permission to see.

Let’s step through what’s happening in the code below.

We’re defining a function that gets the ID of the current customer and fetches the customer group information associated with that customer, including which categories should be visible. Then, we’re filtering the array of BigCommerce categories to return only the categories that match the customer’s group visibility settings. So, we’re returning a category only if the customer group category access is empty (indicating it’s the default fallback), or the customer’s category access is set to ‘all’, or if the category matches one of the categories that the customer has permission to see. Lastly, we provide that function as a callback to the ‘wp_nav_menu_objects’ hook. This hook allows you run a task that modifies the available menu items before the HTML for the menu is generated. In this case, it allows us to trim the available menu items according to what should be visible to the current customer.

function filter_categories_based_on_customer_group($sorted_menu_items, $args) {
// https://developer.wordpress.org/reference/functions/get_current_user_id/
$current_user_id = get_current_user_id();

$customer = new \BigCommerce\Accounts\Customer($current_user_id);
try {
$customer_group_info = $customer->get_group()->get_info();
} catch ( \Exception $e ) {
wp_die( $e->getMessage() );
}

return array_filter($sorted_menu_items, function($item) use ($current_user_id, $customer_group_info) {
if ($item->object === 'bigcommerce_category') {
// https://developer.wordpress.org/reference/functions/get_term_meta
$category_meta = get_term_meta($item->object_id);
$bigcommerce_category_id = $category_meta['bigcommerce_id'][0];
// Only show this category if one of the two conditions apply:
// 1) The customer group category is blank (is the default fallback)
// or
// 2a) The customer group category access type is set to 'all'
// 2b) The customer group category access type is set to 'specific' and the category is within the categories array
return empty($customer_group_info['category_access']) || ($customer_group_info['category_access']['type'] && ($customer_group_info['category_access']['type'] === 'all' || ($customer_group_info['category_access']['type'] === 'specific' && in_array($bigcommerce_category_id, $customer_group_info['category_access']['categories']))));
}
// For all other types of menu items, pass them through so they display normally.
return true;
});
}
add_filter('wp_nav_menu_objects', 'filter_categories_based_on_customer_group', 10, 2);

Putting It All Together

With just two functions, we’ve built a child plugin that controls the categories that are visible to a customer based on the group permissions defined in BigCommerce. Here’s a look at the finished code:

You can download the zip file here:

https://bit.ly/2MtUsOk

Conclusion

We hope this tutorial has inspired you to explore new functionality you can build into BigCommerce for WordPress, and even to think beyond the features that are natively available on BigCommerce. While we’re busy building new features with each release cycle, it’s also our goal to empower the developer community to unlock new features as they need them, without having to wait for functionality to officially be brought into the core plugin.

And if you are a developer modifying BigCommerce for WordPress and you’ve identified a missing hook that would help you extend the plugin — we want to know about it. Open an issue on our GitHub repo and tell us what you need and why. We try to accommodate as many new hook requests as we can!

You can see the hooks that are currently available in the BigCommerce for WordPress Code Reference.

Lastly, tell us how what you’re building, or how we can make the BigCommerce for WordPress developer experience better! Leave us a comment below or tweet us @BigCommerceDevs.

--

--

Karen White
BigCommerce Developer Blog

Developer Marketing at Atlassian. Formerly at BigCommerce, Rasa. I think Voyager was the best Star Trek.