-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Amasty\Mage245Fix\Model\ResourceModel\Fulltext\Collection\SearchResultApplier; | ||
|
||
use Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection\SearchResultApplierInterface; | ||
use Magento\Framework\Api\Search\SearchResultInterface; | ||
use Magento\Framework\Data\Collection; | ||
|
||
/** | ||
* Correction of strong typing and table prefixes. Our categorical filter can be a multiselect, and therefore we | ||
* always set the array to the filter category, and magneto in this class in the private method | ||
* categoryProductByCustomSortOrder strongly types the value from the filter as int, which causes an error. | ||
* Also fixing an error with table prefixes, in the categoryProductByCustomSortOrder method, | ||
* when sorting by name and price, table prefixes do not resolve | ||
*/ | ||
class Mage245Fix implements SearchResultApplierInterface | ||
This comment has been minimized.
Sorry, something went wrong. |
||
{ | ||
/** | ||
* @var Collection|\Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection | ||
*/ | ||
private $collection; | ||
This comment has been minimized.
Sorry, something went wrong. |
||
|
||
/** | ||
* @var SearchResultInterface | ||
*/ | ||
private $searchResult; | ||
This comment has been minimized.
Sorry, something went wrong. |
||
|
||
/** | ||
* @var int | ||
*/ | ||
private $size; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $currentPage; | ||
|
||
public function __construct( | ||
Collection $collection, | ||
SearchResultInterface $searchResult, | ||
int $size, | ||
int $currentPage | ||
) { | ||
$this->collection = $collection; | ||
$this->searchResult = $searchResult; | ||
$this->size = $size; | ||
$this->currentPage = $currentPage; | ||
} | ||
|
||
public function apply(): void | ||
{ | ||
if (empty($this->searchResult->getItems())) { | ||
$this->collection->getSelect()->where('NULL'); | ||
return; | ||
} | ||
|
||
$ids = []; | ||
$items = $this->sliceItems($this->searchResult->getItems(), $this->size, $this->currentPage); | ||
foreach ($items as $item) { | ||
$ids[] = (int)$item->getId(); | ||
} | ||
|
||
$orderList = implode(',', $ids); | ||
$this->collection->getSelect() | ||
->where('e.entity_id IN (?)', $ids) | ||
->reset(\Magento\Framework\DB\Select::ORDER) | ||
->order(new \Zend_Db_Expr("FIELD(e.entity_id,$orderList)")); | ||
} | ||
|
||
/** | ||
* Slice current items | ||
*/ | ||
private function sliceItems(array $items, int $size, int $currentPage): array | ||
{ | ||
if ($size !== 0) { | ||
// Check that current page is in a range of allowed page numbers, based on items count and items per page, | ||
// than calculate offset for slicing items array. | ||
$itemsCount = count($items); | ||
$maxAllowedPageNumber = (int)ceil($itemsCount/$size); | ||
if ($currentPage < 1) { | ||
$currentPage = 1; | ||
} | ||
if ($currentPage > $maxAllowedPageNumber) { | ||
$currentPage = $maxAllowedPageNumber; | ||
} | ||
|
||
$offset = $this->getOffset($currentPage, $size); | ||
$items = array_slice($items, $offset, $size); | ||
} | ||
|
||
return $items; | ||
} | ||
|
||
/** | ||
* Get offset for given page. | ||
*/ | ||
private function getOffset(int $pageNumber, int $pageSize): int | ||
{ | ||
return ($pageNumber - 1) * $pageSize; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# module-mage-2.4.5-fix | ||
This comment has been minimized.
Sorry, something went wrong.
edspc
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"name": "amasty/module-mage-2.4.5-fix", | ||
"description": "Fix Product Collection Changes issue in Magento 2.4.5 by Amasty", | ||
"require": { | ||
"php": ">=7.3", | ||
"magento/framework": ">=103.0.5", | ||
"magento/module-catalog-inventory": ">=100.4.5", | ||
"amasty/base": ">=1.14.2" | ||
This comment has been minimized.
Sorry, something went wrong.
edspc
|
||
}, | ||
"homepage": "https://amasty.com/", | ||
"type": "magento2-module", | ||
"version": "1.0.2", | ||
"license": [ | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
"Commercial" | ||
], | ||
"autoload": { | ||
"files": [ | ||
"registration.php" | ||
], | ||
"psr-4": { | ||
"Amasty\\Mage245Fix\\": "" | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?xml version="1.0"?> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> | ||
<preference for="Magento\Elasticsearch\Model\ResourceModel\Fulltext\Collection\SearchResultApplier" | ||
type="Amasty\Mage245Fix\Model\ResourceModel\Fulltext\Collection\SearchResultApplier\Mage245Fix" /> | ||
|
||
<type name="Magento\Catalog\Block\Product\ProductList\Toolbar"> | ||
<plugin name="update_toolbar_count" disabled="true"/> | ||
</type> | ||
</config> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?xml version="1.0"?> | ||
|
||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> | ||
<module name="Amasty_Mage245Fix"> | ||
<sequence> | ||
<module name="Magento_Catalog"/> | ||
</sequence> | ||
</module> | ||
</config> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
\Magento\Framework\Component\ComponentRegistrar::register( | ||
\Magento\Framework\Component\ComponentRegistrar::MODULE, | ||
'Amasty_Mage245Fix', | ||
__DIR__ | ||
); |
It's better to specify what precisely the class fixes.
Or because it's not a plugin, we can use the original class name
SearchResultApplier
.