Commit 7d450a17 by dhn

sku排序

parent 8d3f4bcb
...@@ -450,6 +450,11 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection ...@@ -450,6 +450,11 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection
*/ */
protected function _renderFiltersBefore() protected function _renderFiltersBefore()
{ {
if(isset($_REQUEST['skus']) && !empty($_REQUEST['skus']))
{
$this->_renderFiltersSku();
return;
}
if ($this->isLoaded()) { if ($this->isLoaded()) {
return; return;
} }
...@@ -477,6 +482,35 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection ...@@ -477,6 +482,35 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection
$this->getSearchResultApplier($this->searchResult)->apply(); $this->getSearchResultApplier($this->searchResult)->apply();
parent::_renderFiltersBefore(); parent::_renderFiltersBefore();
} }
protected function _renderFiltersSku()
{
if ($this->isLoaded()) {
return;
}
if ($this->searchRequestName !== 'quick_search_container'
|| strlen(trim($this->queryText))
) {
$this->prepareSearchTermFilter();
$this->preparePriceAggregation();
$searchCriteria = $this->getSearchCriteriaResolver()->resolve();
try {
$this->searchResult = $this->getSearch()->searchSku($searchCriteria);
$this->_totalRecords = $this->getTotalRecordsResolver($this->searchResult)->resolve();
} catch (EmptyRequestDataException $e) {
$this->searchResult = $this->createEmptyResult();
} catch (NonExistingRequestNameException $e) {
$this->_logger->error($e->getMessage());
throw new LocalizedException(__('An error occurred. For details, see the error log.'));
}
} else {
$this->searchResult = $this->createEmptyResult();
}
$this->getSearchResultApplier($this->searchResult)->applySku();
parent::_renderFiltersBefore();
}
/** /**
* Create empty search result * Create empty search result
......
...@@ -63,6 +63,7 @@ class SearchResultApplier implements SearchResultApplierInterface ...@@ -63,6 +63,7 @@ class SearchResultApplier implements SearchResultApplierInterface
return; return;
} }
$items = $this->sliceItems($this->searchResult->getItems(), $this->size, $this->currentPage); $items = $this->sliceItems($this->searchResult->getItems(), $this->size, $this->currentPage);
$ids = []; $ids = [];
foreach ($items as $item) { foreach ($items as $item) {
...@@ -73,60 +74,55 @@ class SearchResultApplier implements SearchResultApplierInterface ...@@ -73,60 +74,55 @@ class SearchResultApplier implements SearchResultApplierInterface
->reset(\Magento\Framework\DB\Select::ORDER); ->reset(\Magento\Framework\DB\Select::ORDER);
$sortOrder = $this->searchResult->getSearchCriteria() $sortOrder = $this->searchResult->getSearchCriteria()
->getSortOrders(); ->getSortOrders();
if (!empty($sortOrder['price']) && $this->collection->getLimitationFilters()->isUsingPriceIndex()) {
$sortDirection = $sortOrder['price'];
$this->collection->getSelect()
->order(
new \Zend_Db_Expr("price_index.min_price = 0, price_index.min_price {$sortDirection}")
);
} else {
$orderList = join(',', $ids);
$this->collection->getSelect()
->order(new \Zend_Db_Expr("FIELD(e.entity_id,$orderList)"));
}
}
public function applySku()
{
if (empty($this->searchResult->getItems())) {
$this->collection->getSelect()->where('NULL');
return;
}
$items = $this->searchResult->getItems();
$ids = [];
foreach ($items as $item) {
$ids[] = (int)$item->getId();
}
$this->collection->getSelect()
->where('e.entity_id IN (?)', $ids)
->reset(\Magento\Framework\DB\Select::ORDER);
$sortOrder = $this->searchResult->getSearchCriteria()
->getSortOrders();
$skus = []; $skus = [];
if(isset($_GET['skus']) && !empty($_GET['skus'])) $skus = explode(',', $_REQUEST['skus']);
foreach($skus as $sku)
{ {
if(!isset($_GET['p']) || $_GET['p'] == 1) $this->collection->getSelect()
{ ->order(new \Zend_Db_Expr('e.sku = "'.$sku.'" desc'));
$skus = explode(',', $_GET['skus']);
$this->collection->getSelect()->orWhere("e.sku in ('" . implode("','",$skus)."')");
}
} }
if (!empty($sortOrder['price']) && $this->collection->getLimitationFilters()->isUsingPriceIndex()) { if (!empty($sortOrder['price']) && $this->collection->getLimitationFilters()->isUsingPriceIndex()) {
$sortDirection = $sortOrder['price']; $sortDirection = $sortOrder['price'];
if(empty($skus)) $this->collection->getSelect()
{ ->order(
$this->collection->getSelect() new \Zend_Db_Expr("price_index.min_price = 0, price_index.min_price {$sortDirection}")
->order( );
new \Zend_Db_Expr("price_index.min_price = 0, price_index.min_price {$sortDirection}")
);
}
else
{
foreach($skus as $sku)
{
$this->collection->getSelect()
->order(new \Zend_Db_Expr('e.sku = "'.$sku.'" desc'));
}
$this->collection->getSelect()
->order(
new \Zend_Db_Expr("price_index.min_price = 0, price_index.min_price {$sortDirection}")
);
}
} else { } else {
$orderList = join(',', $ids); $orderList = join(',', $ids);
if(empty($skus)) $this->collection->getSelect()->order(new \Zend_Db_Expr("FIELD(e.entity_id,$orderList)"));
{
$this->collection->getSelect()
->order(new \Zend_Db_Expr("FIELD(e.entity_id,$orderList)"));
}
else
{
foreach($skus as $sku)
{
$this->collection->getSelect()
->order(new \Zend_Db_Expr('e.sku = "'.$sku.'" desc'));
}
$this->collection->getSelect()->order(new \Zend_Db_Expr("FIELD(e.entity_id,$orderList)"));
}
} }
$this->collection->getSelect()->limit($this->size); $this->collection->getSelect()->limit($this->size);
} }
/** /**
* Slice current items * Slice current items
* *
......
...@@ -83,7 +83,36 @@ class Search implements SearchInterface ...@@ -83,7 +83,36 @@ class Search implements SearchInterface
} }
$request = $this->requestBuilder->create(); $request = $this->requestBuilder->create();
$searchResponse = $this->searchEngine->search($request); $searchResponse = $this->searchEngine->search($request);
return $this->searchResponseBuilder->build($searchResponse)
->setSearchCriteria($searchCriteria);
}
public function searchSku(SearchCriteriaInterface $searchCriteria)
{
$this->requestBuilder->setRequestName($searchCriteria->getRequestName());
$scope = $this->scopeResolver->getScope()->getId();
$this->requestBuilder->bindDimension('scope', $scope);
foreach ($searchCriteria->getFilterGroups() as $filterGroup) {
foreach ($filterGroup->getFilters() as $filter) {
$this->addFieldToFilter($filter->getField(), $filter->getValue());
}
}
// $this->requestBuilder->setFrom($searchCriteria->getCurrentPage() * $searchCriteria->getPageSize());
// $this->requestBuilder->setSize($searchCriteria->getPageSize());
/**
* This added in Backward compatibility purposes.
* Temporary solution for an existing API of a fulltext search request builder.
* It must be moved to different API.
* Scope to split Search request builder API in MC-16461.
*/
if (method_exists($this->requestBuilder, 'setSort')) {
$this->requestBuilder->setSort($searchCriteria->getSortOrders());
}
$request = $this->requestBuilder->create();
$searchResponse = $this->searchEngine->search($request);
return $this->searchResponseBuilder->build($searchResponse) return $this->searchResponseBuilder->build($searchResponse)
->setSearchCriteria($searchCriteria); ->setSearchCriteria($searchCriteria);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment