[Condition] User Belongs To WLM Membership(s)

le 26/10/2021
JAG

This members-only tutorial provides the steps to register a custom condition using which elements in Oxygen can be set to be output only if the currently logged in user belongs to the selected membership level when using WishList Member.

To check against multiple membership levels, condition type can be set to OR.
Both == and != operators are made available meaning, it is possible to output elements based on whether the current logged in member belongs to or does not belong to the selected membership(s).
Step 1
Install and activate Code Snippets plugin.
Go to Snippets > Add New.
Title: [Condition] User Belongs To WLM Membership(s)
Code:

class WPDD_WLMapi_Condition {
static $associative_array;

static function init() {
add_action( 'plugins_loaded', array( __CLASS__, 'register_condition' ) );
}

static function register_condition() {
// Retrieves all membership levels associated with WLM.
// http://codex.wishlistproducts.com/function-reference-wlmapi_get_levels/
$levels = wlmapi_get_levels();

// Empty array to store the names of membership levels.
$level_names_array = [];

// Empty array to store id=>level.
self::$associative_array = [];

// store all the level names in $level_names_array and all id=>level key=>value pairs in $associative_array.
foreach ( $levels['levels']['level'] as $level ) {
$level_names_array[] = $level['name'];
self::$associative_array[$level['id']] = $level['name'];
}

if ( function_exists( 'oxygen_vsb_register_condition' ) ) {
oxygen_vsb_register_condition(
// Condition Name
'Belongs To WLM Membership(s)',

// Values: The array of pre-set values the user can choose from.
// Set the custom key's value to true to allow users to input custom values.
array(
'options' => $level_names_array,
'custom' => false
),

// Operators
array( '==', '!=' ),

// Callback Function: Name of function that will be used to handle the condition
// the callback cannot be a member method of a class because in Oxygen's conditions API, the presence of callback is checked using function_exists, rather than method_exists
'wpdd_wlm_membership_callback',

// Condition Category: Default ones are Archive, Author, Other, Post, User
'User'
);
}
}

/**
* Callback function to handle the condition.
* @param mixed $value Input value - in this case, the name of membership level.
* @param string $operator Comparison operator selected by the user.
*
* @return boolean true or false.
*/
static function wpdd_wlm_membership_callback( $value, $operator ) {
// Get the id of selected level.
// https://www.php.net/manual/en/function.array-search.php
$id = array_search( $value, self::$associative_array );

return $operator == "==" ? wlmapi_is_user_a_member( $id, get_current_user_id() ) : ! wlmapi_is_user_a_member( $id, get_current_user_id() );
}
}

// the callback cannot be a member method of a class because in Oxygen's conditions API, the presence of callback is checked using function_exists, rather than method_exists
function wpdd_wlm_membership_callback( $value, $operator ) {
return WPDD_WLMapi_Condition::wpdd_wlm_membership_callback( $value, $operator );
}

WPDD_WLMapi_Condition::init();
Set the snippet to run everywhere. Save changes and activate.
Step 2
Apply the condition to any element in Oxygen that should be shown or not shown to members based on the membership level they belong to.
That’s it!

Below is some notes for my reference.
I’ve initially put together the condition using non object-oriented PHP code.
// Retrieves all membership levels associated with WLM.
// http://codex.wishlistproducts.com/function-reference-wlmapi_get_levels/
$levels = wlmapi_get_levels();

// Empty array to store the names of membership levels.
$level_names_array = [];

// Empty array to store id=>level.
$associative_array = [];

foreach ( $levels['levels']['level'] as $level ) {
$level_names_array[] = $level['name'];
$associative_array[$level['id']] = $level['name'];
}

if ( function_exists( 'oxygen_vsb_register_condition' ) ) {

oxygen_vsb_register_condition(
// Condition Name
'Belongs To WLM Membership(s)',

// Values: The array of pre-set values the user can choose from.
// Set the custom key's value to true to allow users to input custom values.
array(
'options' => $level_names_array,
'custom' => false
),

// Operators
array( '==', '!=' ),

// Callback Function: Name of function that will be used to handle the condition
'wpdd_wlm_membership_callback',

// Condition Category: Default ones are Archive, Author, Other, Post, User
'User'
);

}

