Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save KingMatrix1989/c68070d65441ccc4f0273632cc20a25d to your computer and use it in GitHub Desktop.
Save KingMatrix1989/c68070d65441ccc4f0273632cc20a25d to your computer and use it in GitHub Desktop.
memberships: get plan restriction rules, check for product categories
<?php
// get plan restriction rules if we have an ID, could be used in member area for example
$plan = wc_memberships_get_membership_plan( $plan_id );
$product_rules = $plan->get_product_restriction_rules();
foreach ( $product_rules as $rule ) {
// get the ids of restricted items from the rules
$restricted_items = $rule->get_object_ids();
foreach ( $restricted_items as $item_id ) {
// cast the item ID as an int
$item_id = (int) $item_id;
// check if the restricted objects are terms or not
$term = get_term( $item_id, 'product_cat' );
// if we don't have a term, catch the exception and carry on
if ( is_wp_error( $term ) ) {
continue;
} else {
// if we do have a term, do some stuff
// do whatever you want with $term here -- ie loop categories and show a list, whatever
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment