Skip to content

Instantly share code, notes, and snippets.

@bekarice
Created February 8, 2016 21:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bekarice/e5ca4713698f51a6cbce to your computer and use it in GitHub Desktop.
Save bekarice/e5ca4713698f51a6cbce to your computer and use it in GitHub Desktop.
WC Memberships example: check if user is active member of any plan (prior to Memberships 1.4)
<?php
/**
* 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
*
* (WC Memberships older than v1.4)
*
* @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 membership plans
$plans = wc_memberships_get_membership_plans();
$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 );
}
// only proceed if the user has no active memberships
if ( ! in_array( true, $active_member ) ) {
// 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' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment