Skip to content

Instantly share code, notes, and snippets.

@Basilakis
Last active April 15, 2020 08:29
Show Gist options
  • Save Basilakis/1e5c8a0a68b1aaaa8667c5fc4fe6e52e to your computer and use it in GitHub Desktop.
Save Basilakis/1e5c8a0a68b1aaaa8667c5fc4fe6e52e to your computer and use it in GitHub Desktop.
Check if WooCommerce Restrict Content user has Membership
<?php
/*
* More Refrences: https://docs.woocommerce.com/document/woocommerce-memberships-admin-hook-reference/
* Function that checked if user has access to this specific content
* Usage: if(can_user_access_content(get_current_user_id(),$post->ID)){ }
*/
function can_user_access_content($user_id,$post_id){
//check if there's a force public on this content
if(get_post_meta($post_id,'_wc_memberships_force_public',true)=='yes') return true;
$args = array( 'status' => array( 'active' ));
$plans = wc_memberships_get_user_memberships( $user_id, $args );
$user_plans = array();
foreach($plans as $plan){
array_push($user_plans,$plan->plan_id);
}
$rules = wc_memberships()->get_rules_instance()->get_post_content_restriction_rules( $post_id );
foreach($rules as $rule){
if(in_array($rule->get_membership_plan_id(), $user_plans)){
return true;
}
}
return false;
}
/*
* You can replace the top function with this, to check if any plan and not just the subscribed
*/
$plans = wc_memberships_get_membership_plans();
// set a flag
$active_member = array();
// check if the member has an active membership for any plan
foreach ( $plans as $plan ) {
$active = wc_memberships_is_user_active_member( get_current_user_id(), $plan );
array_push( $active_member, $active );
}
/*
* You can replace the top function with this, to check if any plan and not just the subscribed
*/
add_filter( 'wc_membership_plan_data_tabs', 'rudr_membershiptabs' ); /* add tabs */
add_action( 'wc_membership_plan_data_panels', 'rudr_membershiptabcontent' ); /* add tab content */
/**
* @param array $tabs contains all WooCommerce Membership plan tabs data
*/
function rudr_membershiptabs( $tabs ) {
$tabs['truemailchimp'] = array(
'label' => 'Mailchimp',
'target' => 'rudr_mailchimp' /* ID of the HTML tab body element */
);
return $tabs;
}
function rudr_membershiptabcontent(){
global $post;
$tabbody = '<div id="rudr_mailchimp" class="panel woocommerce_options_panel">
<div class="table-wrap">
<p>Please, enter the Mailchimp list ID to synchronize active users of this plan with it.</p>
<div class="options_group">';
wp_nonce_field( basename( __FILE__ ), 'rudr_mchimp_nonce' );
$list_id = get_post_meta( $post->ID, 'rudr_list_id', true );
$tabbody .= '<p class="form-field post_name_field ">
<label for="rudr_list_id">List ID:</label>
<input type="text" name="rudr_list_id" id="rudr_list_id" value="' . esc_attr( $list_id ) . '" />
</p>
</div><!--.option_group-->
</div><!--.table-wrap-->
</div><!--#rudr_mailchimp-->';
echo $tabbody;
}
// Save the field value
add_action( 'save_post', 'rudr_membershipsave' );
/**
* @param int $post_id ID of the membership plan
*/
function rudr_membershipsave( $post_id ){
if ( !isset( $_POST['rudr_mchimp_nonce'] ) || !wp_verify_nonce( $_POST['rudr_mchimp_nonce'], basename( __FILE__ ) ) )
return $post_id;
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return $post_id;
if ( !current_user_can( 'edit_post', $post_id ) )
return $post_id;
$post = get_post( $post_id );
/* save data only for membership plan custom post type */
if ( $post->post_type == 'wc_membership_plan' )
update_post_meta( $post_id, 'rudr_list_id', sanitize_text_field( $_POST['rudr_list_id'] ) );
return $post_id;
}
/**
* Modify membership product pricing display for non-members
* changes pricing display if purchasing is restricted to members;
* active members will see the price instead of a message
*
* @param string $price the WC price HTML
* @return string $price the updated price HTML
*/
function sv_change_member_product_price_display( $price ) {
// bail if Memberships isn't active
if ( ! function_exists( 'wc_memberships' ) ) {
return $price;
}
// get any active user memberships (requires Memberships 1.4+)
$user_id = get_current_user_id();
$args = array(
'status' => array( 'active', 'complimentary', 'pending' ),
);
$active_memberships = wc_memberships_get_user_memberships( $user_id, $args );
// only proceed if the user has no active memberships
if ( empty( $active_memberships ) ) {
// change price display if purchasing is restricted
if ( wc_memberships_is_product_purchasing_restricted() ) {
$price = 'Price for members only';
}
}
return $price;
}
add_filter( 'woocommerce_get_price_html', 'sv_change_member_product_price_display' );
add_filter( 'woocommerce_cart_item_price', 'sv_change_member_product_price_display' );
/**
* There are two ways we could prevent purchasing more than one membership:
* 1. prevent access if the customer already has an active membership
* 2. prevent access if the customer already has any membership
*
* This snippet shows the second scenario.
*/
/**
* Do not grant membership access to purchasers if they already hold any membership, regardless of status
*
* @param bool $grant_access true if the purchaser should get memberships access from this order
* @param array $args {
* @type int $user_id purchaser's WordPress user ID
* @type int $product_id the ID of the access-granting product
* @type int $order_id the ID of order for this purchase
* }
* @return bool $grant_access
*/
function sv_wc_memberships_limit_to_one_membership( $grant_access, $args ) {
// get all active memberships for the purchaser, regardless of status
$memberships = wc_memberships_get_user_memberships( $args['user_id'] );
// if there are any memberships returned, do not grant access from purchase
if ( ! empty( $memberships ) ) {
return false;
}
return $grant_access;
}
add_filter( 'wc_memberships_grant_access_from_new_purchase', 'sv_wc_memberships_limit_to_one_membership', 1, 2 );
/**
* There are two ways we could prevent purchasing more than one membership:
* 1. prevent access if the customer already has an active membership
* 2. prevent access if the customer already has any membership
*
* This snippet shows the first scenario.
*/
/**
* Do not grant membership access to purchasers if they already hold an active membership
*
* @param bool $grant_access true if the purchaser should get memberships access from this order
* @param array $args {
* @type int $user_id purchaser's WordPress user ID
* @type int $product_id the ID of the access-granting product
* @type int $order_id the ID of order for this purchase
* }
* @return bool $grant_access
*/
function sv_wc_memberships_limit_membership_count( $grant_access, $args ) {
// get all active memberships for the purchaser
// you can remove any of these if you don't want to allow multiples
// ie you may not want to count complimentary memberships
$statuses = array(
'status' => array( 'active', 'complimentary', 'pending', 'free_trial' ),
);
$active_memberships = wc_memberships_get_user_memberships( $args['user_id'], $statuses );
// if there are any active memberships returned, do not grant access from purchase
if ( ! empty( $active_memberships ) ) {
return false;
}
return $grant_access;
}
add_filter( 'wc_memberships_grant_access_from_new_purchase', 'sv_wc_memberships_limit_membership_count', 1, 2 );
/**
* Remove the "Renew" action for members from the "My Memberships" table
*
* @param array $actions the array of membership action links
* @return array $actions the updated array of actions
*/
function sv_wc_memberships_remove_renew_action( $actions ) {
if ( isset( $actions['renew'] ) ) {
unset( $actions['renew'] );
}
return $actions;
}
add_filter( 'wc_memberships_members_area_my-memberships_actions', 'sv_wc_memberships_remove_renew_action' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment