9
9
use Magento \Catalog \Model \Product \Visibility ;
10
10
use Magento \Framework \Api \SearchCriteriaBuilder ;
11
11
use Magento \Framework \App \Config \Storage \WriterInterface ;
12
+ use Magento \Framework \App \State ;
12
13
use Magento \Framework \Event \Observer ;
13
14
use Magento \Framework \Event \ObserverInterface ;
14
15
use Magento \Framework \Exception \LocalizedException ;
16
+ use Magento \Framework \Message \ManagerInterface as MessageManagerInterface ;
15
17
16
18
class RecommendSettings implements ObserverInterface
17
19
{
@@ -36,7 +38,9 @@ public function __construct(
36
38
protected readonly WriterInterface $ configWriter ,
37
39
protected readonly ProductRepositoryInterface $ productRepository ,
38
40
protected readonly RecommendManagementInterface $ recommendManagement ,
39
- protected readonly SearchCriteriaBuilder $ searchCriteriaBuilder
41
+ protected readonly SearchCriteriaBuilder $ searchCriteriaBuilder ,
42
+ protected readonly State $ appState ,
43
+ protected readonly MessageManagerInterface $ messageManager
40
44
){}
41
45
42
46
/**
@@ -143,9 +147,10 @@ protected function validateRecommendation(string $changedPath, string $recommend
143
147
{
144
148
try {
145
149
$ 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. " ,
149
154
$ this ->configHelper ->getApplicationID ()
150
155
));
151
156
}
@@ -160,6 +165,19 @@ protected function validateRecommendation(string $changedPath, string $recommend
160
165
}
161
166
}
162
167
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
+
163
181
protected function getUserFriendlyRecommendApiErrorMessage (\Exception $ e ): string
164
182
{
165
183
$ msg = $ e ->getMessage ();
0 commit comments