Created
July 11, 2021 17:16
-
-
Save jorpdesigns/e7d5f93ddcda57821af521d9199836fe to your computer and use it in GitHub Desktop.
Snippet to add customer WooCommerce Membership plan as body class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( 'body_class', 'membership_body_class' ); | |
function membership_body_class( $classes ) { | |
if ( ! function_exists( 'wc_memberships' ) ) { | |
return $classes; | |
} | |
$user_id = get_current_user_id(); | |
$args = array( | |
'status' => array( 'active', 'pending-cancellation' ), // Add or remove other statuses if you want | |
); | |
// Get user memberships | |
$active_memberships = wc_memberships_get_user_memberships( $user_id, $args ); | |
$membershipPlansArray = array(); | |
if ( ! empty( $active_memberships ) ) { | |
// Get user membership plans | |
foreach( $active_memberships as $membership ) { | |
$membershipPlansArray[] = $membership->plan; | |
} | |
// Get slug for each membership plan and add as class | |
foreach( $membershipPlansArray as $membershipPlan ) { | |
$classes[] = $membershipPlan->slug; | |
} | |
} | |
return $classes; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment