3
3
namespace Algolia \AlgoliaSearch \Helper ;
4
4
5
5
use Algolia \AlgoliaSearch \Api \Product \ReplicaManagerInterface ;
6
- use Algolia \AlgoliaSearch \Service \IndexNameFetcher ;
7
6
use Magento ;
8
7
use Magento \Cookie \Helper \Cookie as CookieHelper ;
9
- use Magento \Customer \Api \GroupExcludedWebsiteRepositoryInterface ;
10
- use Magento \Customer \Model \ResourceModel \Group \Collection as GroupCollection ;
11
8
use Magento \Directory \Model \Currency as DirCurrency ;
12
9
use Magento \Framework \App \Filesystem \DirectoryList ;
13
10
use Magento \Framework \DataObject ;
14
- use Magento \Framework \Exception \LocalizedException ;
15
- use Magento \Framework \Exception \NoSuchEntityException ;
16
11
use Magento \Framework \Locale \Currency ;
17
12
use Magento \Framework \Serialize \SerializerInterface ;
18
13
use Magento \Store \Model \ScopeInterface ;
19
14
use Magento \Store \Model \StoreManagerInterface ;
15
+ use Magento \Customer \Api \GroupExcludedWebsiteRepositoryInterface ;
16
+ use Magento \Customer \Model \ResourceModel \Group \Collection as GroupCollection ;
20
17
21
18
class ConfigHelper
22
19
{
@@ -155,26 +152,6 @@ class ConfigHelper
155
152
public const NUMBER_OF_ELEMENT_BY_PAGE = 'algoliasearch_advanced/queue/number_of_element_by_page ' ;
156
153
public const ARCHIVE_LOG_CLEAR_LIMIT = 'algoliasearch_advanced/queue/archive_clear_limit ' ;
157
154
158
- /**
159
- * @var array<int,<array<string, mixed>>>
160
- */
161
- protected array $ _sortingIndices = [];
162
-
163
- /**
164
- * @param Magento\Framework\App\Config\ScopeConfigInterface $configInterface
165
- * @param StoreManagerInterface $storeManager
166
- * @param Currency $currency
167
- * @param DirCurrency $dirCurrency
168
- * @param DirectoryList $directoryList
169
- * @param Magento\Framework\Module\ResourceInterface $moduleResource
170
- * @param Magento\Framework\App\ProductMetadataInterface $productMetadata
171
- * @param Magento\Framework\Event\ManagerInterface $eventManager
172
- * @param SerializerInterface $serializer
173
- * @param GroupCollection $groupCollection
174
- * @param GroupExcludedWebsiteRepositoryInterface $groupExcludedWebsiteRepository
175
- * @param CookieHelper $cookieHelper
176
-
177
- */
178
155
public function __construct (
179
156
protected Magento \Framework \App \Config \ScopeConfigInterface $ configInterface ,
180
157
protected Magento \Framework \App \Config \Storage \WriterInterface $ configWriter ,
@@ -189,7 +166,6 @@ public function __construct(
189
166
protected GroupCollection $ groupCollection ,
190
167
protected GroupExcludedWebsiteRepositoryInterface $ groupExcludedWebsiteRepository ,
191
168
protected CookieHelper $ cookieHelper ,
192
- protected IndexNameFetcher $ indexNameFetcher
193
169
)
194
170
{}
195
171
@@ -1034,160 +1010,106 @@ public function getAutocompleteMinimumCharacterLength($storeId = null): int
1034
1010
}
1035
1011
1036
1012
/**
1037
- * When group pricing is enabled a replica must be created for each possible sort
1038
- *
1039
- * @param string $originalIndexName
1040
- * @param int $customerGroupId
1041
- * @param string $currency
1042
- * @param array $origAttr
1013
+ * @param $originalIndexName
1014
+ * @param $storeId
1015
+ * @param $currentCustomerGroupId
1016
+ * @param $attrs
1043
1017
* @return array
1018
+ * @throws Magento\Framework\Exception\LocalizedException
1019
+ * @throws Magento\Framework\Exception\NoSuchEntityException
1020
+ * @deprecated This method has been deprecated and should no longer be used
1021
+ * @see \Algolia\AlgoliaSearch\Service\Product\SortingTransformer
1044
1022
*/
1045
- protected function getCustomerGroupSortPriceOverride (
1046
- string $ originalIndexName ,
1047
- int $ customerGroupId ,
1048
- string $ currency ,
1049
- array $ origAttr ): array
1050
- {
1051
- $ attrName = $ origAttr ['attribute ' ];
1052
- $ sortDir = $ origAttr ['sort ' ];
1053
- $ groupIndexNameSuffix = 'group_ ' . $ customerGroupId ;
1054
- $ groupIndexName = $ originalIndexName . '_ ' . $ attrName . '_ ' . $ groupIndexNameSuffix . '_ ' . $ sortDir ;
1055
-
1056
- $ newAttr = array_merge (
1057
- $ origAttr ,
1058
- ['name ' => $ groupIndexName ]
1059
- );
1060
-
1061
- $ groupSortAttribute = $ attrName . '. ' . $ currency . '. ' . $ groupIndexNameSuffix ;
1062
- $ newAttr ['ranking ' ] = $ this ->getSortAttributingRankingSetting ($ groupSortAttribute , $ sortDir );
1063
- return $ this ->decorateSortAttribute ($ newAttr );
1064
- }
1065
-
1066
- /*
1067
- * Add data to the sort attribute object
1068
- */
1069
- protected function decorateSortAttribute (array $ attr ): array {
1070
- if (!array_key_exists ('label ' , $ attr ) && array_key_exists ('sortLabel ' , $ attr )) {
1071
- $ attr ['label ' ] = $ attr ['sortLabel ' ];
1072
- }
1073
- return $ attr ;
1074
- }
1075
-
1076
- /**
1077
- * Get ranking setting to be used for the standard sorting replica
1078
- * @param string $attrName
1079
- * @param string $sortDir
1080
- * @return string[]
1081
- */
1082
- protected function getSortAttributingRankingSetting (string $ attrName , string $ sortDir ): array
1083
- {
1084
- return [
1085
- $ sortDir . '( ' . $ attrName . ') ' ,
1086
- 'typo ' ,
1087
- 'geo ' ,
1088
- 'words ' ,
1089
- 'filters ' ,
1090
- 'proximity ' ,
1091
- 'attribute ' ,
1092
- 'exact ' ,
1093
- 'custom ' ,
1094
- ];
1095
- }
1096
-
1097
- /**
1098
- * @throws LocalizedException
1099
- */
1100
- protected function isGroupPricingExcludedFromWebsite (int $ customerGroupId , int $ websiteId ): bool
1023
+ public function getSortingIndices ($ originalIndexName , $ storeId = null , $ currentCustomerGroupId = null , $ attrs = null )
1101
1024
{
1102
- $ excludedWebsites = $ this ->groupExcludedWebsiteRepository ->getCustomerGroupExcludedWebsites ($ customerGroupId );
1103
- return in_array ($ websiteId , $ excludedWebsites );
1104
- }
1105
-
1106
- /**
1107
- * Augment sorting configuration with corresponding replica indices, ranking,
1108
- * and (as needed) customer group pricing
1109
- *
1110
- * @param ?int $storeId
1111
- * @param ?int $currentCustomerGroupId
1112
- * @param ?array $attrs - serialized array of sorting attributes to transform (defaults to saved sorting config)
1113
- * @return array of transformed sorting / replica objects
1114
- * @throws LocalizedException
1115
- * @throws NoSuchEntityException
1116
- */
1117
- public function getSortingIndices (
1118
- ?int $ storeId = null ,
1119
- ?int $ currentCustomerGroupId = null ,
1120
- ?array $ attrs = null
1121
- ): array
1122
- {
1123
- // Selectively cache this result - only cache manipulation of saved settings per store
1124
- $ useCache = is_null ($ currentCustomerGroupId ) && is_null ($ attrs );
1125
-
1126
- if ($ useCache
1127
- && array_key_exists ($ storeId , $ this ->_sortingIndices )
1128
- && is_array ($ this ->_sortingIndices [$ storeId ])) {
1129
- return $ this ->_sortingIndices [$ storeId ];
1130
- }
1131
-
1132
- // If no sorting configuration is supplied - obtain from the saved configuration
1133
- if (!$ attrs ) {
1025
+ if (!$ attrs ){
1134
1026
$ attrs = $ this ->getSorting ($ storeId );
1135
1027
}
1136
1028
1137
- $ primaryIndexName = $ this ->indexNameFetcher ->getProductIndexName ($ storeId );
1138
1029
$ currency = $ this ->getCurrencyCode ($ storeId );
1139
1030
$ attributesToAdd = [];
1140
1031
foreach ($ attrs as $ key => $ attr ) {
1141
1032
$ indexName = false ;
1142
1033
$ sortAttribute = false ;
1143
- // Group pricing
1144
- if ($ this ->isCustomerGroupsEnabled ($ storeId ) && $ attr [ReplicaManagerInterface::SORT_KEY_ATTRIBUTE_NAME ] === ReplicaManagerInterface::SORT_ATTRIBUTE_PRICE ) {
1145
- $ websiteId = (int ) $ this ->storeManager ->getStore ($ storeId )->getWebsiteId ();
1034
+ if ($ this ->isCustomerGroupsEnabled ($ storeId ) && $ attr ['attribute ' ] === 'price ' ) {
1035
+ $ websiteId = (int )$ this ->storeManager ->getStore ($ storeId )->getWebsiteId ();
1146
1036
$ groupCollection = $ this ->groupCollection ;
1147
1037
if (!is_null ($ currentCustomerGroupId )) {
1148
1038
$ groupCollection ->addFilter ('customer_group_id ' , $ currentCustomerGroupId );
1149
1039
}
1150
1040
foreach ($ groupCollection as $ group ) {
1151
- $ customerGroupId = (int ) $ group ->getData ('customer_group_id ' );
1152
- if (! $ this ->isGroupPricingExcludedFromWebsite ($ customerGroupId, $ websiteId )) {
1153
- $ newAttr = $ this -> getCustomerGroupSortPriceOverride ( $ primaryIndexName , $ customerGroupId , $ currency , $ attr );;
1154
- $ attributesToAdd [ $ newAttr [ ' sort ' ]][] = $ this -> decorateSortAttribute ( $ newAttr ) ;
1041
+ $ customerGroupId = (int )$ group ->getData ('customer_group_id ' );
1042
+ $ excludedWebsites = $ this ->groupExcludedWebsiteRepository -> getCustomerGroupExcludedWebsites ($ customerGroupId);
1043
+ if ( in_array ( $ websiteId , $ excludedWebsites )) {
1044
+ continue ;
1155
1045
}
1046
+ $ groupIndexNameSuffix = 'group_ ' . $ customerGroupId ;
1047
+ $ groupIndexName =
1048
+ $ originalIndexName . '_ ' . $ attr ['attribute ' ] . '_ ' . $ groupIndexNameSuffix . '_ ' . $ attr ['sort ' ];
1049
+ $ groupSortAttribute = $ attr ['attribute ' ] . '. ' . $ currency . '. ' . $ groupIndexNameSuffix ;
1050
+ $ newAttr = [];
1051
+ $ newAttr ['name ' ] = $ groupIndexName ;
1052
+ $ newAttr ['attribute ' ] = $ attr ['attribute ' ];
1053
+ $ newAttr ['sort ' ] = $ attr ['sort ' ];
1054
+ $ newAttr ['sortLabel ' ] = $ attr ['sortLabel ' ];
1055
+ if (!array_key_exists ('label ' , $ newAttr ) && array_key_exists ('sortLabel ' , $ newAttr )) {
1056
+ $ newAttr ['label ' ] = $ newAttr ['sortLabel ' ];
1057
+ }
1058
+ $ newAttr ['ranking ' ] = [
1059
+ $ newAttr ['sort ' ] . '( ' . $ groupSortAttribute . ') ' ,
1060
+ 'typo ' ,
1061
+ 'geo ' ,
1062
+ 'words ' ,
1063
+ 'filters ' ,
1064
+ 'proximity ' ,
1065
+ 'attribute ' ,
1066
+ 'exact ' ,
1067
+ 'custom ' ,
1068
+ ];
1069
+ $ attributesToAdd [$ newAttr ['sort ' ]][] = $ newAttr ;
1156
1070
}
1157
- // Regular pricing
1158
- } elseif ($ attr [ReplicaManagerInterface::SORT_KEY_ATTRIBUTE_NAME ] === ReplicaManagerInterface::SORT_ATTRIBUTE_PRICE ) {
1159
- $ indexName = $ primaryIndexName . '_ ' . $ attr ['attribute ' ] . '_ ' . 'default ' . '_ ' . $ attr ['sort ' ];
1071
+ } elseif ($ attr ['attribute ' ] === 'price ' ) {
1072
+ $ indexName = $ originalIndexName . '_ ' . $ attr ['attribute ' ] . '_ ' . 'default ' . '_ ' . $ attr ['sort ' ];
1160
1073
$ sortAttribute = $ attr ['attribute ' ] . '. ' . $ currency . '. ' . 'default ' ;
1161
- // All other sort attributes
1162
1074
} else {
1163
- $ indexName = $ primaryIndexName . '_ ' . $ attr ['attribute ' ] . '_ ' . $ attr ['sort ' ];
1075
+ $ indexName = $ originalIndexName . '_ ' . $ attr ['attribute ' ] . '_ ' . $ attr ['sort ' ];
1164
1076
$ sortAttribute = $ attr ['attribute ' ];
1165
1077
}
1166
-
1167
- // Decorate all non group pricing attributes
1168
1078
if ($ indexName && $ sortAttribute ) {
1169
1079
$ attrs [$ key ]['name ' ] = $ indexName ;
1170
- $ attrs [$ key ]['ranking ' ] = $ this ->getSortAttributingRankingSetting ($ sortAttribute , $ attr ['sort ' ]);
1171
- $ attrs [$ key ] = $ this ->decorateSortAttribute ($ attrs [$ key ]);
1080
+ if (!array_key_exists ('label ' , $ attrs [$ key ]) && array_key_exists ('sortLabel ' , $ attrs [$ key ])) {
1081
+ $ attrs [$ key ]['label ' ] = $ attrs [$ key ]['sortLabel ' ];
1082
+ }
1083
+ $ attrs [$ key ]['ranking ' ] = [
1084
+ $ attr ['sort ' ] . '( ' . $ sortAttribute . ') ' ,
1085
+ 'typo ' ,
1086
+ 'geo ' ,
1087
+ 'words ' ,
1088
+ 'filters ' ,
1089
+ 'proximity ' ,
1090
+ 'attribute ' ,
1091
+ 'exact ' ,
1092
+ 'custom ' ,
1093
+ ];
1172
1094
}
1173
1095
}
1174
1096
$ attrsToReturn = [];
1175
-
1176
- foreach ($ attrs as $ attr ) {
1177
- if ($ attr [ReplicaManagerInterface::SORT_KEY_ATTRIBUTE_NAME ] == ReplicaManagerInterface::SORT_ATTRIBUTE_PRICE
1178
- && count ($ attributesToAdd )
1179
- && isset ($ attributesToAdd [$ attr ['sort ' ]])) {
1180
- $ attrsToReturn = array_merge ($ attrsToReturn , $ attributesToAdd [$ attr ['sort ' ]]);
1181
- } else {
1182
- $ attrsToReturn [] = $ attr ;
1097
+ if (count ($ attributesToAdd ) > 0 ) {
1098
+ foreach ($ attrs as $ key => $ attr ) {
1099
+ if ($ attr ['attribute ' ] == 'price ' && isset ($ attributesToAdd [$ attr ['sort ' ]])) {
1100
+ $ attrsToReturn = array_merge ($ attrsToReturn , $ attributesToAdd [$ attr ['sort ' ]]);
1101
+ } else {
1102
+ $ attrsToReturn [] = $ attr ;
1103
+ }
1183
1104
}
1184
1105
}
1185
-
1186
- if ($ useCache ) {
1187
- $ this ->_sortingIndices [$ storeId ] = $ attrsToReturn ;
1106
+ if (count ($ attrsToReturn ) > 0 ) {
1107
+ return $ attrsToReturn ;
1188
1108
}
1189
-
1190
- return $ attrsToReturn ;
1109
+ if (is_array ($ attrs )) {
1110
+ return $ attrs ;
1111
+ }
1112
+ return [];
1191
1113
}
1192
1114
1193
1115
/***
0 commit comments