Skip to content

Commit 44e566c

Browse files
committed
MAGE-1201 Remove dependency on renderingContent check and add a soft fail only on unexpected API results
1 parent 8e7c819 commit 44e566c

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

Observer/RecommendSettings.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
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;
1517

1618
class RecommendSettings implements ObserverInterface
1719
{
@@ -36,7 +38,9 @@ public function __construct(
3638
protected readonly WriterInterface $configWriter,
3739
protected readonly ProductRepositoryInterface $productRepository,
3840
protected readonly RecommendManagementInterface $recommendManagement,
39-
protected readonly SearchCriteriaBuilder $searchCriteriaBuilder
41+
protected readonly SearchCriteriaBuilder $searchCriteriaBuilder,
42+
protected readonly State $appState,
43+
protected readonly MessageManagerInterface $messageManager
4044
){}
4145

4246
/**
@@ -143,9 +147,10 @@ protected function validateRecommendation(string $changedPath, string $recommend
143147
{
144148
try {
145149
$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.",
150+
if ($this->shouldDisplayWarning($recommendations)) {
151+
$this->messageManager->addWarningMessage(__(
152+
"It appears that there is no trained model available for Algolia application ID %1. "
153+
. "Please verify your configuration in the Algolia Dashboard before continuing.",
149154
$this->configHelper->getApplicationID()
150155
));
151156
}
@@ -160,6 +165,19 @@ protected function validateRecommendation(string $changedPath, string $recommend
160165
}
161166
}
162167

168+
/**
169+
* If API does not return a hits response the model may not be configured correctly.
170+
* Do not hard fail but alert the end user.
171+
* @throws LocalizedException
172+
*/
173+
protected function shouldDisplayWarning(array $recommendationResponse): bool
174+
{
175+
return
176+
$this->appState->getAreaCode() === \Magento\Framework\App\Area::AREA_ADMINHTML
177+
&&
178+
!array_key_exists('hits', $recommendationResponse);
179+
}
180+
163181
protected function getUserFriendlyRecommendApiErrorMessage(\Exception $e): string
164182
{
165183
$msg = $e->getMessage();

0 commit comments

Comments
 (0)