Skip to content

Commit

Permalink
Merge pull request #168 from Nosto/hotfix/compatibility-mage-246
Browse files Browse the repository at this point in the history
Force use Nosto sorting
  • Loading branch information
supercid authored Apr 11, 2023
2 parents 35d2609 + 345de4b commit 7aa93e5
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
90 changes: 90 additions & 0 deletions Plugin/Api/Search/SearchResultApplier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php
/**
* Copyright (c) 2020, Nosto Solutions Ltd
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @author Nosto Solutions Ltd <contact@nosto.com>
* @copyright 2020 Nosto Solutions Ltd
* @license http://opensource.org/licenses/BSD-3-Clause BSD 3-Clause
*
*/

namespace Nosto\Cmp\Plugin\Api\Search;

use Magento\Framework\DB\Select;
use Nosto\Cmp\Model\Service\Merchandise\LastResult;
use Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection;
use Magento\Elasticsearch\Model\ResourceModel\Fulltext\Collection\SearchResultApplier as ESearchResultApplier;
use Zend_Db_Expr;

class SearchResultApplier
{

/** @var Collection */
private Collection $collection;

/** @var LastResult */
private LastResult $lastResult;

/**
* @param LastResult $lastResult
* @param Collection $collection
*/
public function __construct(
LastResult $lastResult,
Collection $collection
) {
$this->lastResult = $lastResult;
$this->collection = $collection;
}

/**
* @param ESearchResultApplier $subject
* @param $result
* @return mixed
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
* @noinspection PhpUnused
* @noinspection PhpUnusedParameterInspection
*/
public function afterApply(// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter
ESearchResultApplier $subject,
$result
) {
$cmResult = $this->lastResult->getLastResult();
if ($cmResult !== null) {
$productIds = $cmResult->parseProductIds();
$orderList = implode(',', $productIds);
$this->collection->getSelect()
->where('e.entity_id IN (?)', $productIds)
->reset(Select::ORDER)
->order(new Zend_Db_Expr("FIELD(e.entity_id,$orderList)"));

}
return $result;
}
}
4 changes: 4 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@
<type name="Magento\Framework\Api\Search\SearchResult">
<plugin name="nosto_search_result_sort" type="Nosto\Cmp\Plugin\Api\Search\SearchResultSorter"/>
</type>
<type name="Magento\Elasticsearch\Model\ResourceModel\Fulltext\Collection\SearchResultApplier">
<plugin name="nosto_search_result_applier" type="Nosto\Cmp\Plugin\Api\Search\SearchResultApplier" sortOrder="1" disabled="false" />
</type>

<type name="Magento\Catalog\Block\Product\ListProduct">
<plugin name="nosto_search_result_sort" type="Nosto\Cmp\Plugin\Catalog\Block\ListProduct"/>
</type>
Expand Down

0 comments on commit 7aa93e5

Please sign in to comment.