2
2
3
3
namespace Algolia \AlgoliaSearch \Helper ;
4
4
5
- use Algolia \AlgoliaSearch \Model \Product \ReplicaManager ;
5
+ use Algolia \AlgoliaSearch \Api \Product \ReplicaManagerInterface ;
6
6
use Magento ;
7
+ use Magento \Cookie \Helper \Cookie as CookieHelper ;
8
+ use Magento \Customer \Api \GroupExcludedWebsiteRepositoryInterface ;
7
9
use Magento \Customer \Model \ResourceModel \Group \Collection as GroupCollection ;
8
10
use Magento \Directory \Model \Currency as DirCurrency ;
9
11
use Magento \Framework \App \Filesystem \DirectoryList ;
13
15
use Magento \Framework \Serialize \SerializerInterface ;
14
16
use Magento \Store \Model \ScopeInterface ;
15
17
use Magento \Store \Model \StoreManagerInterface ;
16
- use Magento \Customer \Api \GroupExcludedWebsiteRepositoryInterface ;
17
- use Magento \Cookie \Helper \Cookie as CookieHelper ;
18
-
19
18
20
19
class ConfigHelper
21
20
{
@@ -147,70 +146,6 @@ class ConfigHelper
147
146
public const ENHANCED_QUEUE_ARCHIVE = 'algoliasearch_advanced/queue/enhanced_archive ' ;
148
147
public const NUMBER_OF_ELEMENT_BY_PAGE = 'algoliasearch_advanced/queue/number_of_element_by_page ' ;
149
148
public const ARCHIVE_LOG_CLEAR_LIMIT = 'algoliasearch_advanced/queue/archive_clear_limit ' ;
150
- // https://www.algolia.com/doc/guides/managing-results/refine-results/sorting/in-depth/replicas/#what-are-virtual-replicas
151
- public const MAX_VIRTUAL_REPLICA_LIMIT = 20 ;
152
-
153
- public const SORT_ATTRIBUTE_PRICE = 'price ' ;
154
-
155
- /**
156
- * @var Magento\Framework\App\Config\ScopeConfigInterface
157
- */
158
- protected $ configInterface ;
159
-
160
- /**
161
- * @var Currency
162
- */
163
- protected $ currency ;
164
-
165
- /**
166
- * @var StoreManagerInterface
167
- */
168
- protected $ storeManager ;
169
-
170
- /**
171
- * @var DirCurrency
172
- */
173
- protected $ dirCurrency ;
174
-
175
- /**
176
- * @var DirectoryList
177
- */
178
- protected $ directoryList ;
179
-
180
- /**
181
- * @var Magento\Framework\Module\ResourceInterface
182
- */
183
- protected $ moduleResource ;
184
-
185
- /**
186
- * @var Magento\Framework\App\ProductMetadataInterface
187
- */
188
- protected $ productMetadata ;
189
-
190
- /**
191
- * @var Magento\Framework\Event\ManagerInterface
192
- */
193
- protected $ eventManager ;
194
-
195
- /**
196
- * @var SerializerInterface
197
- */
198
- protected $ serializer ;
199
-
200
- /**
201
- * @var GroupCollection
202
- */
203
- protected $ groupCollection ;
204
-
205
- /**
206
- * @var GroupExcludedWebsiteRepositoryInterface
207
- */
208
- protected $ groupExcludedWebsiteRepository ;
209
-
210
- /**
211
- * @var CookieHelper
212
- */
213
- protected $ cookieHelper ;
214
149
215
150
/**
216
151
* @var array<int,<array<string, mixed>>>
@@ -233,32 +168,21 @@ class ConfigHelper
233
168
234
169
*/
235
170
public function __construct (
236
- Magento \Framework \App \Config \ScopeConfigInterface $ configInterface ,
237
- StoreManagerInterface $ storeManager ,
238
- Currency $ currency ,
239
- DirCurrency $ dirCurrency ,
240
- DirectoryList $ directoryList ,
241
- Magento \Framework \Module \ResourceInterface $ moduleResource ,
242
- Magento \Framework \App \ProductMetadataInterface $ productMetadata ,
243
- Magento \Framework \Event \ManagerInterface $ eventManager ,
244
- SerializerInterface $ serializer ,
245
- GroupCollection $ groupCollection ,
246
- GroupExcludedWebsiteRepositoryInterface $ groupExcludedWebsiteRepository ,
247
- CookieHelper $ cookieHelper
248
- ) {
249
- $ this ->configInterface = $ configInterface ;
250
- $ this ->currency = $ currency ;
251
- $ this ->storeManager = $ storeManager ;
252
- $ this ->dirCurrency = $ dirCurrency ;
253
- $ this ->directoryList = $ directoryList ;
254
- $ this ->moduleResource = $ moduleResource ;
255
- $ this ->productMetadata = $ productMetadata ;
256
- $ this ->eventManager = $ eventManager ;
257
- $ this ->serializer = $ serializer ;
258
- $ this ->groupCollection = $ groupCollection ;
259
- $ this ->groupExcludedWebsiteRepository = $ groupExcludedWebsiteRepository ;
260
- $ this ->cookieHelper = $ cookieHelper ;
261
- }
171
+ protected Magento \Framework \App \Config \ScopeConfigInterface $ configInterface ,
172
+ protected Magento \Framework \App \Config \Storage \WriterInterface $ configWriter ,
173
+ protected StoreManagerInterface $ storeManager ,
174
+ protected Currency $ currency ,
175
+ protected DirCurrency $ dirCurrency ,
176
+ protected DirectoryList $ directoryList ,
177
+ protected Magento \Framework \Module \ResourceInterface $ moduleResource ,
178
+ protected Magento \Framework \App \ProductMetadataInterface $ productMetadata ,
179
+ protected Magento \Framework \Event \ManagerInterface $ eventManager ,
180
+ protected SerializerInterface $ serializer ,
181
+ protected GroupCollection $ groupCollection ,
182
+ protected GroupExcludedWebsiteRepositoryInterface $ groupExcludedWebsiteRepository ,
183
+ protected CookieHelper $ cookieHelper
184
+ )
185
+ {}
262
186
263
187
264
188
/**
@@ -519,6 +443,10 @@ public function getAutocompleteSections($storeId = null)
519
443
return [];
520
444
}
521
445
446
+ protected function serialize (array $ value ): string {
447
+ return $ this ->serializer ->serialize ($ value );
448
+ }
449
+
522
450
/**
523
451
* @param $value
524
452
* @return array|bool|float|int|mixed|string|null
@@ -1033,16 +961,14 @@ protected function getCustomerGroupSortPriceOverride(
1033
961
$ attrName = $ origAttr ['attribute ' ];
1034
962
$ sortDir = $ origAttr ['sort ' ];
1035
963
$ groupIndexNameSuffix = 'group_ ' . $ customerGroupId ;
1036
- $ groupIndexName =
1037
- $ originalIndexName . '_ ' . $ attrName . '_ ' . $ groupIndexNameSuffix . '_ ' . $ sortDir ;
1038
- $ groupSortAttribute = $ attrName . '. ' . $ currency . '. ' . $ groupIndexNameSuffix ;
1039
- $ newAttr = [
1040
- 'attribute ' => $ attrName ,
1041
- 'name ' => $ groupIndexName ,
1042
- 'sort ' => $ sortDir ,
1043
- 'sortLabel ' => $ origAttr ['sortLabel ' ]
1044
- ];
964
+ $ groupIndexName = $ originalIndexName . '_ ' . $ attrName . '_ ' . $ groupIndexNameSuffix . '_ ' . $ sortDir ;
965
+
966
+ $ newAttr = array_merge (
967
+ $ origAttr ,
968
+ ['name ' => $ groupIndexName ]
969
+ );
1045
970
971
+ $ groupSortAttribute = $ attrName . '. ' . $ currency . '. ' . $ groupIndexNameSuffix ;
1046
972
$ newAttr ['ranking ' ] = $ this ->getSortAttributingRankingSetting ($ groupSortAttribute , $ sortDir );
1047
973
return $ this ->decorateSortAttribute ($ newAttr );
1048
974
}
@@ -1090,6 +1016,7 @@ protected function isGroupPricingExcludedFromWebsite(int $customerGroupId, int $
1090
1016
/**
1091
1017
* Augment sorting configuration with corresponding replica indices, ranking,
1092
1018
* and (as needed) customer group pricing
1019
+ * TODO: MAGE-941 Remove the $originalIndexName param - this should never be needed as tmp indices cannot have attached replicas
1093
1020
*
1094
1021
* @param string $originalIndexName
1095
1022
* @param ?int $storeId
@@ -1126,7 +1053,7 @@ public function getSortingIndices(
1126
1053
$ indexName = false ;
1127
1054
$ sortAttribute = false ;
1128
1055
// Group pricing
1129
- if ($ this ->isCustomerGroupsEnabled ($ storeId ) && $ attr [' attribute ' ] === self ::SORT_ATTRIBUTE_PRICE ) {
1056
+ if ($ this ->isCustomerGroupsEnabled ($ storeId ) && $ attr [ReplicaManagerInterface:: SORT_KEY_ATTRIBUTE_NAME ] === ReplicaManagerInterface ::SORT_ATTRIBUTE_PRICE ) {
1130
1057
$ websiteId = (int ) $ this ->storeManager ->getStore ($ storeId )->getWebsiteId ();
1131
1058
$ groupCollection = $ this ->groupCollection ;
1132
1059
if (!is_null ($ currentCustomerGroupId )) {
@@ -1140,7 +1067,7 @@ public function getSortingIndices(
1140
1067
}
1141
1068
}
1142
1069
// Regular pricing
1143
- } elseif ($ attr [' attribute ' ] === self ::SORT_ATTRIBUTE_PRICE ) {
1070
+ } elseif ($ attr [ReplicaManagerInterface:: SORT_KEY_ATTRIBUTE_NAME ] === ReplicaManagerInterface ::SORT_ATTRIBUTE_PRICE ) {
1144
1071
$ indexName = $ originalIndexName . '_ ' . $ attr ['attribute ' ] . '_ ' . 'default ' . '_ ' . $ attr ['sort ' ];
1145
1072
$ sortAttribute = $ attr ['attribute ' ] . '. ' . $ currency . '. ' . 'default ' ;
1146
1073
// All other sort attributes
@@ -1159,7 +1086,7 @@ public function getSortingIndices(
1159
1086
$ attrsToReturn = [];
1160
1087
1161
1088
foreach ($ attrs as $ attr ) {
1162
- if ($ attr [' attribute ' ] == self ::SORT_ATTRIBUTE_PRICE
1089
+ if ($ attr [ReplicaManagerInterface:: SORT_KEY_ATTRIBUTE_NAME ] == ReplicaManagerInterface ::SORT_ATTRIBUTE_PRICE
1163
1090
&& count ($ attributesToAdd )
1164
1091
&& isset ($ attributesToAdd [$ attr ['sort ' ]])) {
1165
1092
$ attrsToReturn = array_merge ($ attrsToReturn , $ attributesToAdd [$ attr ['sort ' ]]);
@@ -1177,26 +1104,42 @@ public function getSortingIndices(
1177
1104
1178
1105
/***
1179
1106
* @param $storeId
1180
- * @return array|bool|float|int|mixed| string|null
1107
+ * @return array< string,<array<string, mixed>>>
1181
1108
*/
1182
- public function getSorting ($ storeId = null )
1109
+ public function getSorting ($ storeId = null ): array
1183
1110
{
1184
1111
return $ this ->unserialize ($ this ->getRawSortingValue ($ storeId ));
1185
1112
}
1186
1113
1187
1114
/**
1188
- * @param $storeId
1189
- * @return mixed
1115
+ * @param int|null $storeId
1116
+ * @return string
1190
1117
*/
1191
- public function getRawSortingValue ($ storeId = null )
1118
+ public function getRawSortingValue (? int $ storeId = null ): string
1192
1119
{
1193
- return $ this ->configInterface ->getValue (
1120
+ return ( string ) $ this ->configInterface ->getValue (
1194
1121
self ::SORTING_INDICES ,
1195
1122
ScopeInterface::SCOPE_STORE ,
1196
1123
$ storeId
1197
1124
);
1198
1125
}
1199
1126
1127
+ /**
1128
+ * @param array $sorting
1129
+ * @param string|null $scope
1130
+ * @param int|null $scopeId
1131
+ * @return void
1132
+ */
1133
+ public function setSorting (array $ sorting , ?string $ scope = null , ?int $ scopeId = null ): void
1134
+ {
1135
+ $ this ->configWriter ->save (
1136
+ self ::SORTING_INDICES ,
1137
+ $ this ->serialize ($ sorting ),
1138
+ $ scope ,
1139
+ $ scopeId
1140
+ );
1141
+ }
1142
+
1200
1143
/**
1201
1144
* @param $storeId
1202
1145
* @return string
@@ -1213,11 +1156,21 @@ public function getCurrencyCode($storeId = null)
1213
1156
* @param $storeId
1214
1157
* @return bool
1215
1158
*/
1216
- public function isCustomerGroupsEnabled ($ storeId = null )
1159
+ public function isCustomerGroupsEnabled ($ storeId = null ): bool
1217
1160
{
1218
1161
return $ this ->configInterface ->isSetFlag (self ::CUSTOMER_GROUPS_ENABLE , ScopeInterface::SCOPE_STORE , $ storeId );
1219
1162
}
1220
1163
1164
+ public function setCustomerGroupsEnabled (bool $ val , ?string $ scope = null , ?int $ scopeId = null ): void
1165
+ {
1166
+ $ this ->configWriter ->save (
1167
+ self ::CUSTOMER_GROUPS_ENABLE ,
1168
+ $ val ? '1 ' : '0 ' ,
1169
+ $ scope ,
1170
+ $ scopeId
1171
+ );
1172
+ }
1173
+
1221
1174
/**
1222
1175
* @param $storeId
1223
1176
* @return bool
@@ -1869,7 +1822,7 @@ public function useVirtualReplica(?int $storeId = null): bool
1869
1822
return (bool ) count (array_filter (
1870
1823
$ this ->getSorting ($ storeId ),
1871
1824
function ($ sort ) {
1872
- return $ sort [ReplicaManager ::SORT_KEY_VIRTUAL_REPLICA ];
1825
+ return $ sort [ReplicaManagerInterface ::SORT_KEY_VIRTUAL_REPLICA ];
1873
1826
}
1874
1827
));
1875
1828
}
0 commit comments