function commerce_conditions_rules_condition_has_terms in Commerce Extra Rules Conditions 7
Check if a line item has a product with certain term ids
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
array $term_ids: An array containing Taxonomy term ids to check for
boolean $require_all: A boolean variable indicating whether or not all term ids must be present
Return value
TRUE if the product has the term applied to it on the field $field_name Otherwise FALSE
File
- ./
commerce_conditions.module, line 146 - Defines functions used for building and processing Rules conditions.
Code
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;
}