Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
alexey-motorny-amasty committed Nov 18, 2022
0 parents commit 7ce5576
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 0 deletions.
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.

Copy link
@edspc

edspc Nov 18, 2022

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.

{
/**
* @var Collection|\Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection
*/
private $collection;

This comment has been minimized.

Copy link
@edspc

edspc Nov 18, 2022

private Collection $collection


/**
* @var SearchResultInterface
*/
private $searchResult;

This comment has been minimized.

Copy link
@edspc

edspc Nov 18, 2022

private SearchResultInterface $searchResult


/**
* @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;
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# module-mage-2.4.5-fix

This comment has been minimized.

Copy link
@edspc

edspc Nov 18, 2022

It needs some description.
Maybe we can use the comment from here

24 changes: 24 additions & 0 deletions composer.json
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.

Copy link
@edspc

edspc Nov 18, 2022

Remove amasty/base dependency.
Ask CI not to add it via adding the IGNORE_BASE_REQUIRE variable with any value.

This comment has been minimized.

Copy link
@alexey-motorny-amasty

alexey-motorny-amasty Nov 25, 2022

Author Collaborator

resolve

},
"homepage": "https://amasty.com/",
"type": "magento2-module",
"version": "1.0.2",
"license": [

This comment has been minimized.

Copy link
@edspc

edspc Nov 18, 2022

Should be
"license": "proprietary",

This comment has been minimized.

Copy link
@alexey-motorny-amasty

alexey-motorny-amasty Nov 25, 2022

Author Collaborator

resolve

"Commercial"
],
"autoload": {
"files": [
"registration.php"
],
"psr-4": {
"Amasty\\Mage245Fix\\": ""
}
}
}
10 changes: 10 additions & 0 deletions etc/di.xml
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>
10 changes: 10 additions & 0 deletions etc/module.xml
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>
7 changes: 7 additions & 0 deletions registration.php
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__
);

0 comments on commit 7ce5576

Please sign in to comment.