You are here

function commerce_rules_extra_has_term in Commerce Rules Extra 7

Check if a line item has a product with a certain term id

Parameters

object $line_item: A commerce_line_item containing the product being checked

string $field_name: A string containing the machine name of a Taxonomy reference field

integer $term_id: An integer corresponding to a Taxonomy term id

Return value

TRUE if the product has the term applied to it on the field $field_name Otherwise FALSE

2 calls to commerce_rules_extra_has_term()
commerce_rules_extra_compare_termed_product_quantity in ./commerce_rules_extra.rules.inc
Calculates the quantity of products in an order that have the term $term_id and compares it to a given value
commerce_rules_extra_rules_condition_has_term in ./commerce_rules_extra.rules.inc
Rule condition for checking if a line item has a product with a certain term id

File

./commerce_rules_extra.rules.inc, line 522
Defines functions used for building and processing Rules conditions.

Code

function commerce_rules_extra_has_term($line_item, $field_name, $term_id) {
  if (!empty($line_item)) {
    $wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
    $product = $wrapper->commerce_product
      ->value();
    if (isset($product->{$field_name})) {
      $product_terms = $wrapper->commerce_product->{$field_name}
        ->value();
    }
    if (!empty($product_terms)) {

      // If ther term reference field is set to allow more than one term
      // $product_terms will be an array
      if (is_array($product_terms)) {
        foreach ($product_terms as $product_term) {
          if ($product_term->tid == $term_id) {
            return TRUE;
          }
        }
      }
      else {
        if ($product_terms->tid == $term_id) {
          return TRUE;
        }
      }
    }
  }
  return FALSE;
}