Skip to content

Commit b6622e2

Browse files
authored
Merge pull request #1759 from algolia/release/3.15.1
MAGE-1267: merge 3.15.1 to main
2 parents 3ecbf4e + ccbe9c4 commit b6622e2

File tree

18 files changed

+737
-146
lines changed

18 files changed

+737
-146
lines changed

Api/Product/ReplicaManagerInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function syncReplicasToAlgolia(int $storeId, array $primaryIndexSettings)
3131
/**
3232
* Delete the replica indices on a store index
3333
* @param int $storeId
34-
* @param bool $unused Defaults to false - if true identifies any straggler indices and deletes those, otherwise deletes the replicas it knows aobut
34+
* @param bool $unused Defaults to false - if true identifies any straggler indices and deletes those, otherwise deletes the replicas it knows about
3535
* @return void
3636
*
3737
* @throws LocalizedException

Block/Configuration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ public function getConfiguration()
274274
'autofocus' => true,
275275
'resultPageUrl' => $this->getCatalogSearchHelper()->getResultUrl(),
276276
'request' => [
277-
'query' => htmlspecialchars(html_entity_decode($query)),
277+
'query' => htmlspecialchars(html_entity_decode((string)$query)),
278278
'refinementKey' => $refinementKey,
279279
'refinementValue' => $refinementValue,
280280
'categoryId' => $categoryId,

CHANGELOG.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
# CHANGE LOG
22

3+
## 3.15.1
4+
5+
### Features
6+
- Algolia's [Query Categorization feature](https://www.algolia.com/doc/integration/magento-2/how-it-works/query-categorization) is now compatible with the extension.
7+
8+
### Updates
9+
- Updated integration tests to use `AlgoliaConnector`
10+
- Updated `ReplicaManager` service to handle multi stores properly with a `$storeId` parameter.
11+
- Pinned PHP Client version to 4.18.3
12+
13+
### Bug Fixes
14+
- Fixed `RebuildReplicasPatch` bug where replica detach logic wasn't properly applied in some cases.
15+
- Fixed a bug where credentials errors weren't gracefully handled on the SKU reindexing form
16+
- Fixed a bug where the `q` parameter wasn't properly handled in case it was missing on the catalogsearch page. (thanks @PromInc)
17+
- Fixed Recommend model validation when configuration is saved in the Magento admin.
18+
319
## 3.15.0
420

521
### Features
@@ -12,6 +28,7 @@
1228
- Added a feature to enable automatic price indexing within the Advanced section of the configuration (This feature should help alleviate issues where missing pricing records prevent Algolia from being able to index products. See [documentation](https://www.algolia.com/doc/integration/magento-2/troubleshooting/data-indexes-queues/#price-index-dependencies) for further details.)
1329
- Reorganization of the test folders
1430
- Added unit and integration tests for full page cache (FPC)
31+
- Added new CLI command for synonyms deduplication
1532

1633
### Updates
1734
- Tests: Added possibility to run tests with multiple applications IDs.

Controller/Adminhtml/Reindex/Save.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,15 @@ public function execute()
100100
[$e->getProduct()->getName(), $e->getProduct()->getSku()]
101101
)
102102
);
103+
} catch (\Exception $e) {
104+
$this->messageManager->addExceptionMessage(
105+
$e,
106+
__(
107+
'Unable to index product(s) due to the following error: %1',
108+
$e->getMessage()
109+
)
110+
);
111+
break;
103112
}
104113
}
105114

Model/RecommendManagement.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public function getTrendingItemsRecommendation(): array
7575
*/
7676
public function getLookingSimilarRecommendation(string $productId): array
7777
{
78-
return $this->getRecommendations($productId, 'bought-together');
78+
return $this->getRecommendations($productId, 'looking-similar');
7979
}
8080

8181
/**

Observer/RecommendSettings.php

Lines changed: 117 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,21 @@
99
use Magento\Catalog\Model\Product\Visibility;
1010
use Magento\Framework\Api\SearchCriteriaBuilder;
1111
use Magento\Framework\App\Config\Storage\WriterInterface;
12+
use Magento\Framework\App\State;
1213
use Magento\Framework\Event\Observer;
1314
use Magento\Framework\Event\ObserverInterface;
1415
use Magento\Framework\Exception\LocalizedException;
16+
use Magento\Framework\Message\ManagerInterface as MessageManagerInterface;
17+
use Psr\Log\LoggerInterface;
1518

1619
class RecommendSettings implements ObserverInterface
1720
{
1821
const QUANTITY_AND_STOCK_STATUS = 'quantity_and_stock_status';
1922
const STATUS = 'status';
2023
const VISIBILITY = 'visibility';
2124

25+
const ENFORCE_VALIDATION = 0;
26+
2227
/**
2328
* @var string
2429
*/
@@ -36,7 +41,10 @@ public function __construct(
3641
protected readonly WriterInterface $configWriter,
3742
protected readonly ProductRepositoryInterface $productRepository,
3843
protected readonly RecommendManagementInterface $recommendManagement,
39-
protected readonly SearchCriteriaBuilder $searchCriteriaBuilder
44+
protected readonly SearchCriteriaBuilder $searchCriteriaBuilder,
45+
protected readonly State $appState,
46+
protected readonly MessageManagerInterface $messageManager,
47+
protected readonly LoggerInterface $logger
4048
){}
4149

