Created
November 28, 2023 12:38
-
-
Save Lonsdale201/726c1ddfe03b18145759a94357477fb6 to your computer and use it in GitHub Desktop.
JetEngine - Custom Macro - Current user Memberships (multiple support)
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
| // place the code in the child theme functions.php or a custom code snippets plugin. | |
| // how to use | |
| // Create a new User Query in the Query builder. | |
| // Go to the Include/exclude section | |
| // Click the Dynamic tag icon, and select the WC Current User memberships macro. In the text field write your membership IDs and done. | |
| // this version can handle multiple memberip id's | |
| // Include or Exclude can apply this macro. | |
| // This macro will return the selected user id | |
| // If the membership ID not exist, or not have user with selected ID membership active, will return | |
| // My all Gist: https://gist.github.com/Lonsdale201 | |
| add_action( 'jet-engine/register-macros', function(){ | |
| class WC_Current_User_Memberships extends \Jet_Engine_Base_Macros { | |
| public function macros_tag() { | |
| return 'wc_current_user_memberships'; | |
| } | |
| public function macros_name() { | |
| return 'WC Current User Memberships'; | |
| } | |
| public function macros_args() { | |
| return array( | |
| 'membership_ids' => array( | |
| 'label' => 'Membership IDs', | |
| 'type' => 'text', | |
| 'default' => '', | |
| ), | |
| ); | |
| } | |
| public function macros_callback( $args = array() ) { | |
| $membership_ids_input = !empty( $args['membership_ids'] ) ? $args['membership_ids'] : ''; | |
| $membership_ids = array_map('intval', explode(',', $membership_ids_input)); | |
| $membership_ids = array_filter($membership_ids); | |
| if ( empty( $membership_ids ) || !function_exists( 'wc_memberships_get_user_memberships' )) { | |
| return 'Invalid Membership IDs'; | |
| } | |
| $valid_membership_ids = array_filter($membership_ids, function($id) { | |
| return wc_memberships_get_membership_plan( $id ) !== false; | |
| }); | |
| if ( empty( $valid_membership_ids ) ) { | |
| return 'No valid Membership Plans for the given IDs'; | |
| } | |
| $user_ids = array(); | |
| $users = get_users( array( 'fields' => 'ids' ) ); | |
| foreach ( $users as $user_id ) { | |
| $memberships = wc_memberships_get_user_memberships( $user_id, array( 'status' => array( 'active' ) ) ); | |
| foreach ( $memberships as $membership ) { | |
| if ( in_array( $membership->get_plan_id(), $valid_membership_ids ) ) { | |
| $user_ids[] = $user_id; | |
| break; | |
| } | |
| } | |
| } | |
| if ( empty( $user_ids ) ) { | |
| return 'No users found for the given Membership IDs'; | |
| } | |
| return implode( ',', array_unique( $user_ids ) ); | |
| } | |
| } | |
| new WC_Current_User_Memberships(); | |
| } ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment