I'm setting up a membership site using WooCommerce, WooCommerce Subscriptions, and WooCommerce Memberships. Ideally, I would first like to check if the user is logged-in and if they have an active membership. Then, I want to add the active membership plan's slug to the page's body class.
I have code below which adds several classes to the page's body tag, including checking for a user's "Gold Membership" plan and adding it to the body tag.
/* Add Page Slug to Body Class */
add_filter( 'body_class', 'am_add_slug_body_class' );
function am_add_slug_body_class( $classes ) {
global $post;
if ( isset( $post ) ) {
$classes[] = $post->post_type . '-' . $post->post_name;
}
return $classes;
}
/* Add Logged-Out Body Class */
add_filter('body_class','am_logged_in_filter');
function am_logged_in_filter($classes) {
if( is_user_logged_in() ) {
$classes[] = '';
} else {
$classes[] = 'logged-out';
}
return $classes;
}
/* Add Gold Membership to Body Class */
add_filter('body_class','am_membership_class_filter');
function am_membership_class_filter($classes) {
if( wc_memberships_is_user_member( null, 'gold-membership' ) ) {
$classes = 'gold-membership';
}
return $classes;
}
However, this last "Gold Membership" function removes all other body classes that I have added prior, rendering any of my "Logged-In" or page-specific CSS non-functional.