4250
/**
@@ -143,24 +151,122 @@ protected function validateRecommendation(string $changedPath, string $recommend
143151
{
144152
try {
145153
$recommendations = $this->recommendManagement->$recommendationMethod($this->getProductId());
146-
if (empty($recommendations['renderingContent'])) {
147-
throw new LocalizedException(__(
148-
"It appears that there is no trained model available for Algolia application ID %1.",
149-
$this->configHelper->getApplicationID()
150-
));
151-
}
154+
155+
$this->validateRecommendApiResponse($recommendations, $modelName);
152156
} catch (\Exception $e) {
153-
$this->configWriter->save($changedPath, 0);
154-
throw new LocalizedException(__(
155-
"Unable to save %1 Recommend configuration due to the following error: %2",
157+
$this->handleRecommendApiException($e, $modelName, $changedPath);
158+
}
159+
}
160+
161+
/**
162+
* If API does not return a hits response the model may not be configured correctly.
163+
* Do not hard fail but alert the end user.
164+
*
165+
* @throws LocalizedException
166+
*/
167+
protected function validateRecommendApiResponse(array $recommendResponse, string $modelName): void
168+
{
169+
if (!array_key_exists('hits', $recommendResponse)) {
170+
$msg = __(
171+
"It appears that there is no trained %1 model available for Algolia application ID %2. "
172+
. "Please verify your configuration in the Algolia Dashboard before continuing.",
173+
$modelName,
174+
$this->configHelper->getApplicationID()
175+
);
176+
177+
if ($this->shouldDisplayWarning()) {
178+
$this->messageManager->addWarningMessage($msg);
179+
}
180+
else {
181+
$this->logger->warning($msg);
182+
}
183+
}
184+
}
185+
186+
/**
187+
* Handles exceptions on the test request against the Recommend API
188+
*
189+
* The goal is to warn the user of potential issues so they do not enable a model on the front end that has not been
190+
* properly configured in Algolia
191+
*
192+
* TODO: Implement store scoped validation
193+
*
194+
* @throws LocalizedException
195+
*/
196+
protected function handleRecommendApiException(\Exception $e, string $modelName, string $changedPath): void
197+
{
198+
$msg = $this->getUserFriendlyRecommendApiErrorMessage($e);
199+
200+
if (self::ENFORCE_VALIDATION) {
201+
$this->rollBack($changedPath, __(
202+
"Unable to save %1 Recommend configuration due to the following error: %2",
156203
$modelName,
157-
$e->getMessage()
204+
$msg
158205
)
159206
);
160207
}
208+
209+
$msg = __(
210+
"Error encountered while enabling %1 recommendations: %2",
211+
$modelName,
212+
$msg
213+
);
214+
215+
if ($this->shouldDisplayWarning()) {
216+
$this->messageManager->addWarningMessage(
217+
$msg
218+
. ' Please verify your configuration in the Algolia Dashboard before continuing.');
219+
}
220+
else {
221+
$this->logger->warning($msg);
222+
}
223+
}
224+
225+
/*
226+
* For hard fail only
227+
*/
228+
protected function rollBack(string $changedPath, \Magento\Framework\Phrase $message): void
229+
{
230+
$this->configWriter->save($changedPath, 0);
231+
throw new LocalizedException($message);
232+
}
233+
234+
/**
235+
* Warnings should only be displayed within the admin panel
236+
* @throws LocalizedException
237+
*/
238+
protected function shouldDisplayWarning(): bool
239+
{
240+
return $this->appState->getAreaCode() === \Magento\Framework\App\Area::AREA_ADMINHTML
241+
&& php_sapi_name() !== 'cli';
242+
}
243+
244+
/**
245+
* If there is no model on the index then a 404 error should be returned
246+
* (which will cause the exception on the API call) because there is no model for that index
247+
* However errors which say "Index does not exist" are cryptic
248+
* This function serves to make this clearer to the user while also filtering out the possible
249+
* "ObjectID does not exist" error which can occur if the model does not contain the test product
250+
*/
251+
protected function getUserFriendlyRecommendApiErrorMessage(\Exception $e): string
252+
{
253+
$msg = $e->getMessage();
254+
if ($e->getCode() === 404) {
255+
if (!!preg_match('/index.*does not exist/i', $msg)) {
256+
$msg = (string) __("A trained model could not be found.");
257+
}
258+
if (!!preg_match('/objectid does not exist/i', $msg)) {
259+
$msg = (string) __("Could not find test product in trained model.");
260+
}
261+
}
262+
return $msg;
161263
}
162264

163265
/**
266+
* Retrieve a test product for requests against the Recommend API
267+
*
268+
* TODO: Implement store scoping and independently address 404 where objectID is not found
269+
*
164270
* @return string - Product ID string for use in API calls
165271
* @throws LocalizedException
166272
*/

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Algolia Search & Discovery extension for Magento 2
22
==================================================
33

4-
![Latest version](https://img.shields.io/badge/latest-3.15.0-green)
4+
![Latest version](https://img.shields.io/badge/latest-3.15.1-green)
55
![Magento 2](https://img.shields.io/badge/Magento-2.4.6+-orange)
66

77
![PHP](https://img.shields.io/badge/PHP-8.1%2C8.2%2C8.3-blue)

0 commit comments

Comments
 (0)