You are here

function commerce_conditions_has_term in Commerce Extra Rules Conditions 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_conditions_has_term()
commerce_conditions_compare_termed_product_quantity in ./commerce_conditions.module
Calculates the quantity of products in an order that have the term $term_id and compares it to a given value
commerce_conditions_rules_condition_has_term in ./commerce_conditions.module
Rule condition for checking if a line item has a product with a certain term id

File

./commerce_conditions.module, line 102
Defines functions used for building and processing Rules conditions.

Code

function commerce_conditions_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;
}