commerce_product_autocomplete

  1. commerce
    1. 7
Versions
7 commerce_product_autocomplete($entity_type, $field_name, $bundle, $string = '')

Returns output for product autocompletes.

The values returned will be keyed by SKU and appear as such in the textfield, even though the preview in the autocomplete list shows "SKU: Title".

Code

modules/product/commerce_product.module, line 715

<?php
function commerce_product_autocomplete($entity_type, $field_name, $bundle, $string = '') {
  $field = field_info_field($field_name);
  $instance = field_info_instance($entity_type, $field_name, $bundle);

  $matches = array();

  // The user enters a comma-separated list of tags. We only autocomplete the last tag.
  $tags_typed = drupal_explode_tags($string);
  $tag_last = drupal_strtolower(array_pop($tags_typed));

  if (!empty($tag_last)) {
    $prefix = count($tags_typed) ? implode(', ', $tags_typed) . ', ' : '';

    // Determine the type of autocomplete match to use when searching for products.
    $match = isset($field['widget']['autocomplete_match']) ? $field['widget']['autocomplete_match'] : 'contains';

    // Get an array of matching products.
    $products = commerce_product_match_products($field, $instance, $tag_last, $match, array(), 10);

    // Loop through the products and convert them into autocomplete output.
    foreach ($products as $product_id => $data) {
      // Add a class wrapper for a few required CSS overrides.
      $matches[$prefix . $data['sku']] = '<div class="reference-autocomplete">' . $data['rendered'] . '</div>';
    }
  }

  drupal_json_output($matches);
}
?>