You are here

function commerce_product_has_specified_terms_build in Commerce Discount 7

Build callback for commerce_product_has_specified_terms on product.

Parameters

EntityDrupalWrapper $wrapper: Wrapped entity type given by the rule.

array $terms: Values for the condition settings.

Return value

bool True is condition is valid. false otherwise.

1 string reference to 'commerce_product_has_specified_terms_build'
commerce_discount_inline_conditions_info in ./commerce_discount.inline_conditions.inc
Implements hook_inline_conditions_info().

File

./commerce_discount.rules.inc, line 840
Rules integration for the Commerce Discount module.

Code

function commerce_product_has_specified_terms_build(EntityDrupalWrapper $wrapper, $terms) {
  $terms_name = explode(', ', $terms);
  if (in_array('commerce_product', array_keys($wrapper
    ->getPropertyInfo()))) {

    // Fetch all the fields name of taxonomy_term type for the passed entity.
    foreach ($wrapper->commerce_product
      ->getPropertyInfo() as $field_name => $property) {
      if (preg_match('/taxonomy_term/', $property['type'])) {

        // If the wrapped field is an instance of EntityListWrapper class, that
        // means that field contains multiple values.
        if ($wrapper->commerce_product->{$field_name} instanceof EntityListWrapper) {
          foreach ($wrapper->commerce_product->{$field_name} as $wrapper_term) {
            if (($key = array_search($wrapper_term
              ->getIdentifier(), $terms_name)) !== FALSE) {
              unset($terms_name[$key]);
            }
          }
        }
        elseif ($term = $wrapper->commerce_product->{$field_name}
          ->value()) {
          if (($key = array_search($term->tid, $terms_name)) !== FALSE) {
            unset($terms_name[$key]);
          }
        }
      }
    }
  }
  return empty($terms_name);
}