Skip to content

Commit 0060662

Browse files
committed
MAGE-997 Merge new getCookie functionality
1 parent 1cd44fb commit 0060662

File tree

1 file changed

+28
-19
lines changed

1 file changed

+28
-19
lines changed

view/frontend/web/js/internals/common.js

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
define(['jquery', 'algoliaInstantSearchLib', 'algoliaBase64'], function ($, instantsearch, algoliaBase64) {
1+
define(['jquery', 'algoliaInstantSearchLib', 'algoliaBase64', 'Magento_PageCache/js/form-key-provider'], function ($, instantsearch, algoliaBase64) {
22
const USE_GLOBALS = true;
33

44
// Character maps supplied for more performant Regex ops
@@ -243,23 +243,36 @@ define(['jquery', 'algoliaInstantSearchLib', 'algoliaBase64'], function ($, inst
243243
const mobileRegex = /android|webos|iphone|ipad|ipod|blackberry|iemobile|opera mini|mobile/i;
244244
return mobileRegex.test(navigator.userAgent);
245245
},
246-
246+
247247
isTouchDevice: () => {
248248
return 'ontouchstart' in window || navigator.maxTouchPoints > 0;
249249
}
250250
};
251-
251+
252252
const legacyGlobalFunctions = {
253253
isMobile: () => {
254254
return utils.isMobileUserAgent() || utils.isTouchDevice();
255255
},
256256

257257
getCookie: (name) => {
258-
const value = `; ${document.cookie}`;
259-
const parts = value.split(`; ${name}=`);
260-
return (parts.length === 2)
261-
? parts.pop().split(';').shift()
262-
: '';
258+
let cookie, i;
259+
260+
const cookieName = name + "=",
261+
cookieArr = document.cookie.split(';');
262+
263+
for (i = 0; i < cookieArr.length; i++) {
264+
cookie = cookieArr[i];
265+
266+
while (cookie.charAt(0) === ' ') {
267+
cookie = cookie.substring(1, cookie.length);
268+
}
269+
270+
if (cookie.indexOf(cookieName) === 0) {
271+
return cookie.substring(cookieName.length, cookie.length);
272+
}
273+
}
274+
275+
return "";
263276
},
264277

265278
// @deprecated This function will be removed from this module in a future version
@@ -365,27 +378,23 @@ define(['jquery', 'algoliaInstantSearchLib', 'algoliaBase64'], function ($, inst
365378
}
366379
hit._highlightResult.default_bundle_options = default_bundle_option;
367380
}
368-
381+
369382
// Add to cart parameters
370383
var action = algoliaConfig.instant.addToCartParams.action + 'product/' + hit.objectID + '/';
371-
372-
var correctFKey = this.getCookie('form_key');
373-
374-
if (correctFKey != "" && algoliaConfig.instant.addToCartParams.formKey != correctFKey) {
375-
algoliaConfig.instant.addToCartParams.formKey = correctFKey;
376-
}
377-
384+
385+
algoliaConfig.instant.addToCartParams.formKey = getCookie('form_key');
386+
378387
hit.addToCart = {
379388
'action': action,
380389
'redirectUrlParam': algoliaConfig.instant.addToCartParams.redirectUrlParam,
381390
'uenc': algoliaBase64.mageEncode(action),
382391
'formKey': algoliaConfig.instant.addToCartParams.formKey
383392
};
384-
393+
385394
if (hit.__queryID) {
386-
395+
387396
hit.urlForInsights = hit.url;
388-
397+
389398
if (algoliaConfig.ccAnalytics.enabled
390399
&& algoliaConfig.ccAnalytics.conversionAnalyticsMode !== 'disabled') {
391400
var insightsDataUrlString = $.param({

0 commit comments

Comments
 (0)