/**
* Callback function to handle the condition.
* @param mixed $value Input value - in this case, the name of membership level.
* @param string $operator Comparison operator selected by the user.
*
* @return boolean true or false.
*/
function wpdd_wlm_membership_callback( $value, $operator ) {
// Get the id of selected level.
// https://www.php.net/manual/en/function.array-search.php
$id = array_search( $value, $associative_array );

return $operator == "==" ? wlmapi_is_user_a_member( $id, get_current_user_id() ) : ! wlmapi_is_user_a_member( $id, get_current_user_id() );
}
But this does not work.
Here’s why:
the problem was due to the array $associtive_array, not being available to the callback function during runtime. I have made some changes to the code, primarily to isolate the scope of variables, but putting them as static members inside a class.Gagan S Goraya
Many thanks to Gagan for “OOPifying” the code.
References
http://codex.wishlistproducts.com/function-reference-wlmapi_get_levels/
https://stackoverflow.com/a/10152943/778809
https://stackoverflow.com/a/3833907/778809

get_current_user_id()


https://stackoverflow.com/a/6266378/778809
http://codex.wishlistproducts.com/function-reference-wlmapi_is_user_a_member/

Nos experts se tiennent à votre disposition
pour répondre à vos questions et vous conseiller
dans votre projet digital.

Vous souhaitez développer une application ?

Envie d’échanger avec l’un de nos experts ? Besoin d’en savoir plus ?
En savoir plus

Recevez notre newsletter

La protection des données nous tient à cœur. JAG utilise les informations que vous fournissez afin de vous proposer des informations et du contenu pertinent sur nos produits et services. Vous pouvez vous désinscrire de ce type de communications à tout moment. Pour plus d'informations, consultez notre politique de confidentialité
© 2022 JAG Consulting, sas. Tous droits réservés.
© 2022 JAG Consulting, sas. Tous droits réservés.
4.8
Lire les 51 avis
google
Brieuc Pauly
08/09/2022

Créative, dynamique et très professionnelle, c'est à mon sens les piliers qui soutiennent cette équipe talentueuse ! Une agence à taille humaine qui privilégie la qualité et le résultat. Alors si vous souhaitez le meilleur pour votre communication adressez vous les yeux fermés à ces passionnés, plus que compétents ! (Translated by Google) Creative, dynamic and very professional, these are in my opinion the pillars that support this talented team! A human-sized agency that favors quality and results. So if you want the best for your communication, turn your eyes closed to these enthusiasts, who are more than competent!

google
Amaury Tardier
08/09/2022

Merci à Julien et son équipe, plus que satisfait par le travail fourni ! Je recommande vivement ! (Translated by Google) Thanks to Julien and his team, more than satisfied with the work provided! I highly recommend !

Ravi de travailler avec l'équipe JAG pour la gestion de mon site de e-Commerce. Equipe réactive et toujours pleine de bonnes idées pour améliorer les performances du site (Translated by Google) Glad to work with the JAG team for the management of my e-Commerce site. Responsive team always full of good ideas to improve the performance of the site

Merci à Julien pour son accompagnement dans mon projet, je recommande à 200% c est le Top (Translated by Google) Thanks to Julien for his support in my project, I recommend 200% it is the top

Excellente agence. Equipe est très pro, à l'écoute et de bon conseils. (Translated by Google) Excellent agency. The team is very professional, attentive and gives good advice.

Temps de réponse hors pair quand il y a eu un pb sur mon site, je vous recommande chaleureusement Julien :-) (Translated by Google) Unparalleled response time when there was a problem on my site, I warmly recommend Julien :-)

Charger plus
listarrow-right
Julien

Julien

Votre conseiller

I will be back soon

Une question ?

Julien
Bonjour ! Je suis Julien.
Comment puis-je vous aider?
Démarrer la conversation:
chat Live Chat