Révision dbb0c974
Ajouté par Assos Assos il y a plus de 9 ans
drupal7/sites/all/modules/commerce/modules/product/commerce_product.module | ||
---|---|---|
832 | 832 |
* Returns an array of products matching the specific parameters. |
833 | 833 |
*/ |
834 | 834 |
function _commerce_product_match_products_standard($instance, $string = '', $match = 'contains', $ids = array(), $limit = NULL, $access_tag = FALSE) { |
835 |
// Build the query object with the necessary fields. |
|
836 |
$query = db_select('commerce_product', 'cp'); |
|
835 |
$query = new EntityFieldQuery; |
|
836 |
|
|
837 |
$query->entityCondition('entity_type', 'commerce_product'); |
|
837 | 838 |
|
838 | 839 |
// Add the access control tag if specified. |
839 | 840 |
if ($access_tag) { |
... | ... | |
843 | 844 |
// Add a global query tag so anyone can alter this query. |
844 | 845 |
$query->addTag('commerce_product_match'); |
845 | 846 |
|
846 |
$product_id_alias = $query->addField('cp', 'product_id'); |
|
847 |
$product_sku_alias = $query->addField('cp', 'sku'); |
|
848 |
$product_title_alias = $query->addField('cp', 'title'); |
|
849 |
$product_type_alias = $query->addField('cp', 'type'); |
|
850 |
|
|
851 | 847 |
// Add a condition to the query to filter by matching product types. |
852 | 848 |
if (!empty($instance['settings']['referenceable_types'])) { |
853 | 849 |
$types = array_diff(array_values($instance['settings']['referenceable_types']), array(0, NULL)); |
854 | 850 |
|
855 | 851 |
// Only filter by type if some types have been specified. |
856 | 852 |
if (!empty($types)) { |
857 |
$query->condition('cp.type', $types, 'IN');
|
|
853 |
$query->propertyCondition('type', $types, 'IN');
|
|
858 | 854 |
} |
859 | 855 |
} |
860 | 856 |
|
861 | 857 |
if ($string !== '') { |
862 |
$args = array(); |
|
863 |
|
|
864 |
// Build a where clause matching on either the SKU or title. |
|
865 |
switch ($match) { |
|
866 |
case 'contains': |
|
867 |
$where = '(cp.sku LIKE :sku_match OR cp.title LIKE :title_match)'; |
|
868 |
$args['sku_match'] = '%' . $string . '%'; |
|
869 |
$args['title_match'] = '%' . $string . '%'; |
|
870 |
break; |
|
871 |
|
|
872 |
case 'starts_with': |
|
873 |
$where = '(cp.sku LIKE :sku_match OR cp.title LIKE :title_match)'; |
|
874 |
$args['sku_match'] = $string . '%'; |
|
875 |
$args['title_match'] = $string . '%'; |
|
876 |
break; |
|
858 |
// EntityFieldQuery cannot do OR clauses, so we use hook_query_TAG_alter. |
|
859 |
$query->addTag('commerce_sku_or_title_match'); |
|
877 | 860 |
|
878 |
case 'equals':
|
|
879 |
default:
|
|
880 |
$where = '(cp.sku = :sku_match OR cp.title = :title_match)';
|
|
881 |
$args['sku_match'] = $string;
|
|
882 |
$args['title_match'] = $string;
|
|
883 |
break;
|
|
884 |
}
|
|
861 |
$sku_title_meta = new stdClass();
|
|
862 |
$sku_title_meta->properties = array(
|
|
863 |
'sku',
|
|
864 |
'title',
|
|
865 |
);
|
|
866 |
$sku_title_meta->string = $string;
|
|
867 |
$sku_title_meta->match = $match;
|
|
885 | 868 |
|
886 |
$query->where($where, $args);
|
|
869 |
$query->addMetaData('commerce_sku_or_title_match', $sku_title_meta);
|
|
887 | 870 |
} |
888 | 871 |
elseif ($ids) { |
889 | 872 |
// Otherwise add a product_id specific condition if specified. |
890 |
$query->condition($product_id_alias, $ids, 'IN', $ids);
|
|
873 |
$query->propertyCondition('product_id', $ids, 'IN');
|
|
891 | 874 |
} |
892 | 875 |
|
893 | 876 |
// Order the results by SKU, title, and then product type. |
894 | 877 |
$query |
895 |
->orderBy($product_sku_alias)
|
|
896 |
->orderBy($product_title_alias)
|
|
897 |
->orderBy($product_type_alias);
|
|
878 |
->propertyOrderBy('sku')
|
|
879 |
->propertyOrderBy('title')
|
|
880 |
->propertyOrderBy('type');
|
|
898 | 881 |
|
899 | 882 |
// Add a limit if specified. |
900 | 883 |
if ($limit) { |
901 | 884 |
$query->range(0, $limit); |
902 | 885 |
} |
903 | 886 |
|
904 |
// Execute the query and build the results array. |
|
905 |
$result = $query->execute(); |
|
887 |
$entities = $query->execute(); |
|
906 | 888 |
|
907 | 889 |
$matches = array(); |
908 | 890 |
|
909 |
foreach ($result->fetchAll() as $product) { |
|
910 |
$matches[$product->product_id] = array( |
|
911 |
'sku' => $product->sku, |
|
912 |
'type' => $product->type, |
|
913 |
'title' => $product->title, |
|
914 |
'rendered' => t('@sku: @title', array('@sku' => $product->sku, '@title' => $product->title)), |
|
915 |
); |
|
891 |
if (isset($entities['commerce_product'])) { |
|
892 |
$pids = array_keys($entities['commerce_product']); |
|
893 |
|
|
894 |
// EntityFieldQuery doesn't return sku and title, so we have to load again. |
|
895 |
$products = commerce_product_load_multiple($pids); |
|
896 |
foreach ($products AS $product) { |
|
897 |
$matches[$product->product_id] = array( |
|
898 |
'sku' => $product->sku, |
|
899 |
'type' => $product->type, |
|
900 |
'title' => $product->title, |
|
901 |
'rendered' => t('@sku: @title', array('@sku' => $product->sku, '@title' => $product->title)), |
|
902 |
); |
|
903 |
} |
|
916 | 904 |
} |
917 | 905 |
|
918 | 906 |
return $matches; |
919 | 907 |
} |
920 | 908 |
|
909 |
/** |
|
910 |
* Implements hook_query_TAG_alter. |
|
911 |
* |
|
912 |
* EntityFieldQuery used in _commerce_product_match_products_standard() does not |
|
913 |
* allow OR clauses. Alter the SQL query to string match on sku OR title. |
|
914 |
* |
|
915 |
* @param QueryAlterableInterface $query |
|
916 |
*/ |
|
917 |
function commerce_product_query_commerce_sku_or_title_match_alter(QueryAlterableInterface $query) { |
|
918 |
$string = $query->alterMetaData['commerce_sku_or_title_match']->string; |
|
919 |
$match = $query->alterMetaData['commerce_sku_or_title_match']->match; |
|
920 |
|
|
921 |
if (isset($string, $match)) { |
|
922 |
// Build a where clause matching on either the SKU or title. |
|
923 |
switch ($match) { |
|
924 |
case 'contains': |
|
925 |
$or = db_or()->condition('sku', '%' . $string . '%', 'LIKE') |
|
926 |
->condition('title', '%' . $string . '%', 'LIKE'); |
|
927 |
break; |
|
928 |
|
|
929 |
case 'starts_with': |
|
930 |
$or = db_or()->condition('sku', $string . '%', 'LIKE') |
|
931 |
->condition('title', $string . '%', 'LIKE'); |
|
932 |
break; |
|
933 |
|
|
934 |
case 'equals': |
|
935 |
default: |
|
936 |
$or = db_or()->condition('sku', $string, '=') |
|
937 |
->condition('title', $string, '='); |
|
938 |
break; |
|
939 |
} |
|
940 |
|
|
941 |
$query->condition($or); |
|
942 |
} |
|
943 |
} |
|
944 |
|
|
921 | 945 |
/** |
922 | 946 |
* Access callback: determines access to a product's translation tab. |
923 | 947 |
*/ |
Formats disponibles : Unified diff
Weekly update of contrib modules