_commerce_product_match_products_standard

  1. commerce
    1. 7
Versions
7 _commerce_product_match_products_standard($instance, $string = '', $match = 'contains', $ids = array(), $limit = NULL, $access_tag = FALSE)

Helper function for commerce_product_match_products().

Returns an array of products matching the specific parameters.

Code

modules/product/commerce_product.module, line 808

<?php
function _commerce_product_match_products_standard($instance, $string = '', $match = 'contains', $ids = array(), $limit = NULL, $access_tag = FALSE) {
  // Build the query object with the necessary fields.
  $query = db_select('commerce_product', 'cp');

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

  $product_id_alias = $query->addField('cp', 'product_id');
  $product_sku_alias = $query->addField('cp', 'sku');
  $product_title_alias = $query->addField('cp', 'title');
  $product_type_alias = $query->addField('cp', 'type');

  // 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->condition('cp.type', $types, 'IN');
    }
  }

  if ($string !== '') {
    $args = array();

    // Build a where clause matching on either the SKU or title.
    switch ($match) {
      case 'contains':
        $where = '(cp.sku LIKE :sku_match OR cp.title LIKE :title_match)';
        $args['sku_match'] = '%' . $string . '%';
        $args['title_match'] = '%' . $string . '%';
        break;

      case 'starts_with':
        $where = '(cp.sku LIKE :sku_match OR cp.title LIKE :title_match)';
        $args['sku_match'] = $string . '%';
        $args['title_match'] = $string . '%';
        break;

      case 'equals':
      default:
        $where = '(cp.sku = :sku_match OR cp.title = :title_match)';
        $args['sku_match'] = $string;
        $args['title_match'] = $string;
        break;
    }

    $query->where($where, $args);
  }
  elseif ($ids) {
    // Otherwise add a product_id specific condition if specified.
    $query->condition($product_id_alias, $ids, 'IN', $ids);
  }

  // Order the results by SKU, title, and then product type.
  $query
    ->orderBy($product_sku_alias)
    ->orderBy($product_title_alias)
    ->orderBy($product_type_alias);

  // Add a limit if specified.
  if ($limit) {
    $query->range(0, $limit);
  }

  // Execute the query and build the results array.
  $result = $query->execute();

  $matches = array();

  foreach ($result->fetchAll() 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;
}
?>