You are here

function commerce_product_query_commerce_sku_or_title_match_alter in Commerce Core 7

Implements hook_query_TAG_alter.

EntityFieldQuery used in _commerce_product_match_products_standard() does not allow OR clauses. Alter the SQL query to string match on sku OR title.

Parameters

QueryAlterableInterface $query:

File

modules/product/commerce_product.module, line 1044
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_query_commerce_sku_or_title_match_alter(QueryAlterableInterface $query) {
  $string = $query->alterMetaData['commerce_sku_or_title_match']->string;
  $match = $query->alterMetaData['commerce_sku_or_title_match']->match;
  if (isset($string, $match)) {

    // Build a where clause matching on either the SKU or title.
    switch ($match) {
      case 'contains':
        $or = db_or()
          ->condition('sku', '%' . $string . '%', 'LIKE')
          ->condition('title', '%' . $string . '%', 'LIKE');
        break;
      case 'starts_with':
        $or = db_or()
          ->condition('sku', $string . '%', 'LIKE')
          ->condition('title', $string . '%', 'LIKE');
        break;
      case 'equals':
      default:
        $or = db_or()
          ->condition('sku', $string, '=')
          ->condition('title', $string, '=');
        break;
    }
    $query
      ->condition($or);
  }
}