Skip to content

Instantly share code, notes, and snippets.

@bekarice
Created April 19, 2016 20:57
Show Gist options
  • Save bekarice/35ac6cc853a17064131fa144125e9deb to your computer and use it in GitHub Desktop.
Save bekarice/35ac6cc853a17064131fa144125e9deb 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