Skip to content

Instantly share code, notes, and snippets.

@becomevocal
Created February 6, 2019 15:36
Show Gist options
  • Save becomevocal/8ece42f71a1291b6b331a387b15906ef to your computer and use it in GitHub Desktop.
Save becomevocal/8ece42f71a1291b6b331a387b15906ef to your computer and use it in GitHub Desktop.
BC4WP: Support for Customer Group Category Visibility (add to bottom of theme functions.php)
use BigCommerce\Accounts\Customer;
const GUEST_CUSTOMER_GROUP_ID = 2;
// Check if transient is set for the guest customer group details. If not, fetch them and store as a transient for 24 hours.
// https://codex.wordpress.org/Transients_API
if ( !get_transient( 'guest_customer_group_category_access' ) && GUEST_CUSTOMER_GROUP_ID !== 0 ) {
$customer = new Customer();
$customer_group_category_access = $customer->get_customer_group_category_access(GUEST_CUSTOMER_GROUP_ID);
set_transient( 'guest_customer_group_category_access', $customer_group_category_access, DAY_IN_SECONDS );
}
function customer_group_login_function( $user_login, $user ) {
$customer = new Customer( $user->ID );
$profile = $customer->get_profile();
$customer_group_category_access = $customer->get_customer_group_category_access($profile['customer_group_id']);
// https://developer.wordpress.org/reference/functions/update_user_meta/
update_user_meta( $user->ID, 'customer_group_id', $profile['customer_group_id'] );
update_user_meta( $user->ID, 'customer_group_category_access', $customer->get_customer_group_category_access($profile['customer_group_id']) );
}
add_action('wp_login', 'customer_group_login_function', 10, 2);
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();
// https://developer.wordpress.org/reference/functions/get_user_meta/
$customer_group_category_access = get_user_meta( $current_user_id, 'customer_group_category_access', true );
return array_filter($sorted_menu_items, function($item) use ($current_user_id, $customer_group_category_access) {
if (!$current_user_id) {
// Visitor is a guest.
if (GUEST_CUSTOMER_GROUP_ID !== 0) {
// There is a guest customer group set, so use the details tha we stored in the transient.
$customer_group_category_access = get_transient( 'guest_customer_group_category_access' );
} else {
// There is no guest customer group set, so show this category. By default all categories are shown.
return true;
}
}
// The visitor is logged in. Now if it's a BigCommerce category we are showing in the menu, get the BC ID that is stored
// within the taxonomy meta data and determine visibility.
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 access type is set to 'all'
// 2) The customer group category access type is set to 'specific' and the categories are
return $customer_group_category_access->type && ($customer_group_category_access->type === 'all' || ($customer_group_category_access->type === 'specific' && in_array($bigcommerce_category_id, $customer_group_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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment