Skip to content

Commit c76ec03

Browse files
committed
Fix default sort order option
1 parent ba773c6 commit c76ec03

File tree

4 files changed

+98
-33
lines changed

4 files changed

+98
-33
lines changed

Plugin/ApplySortOrder.php

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@
88
namespace Opengento\CatalogSortOrder\Plugin;
99

1010
use Magento\Catalog\Block\Product\ProductList\Toolbar;
11-
use Magento\Catalog\Model\Config;
12-
use Magento\Eav\Model\Entity\Attribute;
1311
use Opengento\CatalogSortOrder\Model\Config\SortOrder as SortOrderConfig;
1412
use Opengento\CatalogSortOrder\Model\SortOrder\Direction;
15-
use Opengento\CatalogSortOrder\Model\SortOrder\Option;
13+
use Opengento\CatalogSortOrder\Provider\SortOrders;
1614

1715
use function array_intersect_key;
1816
use function array_pad;
@@ -22,13 +20,12 @@ class ApplySortOrder
2220
{
2321
public function __construct(
2422
private SortOrderConfig $sortOrderConfig,
25-
private Config $catalogConfig
23+
private SortOrders $sortOrders
2624
) {}
2725

2826
public function beforeGetAvailableOrders(Toolbar $subject): array
2927
{
3028
if ($this->sortOrderConfig->isEnabled() && !$subject->hasData('_grid_advanced_available_orders_isset')) {
31-
$subject->setData('_grid_advanced_available_orders_init', true);
3229
$subject->setAvailableOrders([]);
3330
}
3431

@@ -38,9 +35,9 @@ public function beforeGetAvailableOrders(Toolbar $subject): array
3835
public function beforeSetAvailableOrders(Toolbar $subject, array $orders): array
3936
{
4037
if ($this->sortOrderConfig->isEnabled()) {
41-
$orders = $this->sortOrderConfig->isCategoryOrdersOverridden() || $subject->hasData('_grid_advanced_available_orders_init')
42-
? $this->createSortByOptions()
43-
: array_intersect_key($this->createSortByOptions(), $orders);
38+
$orders = $this->sortOrderConfig->isCategoryOrdersOverridden() || $orders !== []
39+
? $this->sortOrders->getOptions()
40+
: array_intersect_key($this->sortOrders->getOptions(), $orders);
4441
$subject->setData('_grid_advanced_available_orders_isset', true);
4542
}
4643

@@ -71,29 +68,4 @@ public function afterGetCurrentOrder(Toolbar $subject, string $result): string
7168

7269
return $result;
7370
}
74-
75-
private function createSortByOptions(): array
76-
{
77-
$attributesForSortBy = $this->catalogConfig->getAttributesUsedForSortBy();
78-
$availableOrders = [];
79-
foreach ($this->sortOrderConfig->getSortOptions() as $option) {
80-
$attribute = $attributesForSortBy[$option->code] ?? null;
81-
if ($option->includeDirection) {
82-
foreach (Direction::cases() as $direction) {
83-
$availableOrders[$direction->appendValue($option->code)] = $direction->prependLabel(
84-
$this->resolveLabel($attribute, $option)
85-
);
86-
}
87-
} else {
88-
$availableOrders[$option->code] = $this->resolveLabel($attribute, $option);
89-
}
90-
}
91-
92-
return $availableOrders;
93-
}
94-
95-
private function resolveLabel(?Attribute $attribute, Option $option): string
96-
{
97-
return $option->label ?: $attribute?->getStoreLabel() ?? $option->code;
98-
}
9971
}

Plugin/ResolveDefaultSort.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/**
3+
* Copyright © OpenGento, All rights reserved.
4+
* See LICENSE bundled with this library for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Opengento\CatalogSortOrder\Plugin;
9+
10+
use Magento\Catalog\Helper\Product\ProductList;
11+
use Opengento\CatalogSortOrder\Model\Config\SortOrder as SortOrderConfig;
12+
use Opengento\CatalogSortOrder\Model\SortOrder\Direction;
13+
use Opengento\CatalogSortOrder\Provider\SortOrders;
14+
15+
class ResolveDefaultSort
16+
{
17+
public function __construct(
18+
private SortOrders $sortOrders,
19+
private SortOrderConfig $sortOrderConfig
20+
) {}
21+
22+
public function afterGetDefaultSortField(ProductList $subject, ?string $result): ?string
23+
{
24+
if ($result && $this->sortOrderConfig->isEnabled()) {
25+
$options = $this->sortOrders->getOptions();
26+
foreach (Direction::cases() as $direction) {
27+
$sort = $direction->appendValue($result);
28+
if (isset($options[$sort])) {
29+
return $sort;
30+
}
31+
}
32+
}
33+
34+
return $result;
35+
}
36+
}

Provider/SortOrders.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* Copyright © OpenGento, All rights reserved.
4+
* See LICENSE bundled with this library for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Opengento\CatalogSortOrder\Provider;
9+
10+
use Magento\Catalog\Model\Config;
11+
use Magento\Eav\Model\Entity\Attribute;
12+
use Opengento\CatalogSortOrder\Model\Config\SortOrder as SortOrderConfig;
13+
use Opengento\CatalogSortOrder\Model\SortOrder\Direction;
14+
use Opengento\CatalogSortOrder\Model\SortOrder\Option;
15+
16+
class SortOrders
17+
{
18+
private ?array $options = null;
19+
20+
public function __construct(
21+
private SortOrderConfig $sortOrderConfig,
22+
private Config $catalogConfig
23+
) {}
24+
25+
public function getOptions(): array
26+
{
27+
return $this->options ??= $this->createOptions();
28+
}
29+
30+
private function createOptions(): array
31+
{
32+
$attributesForSortBy = $this->catalogConfig->getAttributesUsedForSortBy();
33+
$availableOrders = [];
34+
foreach ($this->sortOrderConfig->getSortOptions() as $option) {
35+
$attribute = $attributesForSortBy[$option->code] ?? null;
36+
if ($option->includeDirection) {
37+
foreach (Direction::cases() as $direction) {
38+
$availableOrders[$direction->appendValue($option->code)] = $direction->prependLabel(
39+
$this->resolveLabel($attribute, $option)
40+
);
41+
}
42+
} else {
43+
$availableOrders[$option->code] = $this->resolveLabel($attribute, $option);
44+
}
45+
}
46+
47+
return $availableOrders;
48+
}
49+
50+
private function resolveLabel(?Attribute $attribute, Option $option): string
51+
{
52+
return $option->label ?: $attribute?->getStoreLabel() ?? $option->code;
53+
}
54+
}

etc/frontend/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,7 @@
99
<type name="Magento\Catalog\Block\Product\ProductList\Toolbar">
1010
<plugin name="Opengento_CatalogSortOrder::advanced_sort_order" type="Opengento\CatalogSortOrder\Plugin\ApplySortOrder"/>
1111
</type>
12+
<type name="Magento\Catalog\Helper\Product\ProductList">
13+
<plugin name="Opengento_CatalogSortOrder::resolve_default_sort" type="Opengento\CatalogSortOrder\Plugin\ResolveDefaultSort"/>
14+
</type>
1215
</config>

0 commit comments

Comments
 (0)