Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2015
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.06 KB | None | 0 0
  1. public function getSearchResultProducts(){
  2. if($this->enableProductSearch()) {
  3. #Get collection page limit
  4. $limit = $this->getLimit($this->_prodLimit);
  5. #get store ID
  6. $storeId = Mage::app()->getStore()->getId();
  7. #Get list of selected attributes against which to search.
  8. $attributes1 = Mage::getStoreConfig('supersearch/product_setting/search_attributes', $storeId);
  9. $attributes = explode(",", $attributes1);
  10. #Get collection of possible values for selct type attributes
  11. $attrs = Mage::getModel('eav/entity_attribute_option')
  12. ->getCollection()
  13. ->setStoreFilter()
  14. ->join('attribute', 'attribute.attribute_id=main_table.attribute_id', 'attribute_code')
  15. ->addFieldToFilter('attribute_code', array('in' => $attributes));
  16. $attrs->load();
  17. $options = array();
  18. #Load possible values into $options array
  19. foreach ($attrs as $attr) {
  20. array_push($options, array($attr['option_id'] => strtoupper($attr['default_value'])));
  21. }
  22. $searchOptions = array();
  23. #explode keywords on spaces in order to search each keyword against possible attribute values
  24. foreach (explode(" ", $this->_keyword) as $kw) {
  25. $opts = $options;
  26. $kw = strtoupper($kw);
  27. #Filter attribute available values by keyword part
  28. $opt = array_filter($opts, function ($el) use ($kw) {
  29.  
  30. return (strpos(implode(' ', $el), $kw) !== false);
  31. });
  32. #Add matched options to @searchOptions array
  33. foreach ($opt as $opCode) {
  34. array_push($searchOptions, array_keys($opCode));
  35. }
  36. }
  37. #Create array of filter options for OR joined WHERE clause segment
  38. $filters = $this->formatFilters($attributes, $searchOptions);
  39. #load query from default Magento search system for relevance sort
  40. $query = Mage::getModel('catalogsearch/query')->setQueryText($this->_keyword)->prepare();
  41. $productFulltext = Mage::getResourceModel('catalogsearch/fulltext')->prepareResult(
  42. Mage::getModel('catalogsearch/fulltext'),
  43. $this->_keyword,
  44. $query
  45. );
  46. #Create collection of ALL products that match the $filter array
  47. $products = Mage::getModel('catalog/product')->getCollection()
  48. ->addAttributeToSelect('*')
  49. ->addMinimalPrice()
  50. ->addFinalPrice()
  51. ->addTaxPercents()
  52. ->addStoreFilter($storeId)
  53. ->addAttributeToFilter($filters);
  54. //->setCurPage($this->_pageNum)
  55. //->setPageSize($limit);
  56. #Identify parent products of result items if they exist
  57. $ids = $this->getParents($products);
  58. #Add parent item ids to filter array.
  59. $filters[] = array('attribute' => 'entity_id', 'in' => $ids);
  60. #Reset the product collection clearing the where clause
  61. $products->clear()->getSelect()->reset('where');
  62. #Add updated $filter, set page size and page number
  63. $products->addAttributeToFilter($filters)
  64. ->setCurPage($this->_pageNum)
  65. ->setPageSize($limit);
  66. #Filter results to ensure that only visible products are returned
  67. Mage::getSingleton('catalog/product_visibility')->addVisibleInSiteFilterToCollection($products);
  68. #Join collection with relevance data collected earlier
  69. $products->getSelect()->joinLeft(
  70. array('search_result' => $products->getTable('catalogsearch/result')),
  71. $products->getConnection()->quoteInto(
  72. 'search_result.product_id=e.entity_id AND search_result.query_id=?',
  73. $query->getId()
  74. ),
  75. array('relevance' => 'relevance')
  76. );
  77. # set sort by price
  78. $products->setOrder('price', 'asc');
  79.  
  80. return $products;
  81. }
  82. return null;
  83. }
  84.  
  85. SELECT 1 AS `status`, `e`.`entity_id`, `e`.`type_id`, `e`.`attribute_set_id`, `e`.`entity_id`, `e`.`attribute_set_id`, `e`.`type_id`, `e`.`allow_open_amount`, `e`.`amazon_specialprice`, `e`.`camoflag`, `e`.`camoflag_value`, `e`.`cost`, `e`.`created_at`, `e`.`email_template`, `e`.`employee_discount`, `e`.`employee_discount_value`, `e`.`enable_googlecheckout`, `e`.`free_shipping`, `e`.`free_shipping_value`, `e`.`giftcard_amounts`, `e`.`giftcard_type`, `e`.`gift_message_available`, `e`.`gift_wrapping_available`, `e`.`gift_wrapping_price`, `e`.`has_options`, `e`.`image_label`, `e`.`is_recurring`, `e`.`is_redeemable`, `e`.`lifetime`, `e`.`links_exist`, `e`.`links_purchased_separately`, `e`.`links_title`, `e`.`magazine`, `e`.`msrp`, `e`.`msrp_display_actual_price_type`, `e`.`msrp_enabled`, `e`.`name`, `e`.`news_from_date`, `e`.`news_to_date`, `e`.`no_discount`, `e`.`no_discount_value`, `e`.`open_amount_max`, `e`.`open_amount_min`, `e`.`price`, `e`.`price_type`, `e`.`price_view`, `e`.`recurring_profile`, `e`.`required_options`, `e`.`shipment_type`, `e`.`short_description`, `e`.`sku`, `e`.`sku_type`, `e`.`small_image`, `e`.`small_image_label`, `e`.`special_from_date`, `e`.`special_price`, `e`.`special_to_date`, `e`.`tax_class_id`, `e`.`thumbnail`, `e`.`thumbnail_label`, `e`.`top_tackle_choice`, `e`.`treestand`, `e`.`updated_at`, `e`.`url_key`, `e`.`url_path`, `e`.`use_config_email_template`, `e`.`use_config_is_redeemable`, `e`.`use_config_lifetime`, `e`.`visibility`, `e`.`weight`, `e`.`weight_type`, `e`.`manufacturer`, `e`.`manufacturer_value`, `e`.`featured`, `price_index`.`price`, `price_index`.`tax_class_id`, `price_index`.`final_price`, IF(price_index.tier_price IS NOT NULL, LEAST(price_index.min_price, price_index.tier_price), price_index.min_price) AS `minimal_price`, `price_index`.`min_price`, `price_index`.`max_price`, `price_index`.`tier_price`, `cat_index`.`position` AS `cat_index_position`, `search_result`.`relevance` FROM `catalog_product_flat_11` AS `e` INNER JOIN `catalog_product_index_price` AS `price_index` ON price_index.entity_id = e.entity_id AND price_index.website_id = '11' AND price_index.customer_group_id = 0 INNER JOIN `catalog_category_product_index` AS `cat_index` ON cat_index.product_id=e.entity_id AND cat_index.store_id='11' AND cat_index.visibility IN(3, 2, 4) AND cat_index.category_id='413' LEFT JOIN `catalogsearch_result` AS `search_result` ON search_result.product_id=e.entity_id AND search_result.query_id='85353' WHERE ((`e`.`name` LIKE '%bomber%') OR (`e`.`entity_id` IN('2888', '2999', '8119', '3003', '2343', '4400', '2344', '3022', '2170', '1930', '2887', '2071', '8018', '3021', '9227', '1803', '2306', '9257', '8601', '2648', '2647', '2393', '2597', '7787', '7797', '7804', '7805', '7807', '9422', '7810', '7811', '7812', '7814', '7815', '7816', '7817', '7820', '7821', '7825', '7827', '7829', '7840', '7843', '7844', '7850', '7852', '7854', '7856', '7865', '7874', '7876', '7877', '7878', '7879', '7880', '7888', '7890', '8782', '7895', '7897', '7898', '7900', '7901', '7902', '7907', '7908', '7909', '7910', '7912', '7913', '7915', '8042', '8442', '8643', '8654', '8832', '8834', '8828', '9243', '9473', '9501', '9557'))) ORDER BY `price_index`.`min_price` asc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement