File tree Expand file tree Collapse file tree 3 files changed +66
-0
lines changed Expand file tree Collapse file tree 3 files changed +66
-0
lines changed Original file line number Diff line number Diff line change @@ -157,6 +157,30 @@ public function setAuthenticatedUserToken(Customer $customer): string|null
157
157
return $ userToken ;
158
158
}
159
159
160
+ /**
161
+ * Sets a token for a non-authenticated user
162
+ *
163
+ * @return void
164
+ */
165
+ public function setNonAuthenticatedUserToken (): void
166
+ {
167
+ $ metaData = $ this ->cookieMetadataFactory ->createPublicCookieMetadata ()
168
+ ->setDuration ($ this ->configHelper ->getCookieLifetime ())
169
+ ->setPath ('/ ' )
170
+ ->setHttpOnly (false )
171
+ ->setSecure (false );
172
+
173
+ try {
174
+ $ this ->cookieManager ->setPublicCookie (
175
+ InsightsHelper::ALGOLIA_ANON_USER_TOKEN_COOKIE_NAME ,
176
+ (string ) $ this ->cookieManager ->getCookie (InsightsHelper::ALGOLIA_ANON_USER_TOKEN_COOKIE_NAME ),
177
+ $ metaData
178
+ );
179
+ } catch (LocalizedException $ e ) {
180
+ $ this ->logger ->error ("Error writing anonymous user cookie: " . $ e ->getMessage ());
181
+ }
182
+ }
183
+
160
184
/**
161
185
* @param int|null $storeId
162
186
* @return bool
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Algolia \AlgoliaSearch \Observer \Insights ;
4
+
5
+ use Algolia \AlgoliaSearch \Helper \InsightsHelper ;
6
+ use Magento \Customer \Model \Session as CustomerSession ;
7
+ use Magento \Framework \Event \Observer ;
8
+ use Magento \Framework \Event \ObserverInterface ;
9
+
10
+ class CookieRefresherObserver implements ObserverInterface
11
+ {
12
+ /**
13
+ * CookieRefresherObserver observer constructor.
14
+ *
15
+ * @param CustomerSession $customerSession
16
+ * @param InsightsHelper $insightsHelper
17
+ *
18
+ */
19
+ public function __construct (
20
+ private readonly CustomerSession $ customerSession ,
21
+ private readonly InsightsHelper $ insightsHelper ,
22
+ ) {}
23
+
24
+ /**
25
+ * Renew anonymous or customer session token to update the lifetime
26
+ *
27
+ * @param Observer $observer
28
+ *
29
+ * @return void
30
+ */
31
+ public function execute (Observer $ observer ): void
32
+ {
33
+ if ($ this ->customerSession ->isLoggedIn ()) {
34
+ $ this ->insightsHelper ->setAuthenticatedUserToken ($ this ->customerSession ->getCustomer ());
35
+ } else {
36
+ $ this ->insightsHelper ->setNonAuthenticatedUserToken ();
37
+ }
38
+ }
39
+ }
Original file line number Diff line number Diff line change 32
32
<event name =" catalog_controller_product_init_after" >
33
33
<observer name =" algoliasearch_current_product" instance =" Algolia\AlgoliaSearch\Observer\RegisterCurrentProductObserver" />
34
34
</event >
35
+ <event name =" controller_action_predispatch" >
36
+ <observer name =" algoliasearch_cookie_refresher" instance =" Algolia\AlgoliaSearch\Observer\Insights\CookieRefresherObserver" shared =" false" />
37
+ </event >
35
38
</config >
You can’t perform that action at this time.
0 commit comments