Created
December 2, 2023 11:39
-
-
Save Lonsdale201/94447d2a662c3dffbaa9903a1f9c6808 to your computer and use it in GitHub Desktop.
JetEngine - WooCommerce Memberships - Dynamic visibility - Memberships Access
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 | |
// Elementor ONLY | |
// Select your element in the elementor editor (container, widget etc..whatever), and go to the advanced tab, enable dynamic visibility option. | |
// Scroll down in the list, until you see the new category: wc_membership | |
// select the Access memberships | |
// Choose you logic, hide or show | |
// in the Memberships select field you can choose the memberships (one or more), this relation always "AND" | |
// Status always only the Active | |
// This visibility always check the current logged in user | |
// Of course, you can use the dynamic visibility AND / OR relation, to make more flexibility rules | |
// example image: https://prnt.sc/IQB-BUDLjGxI | |
add_action( 'jet-engine/modules/dynamic-visibility/conditions/register', function( $conditions_manager ) { | |
class WC_Access_Memberships extends \Jet_Engine\Modules\Dynamic_Visibility\Conditions\Base { | |
public function get_id() { | |
return 'wc-access-memberships'; | |
} | |
public function get_name() { | |
return __( 'Access Memberships', 'jet-engine' ); | |
} | |
public function get_group() { | |
return 'wc_membership'; | |
} | |
public function check( $args = array() ) { | |
$user_id = get_current_user_id(); | |
if ( !$user_id ) { | |
return false; | |
} | |
$selected_memberships = isset( $args['condition_settings']['selected_memberships'] ) ? $args['condition_settings']['selected_memberships'] : array(); | |
$user_memberships = wc_memberships_get_user_memberships( $user_id, array( 'status' => array('active') ) ); | |
$has_membership = true; | |
foreach ( $selected_memberships as $selected_membership ) { | |
$has_this_membership = false; | |
if ( $selected_membership == 'any_membership' ) { | |
$has_this_membership = !empty( $user_memberships ); | |
} else { | |
foreach ( $user_memberships as $membership ) { | |
if ( $membership->get_plan()->get_slug() == $selected_membership ) { | |
$has_this_membership = true; | |
break; | |
} | |
} | |
} | |
$has_membership = $has_membership && $has_this_membership; | |
} | |
$type = isset( $args['type'] ) ? $args['type'] : 'show'; | |
return ( 'hide' === $type ) ? !$has_membership : $has_membership; | |
} | |
public function get_custom_controls() { | |
$memberships = wc_memberships_get_membership_plans( array( 'post_status' => 'publish' ) ); | |
$options = array( | |
'any_membership' => __( 'Any Membership', 'jet-engine' ), | |
); | |
foreach ( $memberships as $membership ) { | |
$options[ $membership->get_slug() ] = $membership->get_name(); | |
} | |
return array( | |
'selected_memberships' => array( | |
'label' => __( 'Select Memberships', 'jet-engine' ), | |
'type' => 'select2', | |
'multiple' => true, | |
'default' => array(), | |
'options' => $options, | |
), | |
); | |
} | |
public function is_for_fields() { | |
return false; | |
} | |
public function need_value_detect() { | |
return false; | |
} | |
} | |
$conditions_manager->register_condition( new WC_Access_Memberships() ); | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment