Last active
August 14, 2020 19:10
-
-
Save mzykin/4317de2aa173ab14435ea9aa58f5fc2c to your computer and use it in GitHub Desktop.
WooCommerce Memberships shortcodes and other tweaks
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 | |
/* --------------------------------------------------- | |
/** WooCommerce Memberships | |
--------------------------------------------------- */ | |
/** | |
* Creates a shortcode to output the "My Memberships" table anywhere on the site | |
* Outputs this section only if the current user has 1 or more memberships | |
* | |
* Use the shortcode: [wcm_my_memberships] | |
* | |
* @return void|string buffered HTML contents | |
*/ | |
add_shortcode( 'wcm_my_memberships', 'gwcp_wc_memberships_my_memberships_shortcode' ); | |
function gwcp_wc_memberships_my_memberships_shortcode() { | |
// bail if Memberships isn't active or we're in the admin | |
if ( ! function_exists( 'wc_memberships' ) || is_admin() ) { | |
return; | |
} | |
$user_id = get_current_user_id(); | |
// bail if this is user is not logged in | |
if ( ! is_numeric( $user_id ) || 0 === $user_id ) { | |
return array(); | |
} | |
$args = array( | |
'status' => array( 'active', 'pending' ), | |
); | |
// buffer contents | |
ob_start(); | |
?> | |
<div class="woocommerce"> | |
<h2><?php esc_html_e( 'My Programs', 'gwcp' ); ?></h2> | |
<?php | |
wc_get_template( 'myaccount/my-memberships.php', | |
array( | |
'customer_memberships' => wc_memberships_get_user_memberships( $user_id, $args ), | |
'user_id' => get_current_user_id(), | |
) ); | |
?> | |
</div> | |
<?php | |
// output buffered content | |
return ob_get_clean(); | |
} | |
/** | |
* Creates a shortcode to output the “My Membership Content” table anywhere on the site | |
* Outputs this section only if the current user has 1 or more memberships | |
* | |
* Use the shortcode: [wcm_my_memberships_content] | |
*/ | |
add_shortcode( 'wcm_my_memberships_content', 'gwcp_wc_memberships_my_memberships_content_shortcode' ); | |
function gwcp_wc_memberships_my_memberships_content_shortcode() { | |
// bail if Memberships isn't active or we're in the admin | |
if ( ! function_exists( 'wc_memberships' ) || is_admin() ) {return; } | |
$user_id = get_current_user_id(); | |
// bail if this is user is not logged in | |
if ( ! is_numeric( $user_id ) || 0 === $user_id ) { | |
return array(); | |
} | |
$args = array( | |
'status' => array( 'active', 'pending' ), | |
); | |
$active_memberships = wc_memberships_get_user_memberships( $user_id, $args ); | |
ob_start(); | |
?><div class="woocommerce"><?php | |
foreach ( $active_memberships as $membership ) { | |
if( $membership->plan->name === 'Member' || $membership->plan->name === 'Site Admins' ) { | |
echo ''; | |
} | |
else { | |
echo '<h1>'.$membership->plan->name.'</h1>'; | |
wc_get_template( 'myaccount/my-membership-content.php', | |
array( | |
'customer_membership' => $membership, | |
'restricted_content' => $membership->get_plan()->get_restricted_content(), | |
'user_id' => get_current_user_id(), | |
) | |
); | |
} | |
// output buffered content | |
} | |
?></div><?php | |
return ob_get_clean(); | |
} | |
/** | |
* Creates a shortcode to output the “My Membership Notes” table anywhere on the site | |
* Outputs this section only if the current user has 1 or more memberships | |
* | |
* Use the shortcode: [wcm_my_memberships_notes] | |
*/ | |
add_shortcode( 'wcm_my_memberships_notes', 'gwcp_wc_memberships_my_memberships_notes_shortcode' ); | |
function gwcp_wc_memberships_my_memberships_notes_shortcode() { | |
// bail if Memberships isn't active or we're in the admin | |
if ( ! function_exists( 'wc_memberships' ) || is_admin() ) {return; } | |
$user_id = get_current_user_id(); | |
// bail if this is user is not logged in | |
if ( ! is_numeric( $user_id ) || 0 === $user_id ) { | |
return array(); | |
} | |
$args = array( | |
'status' => array( 'active', 'pending', 'complimentary' ), | |
); | |
$active_memberships = wc_memberships_get_user_memberships( $user_id, $args ); | |
ob_start(); | |
?><div class="woocommerce"><?php | |
foreach ( $active_memberships as $membership ) { | |
if( $membership->plan->name === 'Site Admins' ) { | |
echo ''; | |
} | |
else{ | |
echo '<h1>'.$membership->plan->name.'</h1>'; | |
wc_get_template( 'myaccount/my-membership-notes.php', | |
array( | |
'customer_membership' => $membership, | |
'customer_notes' => $membership->get_notes( 'customer', $paged ), | |
'user_id' => get_current_user_id(), | |
) | |
); | |
} | |
// output buffered content | |
} | |
?></div><?php | |
return ob_get_clean(); | |
} | |
/** | |
* Changes the name of the members area menu item. | |
* | |
* @param string $title the endpoint title | |
* @param \WC_Memberships_Membership_Plan|null $plan the current membership plan or null if viewing the memberships list (unused) | |
* @return string updated title | |
*/ | |
add_filter( 'wc_memberships_my_account_memberships_title', 'gwcp_wc_memberships_change_account_menu_item', 10, 2 ); | |
function gwcp_wc_memberships_change_account_menu_item( $title, $plan ) { | |
// change this to your desired text | |
return 'Wellness Programs'; | |
} | |
/** | |
* Remove the "Cancel" action for members from the "My Memberships" table | |
* Memberships will have to be cancelled manually by a shop admin | |
* | |
* @param array $actions the array of membership action links | |
* @return array $actions the updated array of actions | |
*/ | |
add_filter( 'wc_memberships_members_area_my-memberships_actions', 'gwcp_edit_my_memberships_actions' ); | |
function gwcp_edit_my_memberships_actions( $actions ) { | |
unset( $actions['cancel'] ); | |
return $actions; | |
} | |
/** | |
* Remove and rename the columns from the My Memberships table | |
* | |
* @param array $columns the columns in the "My Memberships" table | |
* @return array $columns the updated array of columns | |
*/ | |
add_filter( 'wc_memberships_members_area_my_membership_notes_column_names', 'gwcp_wc_memberships_notes_manage_columns' ); | |
function gwcp_wc_memberships_notes_manage_columns( $columns ) { | |
$columns['membership-note-author'] = __('Sender', 'gwcp'); | |
$columns['membership-note-content'] = __('Message', 'gwcp'); | |
return $columns; | |
} | |
/** | |
* Remove and rename the columns from the My Memberships table | |
* | |
* @param array $columns the columns in the "My Memberships" table | |
* @return array $columns the updated array of columns | |
*/ | |
add_filter( 'wc_memberships_my_memberships_column_names', 'gwcp_wc_memberships_manage_columns' ); | |
function gwcp_wc_memberships_manage_columns( $columns ) { | |
unset( $columns['membership-start-date'] ); | |
unset( $columns['membership-end-date'] ); | |
unset( $columns['membership-status'] ); | |
$columns['membership-plan'] = __('Program', 'gwcp'); | |
return $columns; | |
} | |
/** | |
* Remove the "Type" column from the My Content section of the member area | |
* | |
* @param array $columns the columns in the "My Content" table | |
* @return array $columns the updated array of columns | |
*/ | |
add_filter( 'wc_memberships_members_area_my_membership_content_column_names', 'gwcp_wc_memberships_my_content_table_columns', 11 ); | |
function gwcp_wc_memberships_my_content_table_columns( $columns ) { | |
// unset the "type" column, which shows post, page, etc | |
unset( $columns['membership-content-accessible'] ); | |
unset( $columns['membership-content-type'] ); | |
return $columns; | |
} | |
/** | |
* v1.9+: Changes the navigation title of the members area section | |
* For example, show "Exclusive Discounts" instead of "Discounts" | |
* | |
* < v1.9: Changes the section header | |
* | |
* @param string $title the heading text for the member area section | |
* @return string $title the updated heading text | |
*/ | |
add_filter( 'wc_memberships_members_area_my_membership_notes_title', 'gwcp_wc_memberships_members_area_section_title' ); | |
function gwcp_wc_memberships_members_area_section_title( $title ) { | |
return __( 'Messages', 'gwcp' ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment