You are here

commerce_conditions.module in Commerce Extra Rules Conditions 7

Defines functions used for building and processing Rules conditions.

File

commerce_conditions.module
View source
<?php

/**
 * @file
 * Defines functions used for building and processing Rules conditions.
 */

/**
 * Rule condition for checking if a line item has a product with a certain term
 * id
 * 
 * @param object $line_item
 *   A commerce_line_item containing the product being checked
 * @param string $field_name
 *   A string containing the machine name of a Taxonomy reference field
 * @param integer $term_id
 *   An integer corresponding to a Taxonomy term id
 * 
 * @return
 *   TRUE if the product has the term applied to it on the field $field_name
 *   Otherwise FALSE
 */
function commerce_conditions_rules_condition_has_term($line_item, $field_name, $term_id) {
  $field_name = trim($field_name);
  return commerce_conditions_has_term($line_item, $field_name, $term_id);
}

/**
 * Calculates the quantity of products in an order that have the term $term_id
 * and compares it to a given value
 * 
 * @param object $order
 *   A commerce_order containing the products being checked
 * @param string $field_name
 *   A string containing the machine name of a Taxonomy reference field
 * @param integer $term_id
 *   An integer corresponding to a Taxonomy term id
 * @param string $operator
 *   A string containing the operator used comparing the calculated quantity to 
 *   $value
 * @param integer $value
 *   An integer to compare to the quantity of products containing $term_id in
 *   the $field_name field
 * 
 * @return
 *   The result of evaluating the calculated quantity against $value with the
 *   specified operator.
 */
function commerce_conditions_compare_termed_product_quantity($order, $field_name, $term_id, $operator, $value) {
  $quantity = 0;
  $field_name = trim($field_name);
  if (!empty($order)) {
    $wrapper = entity_metadata_wrapper('commerce_order', $order);
    $line_items = $wrapper->commerce_line_items
      ->value();
    if (!empty($line_items)) {

      //Holds line items that have products with $term_id
      $termed_line_items = array();
      foreach ($line_items as $line_item) {
        if (in_array($line_item->type, commerce_product_line_item_types())) {
          if (commerce_conditions_has_term($line_item, $field_name, $term_id)) {
            $termed_line_items[] = $line_item;
          }
        }
      }
      if (!empty($termed_line_items)) {
        $quantity = commerce_line_items_quantity($termed_line_items, commerce_product_line_item_types());
      }
    }
  }

  // Make a quantity comparison based on the operator.
  switch ($operator) {
    case '<':
      return $quantity < $value;
    case '<=':
      return $quantity <= $value;
    case '=':
      return $quantity == $value;
    case '>=':
      return $quantity >= $value;
    case '>':
      return $quantity > $value;
  }
  return FALSE;
}

/**
 * Check if a line item has a product with a certain term id
 * 
 * @param object $line_item
 *   A commerce_line_item containing the product being checked
 * @param string $field_name
 *   A string containing the machine name of a Taxonomy reference field
 * @param integer $term_id
 *   An integer corresponding to a Taxonomy term id
 * 
 * @return
 *   TRUE if the product has the term applied to it on the field $field_name
 *   Otherwise FALSE
 */
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;
}

/**
 * Check if a line item has a product with certain term ids
 * 
 * @param object $line_item
 *   A commerce_line_item containing the product being checked
 * @param string $field_name
 *   A string containing the machine name of a Taxonomy reference field
 * @param array $term_ids
 *   An array containing Taxonomy term ids to check for
 * @param boolean $require_all
 *   A boolean variable indicating whether or not all term ids must be present
 * 
 * @return
 *   TRUE if the product has the term applied to it on the field $field_name
 *   Otherwise FALSE
 */
function commerce_conditions_rules_condition_has_terms($line_item, $field_name, $term_ids, $require_all) {
  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 ($require_all) {
        foreach ($term_ids as $term_id) {

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

        //If we haven't returned FALSE already then TRUE must be the answer.
        return TRUE;
      }
      else {
        foreach ($term_ids as $term_id) {

          // 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;
}

/**
 * Returns an array of machine_names for taxonomy term reference fields
 */
function commerce_conditions_term_fields_options_list() {
  $field_options_list = array();
  $fields = field_read_fields(array(
    'type' => 'taxonomy_term_reference',
  ));
  if (!empty($fields)) {
    foreach ($fields as $key => $value) {
      $field_options_list[$key] = $key;
    }
  }
  return $field_options_list;
}

/**
 * Takes the machine name of a Taxonomy reference field and retrieves the terms
 * for the associated vocabulary.
 * 
 * @param string $field_name
 *   A string containing the machine name of a Taxonomy reference field.
 * 
 * @return
 *   An array containing the term names of the vocabulary tied to $field_name
 *   prefixed with a certain number of dashes(-) corresponding to the depth of
 *   the term. Term names are keyed by term id.
 */
function commerce_conditions_get_terms_list($field_name) {
  $field = field_read_field($field_name);
  $vocabulary = taxonomy_vocabulary_machine_name_load($field['settings']['allowed_values'][0]['vocabulary']);
  $terms = taxonomy_get_tree($vocabulary->vid);
  $term_list = array();
  if (!empty($terms)) {
    foreach ($terms as $term) {
      $term_list[$term->tid] = str_repeat('-', $term->depth) . $term->name;
    }
  }
  return $term_list;
}

/**
 * Condition callback: compares the total quantity of products of a specific type
 * against the specified quantity.
 */
function commerce_conditions_compare_total_product_type_quantity($order, $product_type, $exclude, $operator, $value) {
  $total = 0;

  // If we received an order, get the total quantity of products on it.
  if (!empty($order)) {
    $order_wrapper = entity_metadata_wrapper('commerce_order', $order);

    // Populate the array of the quantities of the products on the order.
    foreach ($order_wrapper->commerce_line_items as $delta => $line_item_wrapper) {
      if (in_array($line_item_wrapper->type
        ->value(), commerce_product_line_item_types())) {

        // Get the product
        $product_wrapper = entity_metadata_wrapper('commerce_product', $line_item_wrapper->commerce_product->product_id
          ->value());

        // Extract a product type and quantity from the line item.
        $line_item_product_type = $product_wrapper->type
          ->value();
        $quantity = $line_item_wrapper->quantity
          ->value();

        // Update the product's quantity value.
        if ($exclude) {
          if ($line_item_product_type != $product_type) {
            $total += $quantity;
          }
        }
        else {
          if ($line_item_product_type == $product_type) {
            $total += $quantity;
          }
        }
      }
    }
  }

  // Make a quantity comparison based on the operator.
  switch ($operator) {
    case '<':
      return $total < $value;
    case '<=':
      return $total <= $value;
    case '=':
      return $total == $value;
    case '>=':
      return $total >= $value;
    case '>':
      return $total > $value;
  }
  return FALSE;
}

/**
 * An empty options list so that Rules conditions that need manually populated
 * options sets will have their form variables populated correctly.
 */
function commerce_conditions_empty_options_list() {
  return array();
}

Functions

Namesort descending Description
commerce_conditions_compare_termed_product_quantity Calculates the quantity of products in an order that have the term $term_id and compares it to a given value
commerce_conditions_compare_total_product_type_quantity Condition callback: compares the total quantity of products of a specific type against the specified quantity.
commerce_conditions_empty_options_list An empty options list so that Rules conditions that need manually populated options sets will have their form variables populated correctly.
commerce_conditions_get_terms_list Takes the machine name of a Taxonomy reference field and retrieves the terms for the associated vocabulary.
commerce_conditions_has_term Check if a line item has a product with a certain term id
commerce_conditions_rules_condition_has_term Rule condition for checking if a line item has a product with a certain term id
commerce_conditions_rules_condition_has_terms Check if a line item has a product with certain term ids
commerce_conditions_term_fields_options_list Returns an array of machine_names for taxonomy term reference fields