Skip to content

Instantly share code, notes, and snippets.

@bekarice
Created April 19, 2016 20:57
Show Gist options
  • Select an option

  • Save bekarice/35ac6cc853a17064131fa144125e9deb to your computer and use it in GitHub Desktop.

Select an option

Save bekarice/35ac6cc853a17064131fa144125e9deb to your computer and use it in GitHub Desktop.

Revisions

  1. bekarice created this gist Apr 19, 2016.
    28 changes: 28 additions & 0 deletions memberships-get-rules-sample.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    <?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
    }
    }
    }