You are here

function _commerce_product_match_products_efq in Commerce Core 7

Helper function for commerce_product_match_products().

Returns an array of products matching the specific parameters via EFQ.

1 call to _commerce_product_match_products_efq()
commerce_product_match_products in modules/product/commerce_product.module
Fetches an array of all products matching the given parameters.

File

modules/product/commerce_product.module, line 961
Defines the core Commerce product entity, including the entity itself, the bundle definitions (product types), and various API functions to manage products and interact with them through forms and autocompletes.

Code

function _commerce_product_match_products_efq($instance, $string = '', $match = 'contains', $ids = array(), $limit = NULL, $access_tag = FALSE) {
  $query = new EntityFieldQuery();
  $query
    ->entityCondition('entity_type', 'commerce_product');

  // Add the access control tag if specified.
  if ($access_tag) {
    $query
      ->addTag('commerce_product_access');
  }

  // Add a global query tag so anyone can alter this query.
  $query
    ->addTag('commerce_product_match');

  // Add a condition to the query to filter by matching product types.
  if (!empty($instance['settings']['referenceable_types'])) {
    $types = array_diff(array_values($instance['settings']['referenceable_types']), array(
      0,
      NULL,
    ));

    // Only filter by type if some types have been specified.
    if (!empty($types)) {
      $query
        ->propertyCondition('type', $types, 'IN');
    }
  }
  if ($string !== '') {

    // EntityFieldQuery cannot do OR clauses, so we use hook_query_TAG_alter.
    $query
      ->addTag('commerce_sku_or_title_match');
    $sku_title_meta = new stdClass();
    $sku_title_meta->properties = array(
      'sku',
      'title',
    );
    $sku_title_meta->string = $string;
    $sku_title_meta->match = $match;
    $query
      ->addMetaData('commerce_sku_or_title_match', $sku_title_meta);
  }
  elseif ($ids) {

    // Otherwise add a product_id specific condition if specified.
    $query
      ->propertyCondition('product_id', $ids, 'IN');
  }

  // Order the results by SKU, title, and then product type.
  $query
    ->propertyOrderBy('sku')
    ->propertyOrderBy('title')
    ->propertyOrderBy('type');

  // Add a limit if specified.
  if ($limit) {
    $query
      ->range(0, $limit);
  }
  $entities = $query
    ->execute();
  $matches = array();
  if (isset($entities['commerce_product'])) {
    $pids = array_keys($entities['commerce_product']);

    // EntityFieldQuery doesn't return sku and title, so we have to load again.
    $products = commerce_product_load_multiple($pids);
    foreach ($products as $product) {
      $matches[$product->product_id] = array(
        'sku' => $product->sku,
        'type' => $product->type,
        'title' => $product->title,
        'rendered' => t('@sku: @title', array(
          '@sku' => $product->sku,
          '@title' => $product->title,
        )),
      );
    }
  }
  return $matches;
}