Skip to content
This repository was archived by the owner on Jun 2, 2022. It is now read-only.

Commit 7bf443e

Browse files
author
Patrick McLain
committed
Store result from button status request in cookie
This commit stores the result from the initial one click status ajax request in a cookie for subsequent product views. This removes the need for requesting the status with every page load.
1 parent afbac81 commit 7bf443e

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

view/frontend/web/js/buy-now.js

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ define([
22
'jquery',
33
'mage/translate',
44
'Magento_Ui/js/modal/alert',
5-
'underscore'
5+
'underscore',
6+
'mage/cookies'
67
], function ($, $t, alert, _) {
78
"use strict";
89

@@ -17,20 +18,34 @@ define([
1718
confirmationSelector: '#one-click-confirmation'
1819
},
1920

21+
cookie: 'occ_status',
22+
cookieEnabled: 'enabled',
23+
cookieDisabled: 'disabled',
24+
2025
_create: function () {
21-
this._initButton()
26+
this._initButton();
2227
},
2328

2429
_initButton: function () {
2530
var self = this;
26-
$.ajax({
27-
url: self.options.isAvailableUrl
28-
}).done(function(result) {
29-
if (!result) {
30-
return;
31-
}
32-
self._createButton();
33-
});
31+
32+
switch ($.mage.cookies.get(this.cookie)) {
33+
case this.cookieEnabled:
34+
this._createButton();
35+
break;
36+
case this.cookieDisabled:
37+
break;
38+
default:
39+
$.ajax({
40+
url: self.options.isAvailableUrl
41+
}).done(function(result) {
42+
if (!result) {
43+
$.mage.cookies.set(self.cookie, self.cookieDisabled);
44+
}
45+
$.mage.cookies.set(self.cookie, self.cookieEnabled, {lifetime: -1});
46+
self._createButton();
47+
});
48+
}
3449
},
3550

3651
_createButton: function () {

0 commit comments

Comments
 (0)