Skip to content

Commit f6f8562

Browse files
author
Silvan Laroo
committed
Add php 7 support, small fixes
1 parent 6490680 commit f6f8562

File tree

5 files changed

+66
-42
lines changed

5 files changed

+66
-42
lines changed

Model/Autocomplete/SearchDataProvider.php

Lines changed: 56 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
namespace Sebwite\SmartSearch\Model\Autocomplete;
1414

1515
use Magento\Catalog\Api\ProductRepositoryInterface;
16+
use Magento\Catalog\Helper\Image;
1617
use Magento\Framework\Api\FilterBuilder;
1718
use Magento\Framework\Api\Search\FilterGroupBuilder;
1819
use Magento\Framework\Api\Search\SearchCriteriaFactory as FullTextSearchCriteriaFactory;
@@ -25,7 +26,11 @@
2526
use Magento\Store\Model\StoreManagerInterface;
2627

2728
/**
28-
* Full text search implementation of autocomplete.
29+
* Full text search implementation of autocomplete.
30+
*
31+
* @package Sebwite\SmartSearch
32+
* @author Sebwite
33+
* @copyright Copyright (c) 2015, Sebwite. All rights reserved
2934
*/
3035
class SearchDataProvider implements DataProviderInterface
3136
{
@@ -54,18 +59,24 @@ class SearchDataProvider implements DataProviderInterface
5459

5560
/** @var SearchCriteriaBuilder */
5661
protected $searchCriteriaBuilder;
62+
5763
/**
5864
* @var StoreManagerInterface
5965
*/
60-
private $storeManager;
66+
protected $storeManager;
67+
6168
/**
6269
* @var PriceCurrencyInterface
6370
*/
64-
private $priceCurrency;
71+
protected $priceCurrency;
72+
6573
/**
6674
* @var ProductHelper
6775
*/
68-
private $productHelper;
76+
protected $productHelper;
77+
78+
/** @var \Magento\Catalog\Helper\Image */
79+
protected $imageHelper;
6980

7081
/**
7182
* Initialize dependencies.
@@ -80,6 +91,7 @@ class SearchDataProvider implements DataProviderInterface
8091
* @param SearchCriteriaBuilder $searchCriteriaBuilder
8192
* @param StoreManagerInterface $storeManager
8293
* @param \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
94+
* @param \Magento\Catalog\Helper\Image $imageHelper
8395
*/
8496
public function __construct(
8597
QueryFactory $queryFactory,
@@ -91,48 +103,54 @@ public function __construct(
91103
ProductRepositoryInterface $productRepository,
92104
SearchCriteriaBuilder $searchCriteriaBuilder,
93105
StoreManagerInterface $storeManager,
94-
PriceCurrencyInterface $priceCurrency
95-
) {
96-
$this->queryFactory = $queryFactory;
97-
$this->itemFactory = $itemFactory;
98-
$this->fullTextSearchApi = $search;
106+
PriceCurrencyInterface $priceCurrency,
107+
Image $imageHelper
108+
)
109+
{
110+
$this->queryFactory = $queryFactory;
111+
$this->itemFactory = $itemFactory;
112+
$this->fullTextSearchApi = $search;
99113
$this->fullTextSearchCriteriaFactory = $searchCriteriaFactory;
100-
$this->filterBuilder = $filterBuilder;
101-
$this->searchFilterGroupBuilder = $searchFilterGroupBuilder;
102-
$this->productRepository = $productRepository;
103-
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
104-
$this->storeManager = $storeManager;
105-
$this->priceCurrency = $priceCurrency;
114+
$this->filterBuilder = $filterBuilder;
115+
$this->searchFilterGroupBuilder = $searchFilterGroupBuilder;
116+
$this->productRepository = $productRepository;
117+
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
118+
$this->storeManager = $storeManager;
119+
$this->priceCurrency = $priceCurrency;
120+
$this->imageHelper = $imageHelper;
106121
}
107122

108123
/**
109-
* {@inheritdoc}
124+
* getItems method
125+
*
126+
* @return array
110127
*/
111128
public function getItems()
112129
{
113-
$result = [];
114-
$query = $this->queryFactory->get()->getQueryText();
130+
$result = [ ];
131+
$query = $this->queryFactory->get()->getQueryText();
115132
$productIds = $this->searchProductsFullText($query);
116133

117134
// Check if products are found
118-
if ($productIds) {
135+
if ( $productIds )
136+
{
119137
$searchCriteria = $this->searchCriteriaBuilder->addFilter('entity_id', $productIds, 'in')->create();
120-
$products = $this->productRepository->getList($searchCriteria);
121-
122-
$baseUrl = $this->storeManager->getStore()->getBaseUrl();
138+
$products = $this->productRepository->getList($searchCriteria);
123139

124-
// Loop through products
140+
foreach ( $products->getItems() as $product )
141+
{
125142

126-
foreach ($products->getItems() as $product) {
143+
$image = $this->imageHelper->init($product, 'product_page_image_small')->getUrl();
127144

128145
$resultItem = $this->itemFactory->create([
129-
'title' => $product->getName(), 'price' => $this->priceCurrency->format($product->getFinalPrice(), false),
146+
'title' => $product->getName(),
147+
'price' => $this->priceCurrency->format($product->getFinalPrice(), false),
130148
'special_price' => $this->priceCurrency->format($product->getSpecialPrice(), false),
131149
'has_special_price' => $product->getSpecialPrice() > 0 ? true : false,
132-
'image' => str_replace('index.php/', '', $baseUrl) . '/pub/media/catalog/product' . $product->getImage(),
150+
'image' => $image,
133151
'url' => $product->getProductUrl()
134152
]);
135-
$result[] = $resultItem;
153+
$result[] = $resultItem;
136154
}
137155
}
138156

@@ -142,30 +160,34 @@ public function getItems()
142160
/**
143161
* Perform full text search and find IDs of matching products.
144162
*
145-
* @param string
146-
* @return int[]
163+
* @param $query
164+
*
165+
* @return array
147166
*/
148-
private function searchProductsFullText($query)
167+
protected function searchProductsFullText($query)
149168
{
150169
$searchCriteria = $this->fullTextSearchCriteriaFactory->create();
151170

152171
/** To get list of available request names see Magento/CatalogSearch/etc/search_request.xml */
153172
$searchCriteria->setRequestName('quick_search_container');
154-
$filter = $this->filterBuilder->setField('search_term')->setValue($query)->setConditionType('like')->create();
173+
$filter = $this->filterBuilder->setField('search_term')->setValue($query)->setConditionType('like')->create();
155174
$filterGroup = $this->searchFilterGroupBuilder->addFilter($filter)->create();
156175
$currentPage = 1;
157-
$searchCriteria->setFilterGroups([$filterGroup])
176+
$searchCriteria->setFilterGroups([ $filterGroup ])
158177
->setCurrentPage($currentPage)
159178
->setPageSize(self::PRODUCTS_NUMBER_IN_SUGGEST);
160179
$searchResults = $this->fullTextSearchApi->search($searchCriteria);
161-
$productIds = [];
180+
$productIds = [ ];
181+
162182
/**
163183
* Full text search returns document IDs (in this case product IDs),
164184
* so to get products information we need to load them using filtration by these IDs
165185
*/
166-
foreach ($searchResults->getItems() as $searchDocument) {
186+
foreach ( $searchResults->getItems() as $searchDocument )
187+
{
167188
$productIds[] = $searchDocument->getId();
168189
}
190+
169191
return $productIds;
170192
}
171193
}

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
"name": "sebwite/magento2-smartsearch",
33
"description": "Magento 2.0 SmartSearch",
44
"type": "magento2-module",
5-
"version": "1.0.2",
5+
"version": "1.0.3",
66
"license": [
77
"OSL-3.0",
88
"AFL-3.0"
99
],
1010
"require": {
11-
"php": "~5.5.0|~5.6.0",
11+
"php": "~5.5.0|~7.0",
1212
"magento/magento-composer-installer": "*"
1313
},
1414
"autoload": {

etc/di.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,3 @@
1010
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
1111
<preference for="Magento\CatalogSearch\Model\Autocomplete\DataProvider" type="Sebwite\SmartSearch\Model\Autocomplete\SearchDataProvider" />
1212
</config>
13-
14-

view/frontend/layout/default.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212
</referenceBlock>
1313
</referenceContainer>
1414
</body>
15-
</page>
15+
</page>

view/frontend/web/form-mini.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,9 +252,13 @@ define([
252252

253253
if (value.length >= parseInt(this.options.minSearchLength, 10)) {
254254

255-
$('#search').addClass('loading');
255+
$('#search_mini_form').addClass('is-loading');
256256

257-
$.get(this.options.url, {q: value}, $.proxy(function (data) {
257+
if(this.request) {
258+
this.request.abort();
259+
}
260+
261+
this.request = $.get(this.options.url, {q: value}, $.proxy(function (data) {
258262

259263
// Check if SmartSearch returned results
260264
if( ! data.length)
@@ -274,7 +278,7 @@ define([
274278
.show()
275279
.find(this.options.responseFieldElements + ':visible');
276280

277-
$('#search').removeClass('loading');
281+
$('#search_mini_form').removeClass('is-loading');
278282

279283
this._resetResponseList(false);
280284
this.element.removeAttr('aria-activedescendant');

0 commit comments

Comments
 (0)