You are here

function commerce_product_match_products in Commerce Core 7

Fetches an array of all products matching the given parameters.

This info is used in various places (allowed values, autocomplete results, input validation...). Some of them only need the product_ids, others product_id + titles, others yet product_id + titles + rendered row (for display in widgets).

The array we return contains all the potentially needed information, and lets calling functions use the parts they actually need.

Parameters

$field: The field description.

$string: Optional string to filter SKUs and titles on (used by autocomplete).

$match: Operator to match filtered SKUs and titles against, can be any of: 'contains', 'equals', 'starts_with'

$ids: Optional product ids to lookup (used when $string and $match arguments are not given).

$limit: If non-zero, limit the size of the result set.

$access_tag: Boolean indicating whether or not an access control tag should be added to the query to find matching product data. Defaults to FALSE.

Return value

An array of valid products in the form: array( product_id => array( 'product_sku' => The product SKU, 'title' => The product title, 'rendered' => The text to display in widgets (can be HTML) ), ... )

4 calls to commerce_product_match_products()
CommerceProductReferenceAdminTest::testCommerceProductReferenceReferenceProducts in modules/product_reference/tests/commerce_product_reference.test
Test adding some referenced products.
commerce_product_autocomplete in modules/product/commerce_product.module
Returns output for product autocompletes.
commerce_product_reference_field_validate in modules/product_reference/commerce_product_reference.module
Implements hook_field_validate().
commerce_product_reference_options_list in modules/product_reference/commerce_product_reference.module
Implements hook_options_list().

File

modules/product/commerce_product.module, line 823
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($field, $instance = NULL, $string = '', $match = 'contains', $ids = array(), $limit = NULL, $access_tag = FALSE) {
  $results =& drupal_static(__FUNCTION__, array());

  // Create unique id for static cache.
  $cid = implode(':', array(
    $field['field_name'],
    $match,
    $string !== '' ? $string : implode('-', $ids),
    $limit,
  ));
  if (!isset($results[$cid])) {

    // If the variable commerce_product_match_using_efq has been set, then
    // process the match using an EntityFieldQuery instead of db_select(). EFQ
    // was made the only matching query builder in Commerce 1.10, but this
    // introduced a scalability concern as it required the use of a multiple
    // product load to build the return value instead of directly querying the
    // title / SKU from the local database.
    // Commerce 1.12 then introduced a breaking change by attempting to resolve
    // the scalability issue by changing the options list limit from unlimited
    // to 10 by default. With no clear path to resolve all concerns, we opted to
    // make the routine alterable via configuration for Commerce 1.14.
    // The recommended method to select the EFQ based callback instead of this
    // one is to set the variable directly in the $conf array in your
    // settings.php file. Additionally, you may need to set the product
    // reference options list limit variable as explained in the comments
    // inline in commerce_product_reference_options_list().
    if (variable_get('commerce_product_match_products_efq', FALSE)) {
      $matches = _commerce_product_match_products_efq($instance, $string, $match, $ids, $limit, $access_tag);
    }
    else {
      $matches = _commerce_product_match_products_standard($instance, $string, $match, $ids, $limit, $access_tag);
    }

    // Store the results.
    $results[$cid] = !empty($matches) ? $matches : array();
  }
  return $results[$cid];
}