Commit 0b0b1232d9125733c60c303d0c891c40ae79d89f
1 parent
8015d9d9
bonuses in basket
Showing
4 changed files
with
115 additions
and
3 deletions
Show diff stats
controllers/BasketController.php
@@ -137,4 +137,41 @@ | @@ -137,4 +137,41 @@ | ||
137 | ); | 137 | ); |
138 | return $output; | 138 | return $output; |
139 | } | 139 | } |
140 | + | ||
141 | + | ||
142 | + | ||
143 | + public function actionAddBonus($bonus){ | ||
144 | + $response = \Yii::$app->response; | ||
145 | + $response->format = Response::FORMAT_JSON; | ||
146 | + /** | ||
147 | + * @var Basket $basket | ||
148 | + */ | ||
149 | + $basket = \Yii::$app->get('basket'); | ||
150 | + | ||
151 | + $basket->setBonus($bonus); | ||
152 | + $result = [ | ||
153 | + 'basket' => $basket->getData(), | ||
154 | + 'modal' => $this->getModal($basket), | ||
155 | + 'cart' => $this->getCart($basket), | ||
156 | + 'small' => $this->actionRenderBasket() | ||
157 | + ]; | ||
158 | + return $result; | ||
159 | + } | ||
160 | + | ||
161 | + public function actionDeleteBonus(){ | ||
162 | + $response = \Yii::$app->response; | ||
163 | + $response->format = Response::FORMAT_JSON; | ||
164 | + /** | ||
165 | + * @var Basket $basket | ||
166 | + */ | ||
167 | + $basket = \Yii::$app->get('basket'); | ||
168 | + $basket->removeBonus(); | ||
169 | + $result = [ | ||
170 | + 'basket' => $basket->getData(), | ||
171 | + 'modal' => $this->getModal($basket), | ||
172 | + 'cart' => $this->getCart($basket), | ||
173 | + 'small' => $this->actionRenderBasket() | ||
174 | + ]; | ||
175 | + return $result; | ||
176 | + } | ||
140 | } | 177 | } |
models/Basket.php
@@ -316,5 +316,29 @@ | @@ -316,5 +316,29 @@ | ||
316 | { | 316 | { |
317 | $this->setData([]); | 317 | $this->setData([]); |
318 | } | 318 | } |
319 | + | ||
320 | + /** | ||
321 | + * @param int $bonuses | ||
322 | + */ | ||
323 | + public function setBonus(int $bonuses){ | ||
324 | + $this->session->set('bonus', $bonuses); | ||
325 | + } | ||
326 | + | ||
327 | + /** | ||
328 | + * @return mixed|null | ||
329 | + */ | ||
330 | + public function getBonuses(){ | ||
331 | + if ($this->session->has('bonus')){ | ||
332 | + return $this->session->get('bonus'); | ||
333 | + } | ||
334 | + return null; | ||
335 | + } | ||
336 | + | ||
337 | + /** | ||
338 | + * | ||
339 | + */ | ||
340 | + public function removeBonus(){ | ||
341 | + $this->session->remove('bonus'); | ||
342 | + } | ||
319 | } | 343 | } |
320 | 344 | ||
321 | \ No newline at end of file | 345 | \ No newline at end of file |
web/js/artbox_basket.js
1 | ///<reference path="node_modules/@types/jquery/index.d.ts" /> | 1 | ///<reference path="node_modules/@types/jquery/index.d.ts" /> |
2 | -var ArtboxBasket = (function () { | 2 | +var ArtboxBasket = /** @class */ (function () { |
3 | function ArtboxBasket(settings) { | 3 | function ArtboxBasket(settings) { |
4 | if (settings === void 0) { settings = {}; } | 4 | if (settings === void 0) { settings = {}; } |
5 | this._defaults = { | 5 | this._defaults = { |
@@ -7,7 +7,8 @@ var ArtboxBasket = (function () { | @@ -7,7 +7,8 @@ var ArtboxBasket = (function () { | ||
7 | url: 'basket', | 7 | url: 'basket', |
8 | initError: 'Basket cannot be init', | 8 | initError: 'Basket cannot be init', |
9 | modalSelector: '.basket_modal', | 9 | modalSelector: '.basket_modal', |
10 | - cartSelector: '.basket_wrapper' | 10 | + cartSelector: '.basket_wrapper', |
11 | + smallSelector: '.small_basket_page' | ||
11 | }; | 12 | }; |
12 | this._settings = {}; | 13 | this._settings = {}; |
13 | this._settings = ArtboxBasket.mergeObjects(this._defaults, settings); | 14 | this._settings = ArtboxBasket.mergeObjects(this._defaults, settings); |
@@ -46,6 +47,7 @@ var ArtboxBasket = (function () { | @@ -46,6 +47,7 @@ var ArtboxBasket = (function () { | ||
46 | configurable: true | 47 | configurable: true |
47 | }); | 48 | }); |
48 | ArtboxBasket.prototype.init = function (update_modal, update_cart) { | 49 | ArtboxBasket.prototype.init = function (update_modal, update_cart) { |
50 | + this.deleteBonus(); | ||
49 | $.get(this.getLanguagePath() + this._settings['url'], function (data) { | 51 | $.get(this.getLanguagePath() + this._settings['url'], function (data) { |
50 | this._items = data.basket; | 52 | this._items = data.basket; |
51 | if (update_modal) { | 53 | if (update_modal) { |
@@ -100,6 +102,10 @@ var ArtboxBasket = (function () { | @@ -100,6 +102,10 @@ var ArtboxBasket = (function () { | ||
100 | var cart = $(this._settings['cartSelector']); | 102 | var cart = $(this._settings['cartSelector']); |
101 | cart.html(cart_html); | 103 | cart.html(cart_html); |
102 | }; | 104 | }; |
105 | + ArtboxBasket.prototype.updateSmall = function (small_html) { | ||
106 | + var cart = $(this._settings['smallSelector']); | ||
107 | + cart.html(small_html); | ||
108 | + }; | ||
103 | ArtboxBasket.prototype.hideBasket = function () { | 109 | ArtboxBasket.prototype.hideBasket = function () { |
104 | $(this._settings['modalSelector']) | 110 | $(this._settings['modalSelector']) |
105 | .animate({ | 111 | .animate({ |
@@ -137,5 +143,23 @@ var ArtboxBasket = (function () { | @@ -137,5 +143,23 @@ var ArtboxBasket = (function () { | ||
137 | return '/'; | 143 | return '/'; |
138 | } | 144 | } |
139 | }; | 145 | }; |
146 | + ArtboxBasket.prototype.setBonus = function (bonus) { | ||
147 | + return $.post(this.getLanguagePath() + this._settings['url'] + '/add-bonus?bonus=' + bonus, function (data) { | ||
148 | + this._items = data.basket; | ||
149 | + this.updateModal(data.modal, data.cart, true); | ||
150 | + this.updateSmall(data.small); | ||
151 | + }.bind(this), 'json').fail(function (xhr, status, error) { | ||
152 | + console.error(error); | ||
153 | + }); | ||
154 | + }; | ||
155 | + ArtboxBasket.prototype.deleteBonus = function () { | ||
156 | + return $.post(this.getLanguagePath() + this._settings['url'] + '/delete-bonus', function (data) { | ||
157 | + this._items = data.basket; | ||
158 | + this.updateModal(data.modal, data.cart, true); | ||
159 | + this.updateSmall(data.small); | ||
160 | + }.bind(this), 'json').fail(function (xhr, status, error) { | ||
161 | + console.error(error); | ||
162 | + }); | ||
163 | + }; | ||
140 | return ArtboxBasket; | 164 | return ArtboxBasket; |
141 | }()); | 165 | }()); |
web/js/artbox_basket.ts
@@ -7,7 +7,8 @@ class ArtboxBasket { | @@ -7,7 +7,8 @@ class ArtboxBasket { | ||
7 | url: 'basket', | 7 | url: 'basket', |
8 | initError: 'Basket cannot be init', | 8 | initError: 'Basket cannot be init', |
9 | modalSelector: '.basket_modal', | 9 | modalSelector: '.basket_modal', |
10 | - cartSelector: '.basket_wrapper' | 10 | + cartSelector: '.basket_wrapper', |
11 | + smallSelector: '.small_basket_page' | ||
11 | }; | 12 | }; |
12 | private _settings: Object = {}; | 13 | private _settings: Object = {}; |
13 | 14 | ||
@@ -41,6 +42,7 @@ class ArtboxBasket { | @@ -41,6 +42,7 @@ class ArtboxBasket { | ||
41 | } | 42 | } |
42 | 43 | ||
43 | public init(update_modal, update_cart) { | 44 | public init(update_modal, update_cart) { |
45 | + this.deleteBonus(); | ||
44 | $.get(this.getLanguagePath() + this._settings['url'], function (data) { | 46 | $.get(this.getLanguagePath() + this._settings['url'], function (data) { |
45 | this._items = data.basket; | 47 | this._items = data.basket; |
46 | if (update_modal) { | 48 | if (update_modal) { |
@@ -100,6 +102,11 @@ class ArtboxBasket { | @@ -100,6 +102,11 @@ class ArtboxBasket { | ||
100 | cart.html(cart_html); | 102 | cart.html(cart_html); |
101 | } | 103 | } |
102 | 104 | ||
105 | + public updateSmall(small_html) { | ||
106 | + let cart = $(this._settings['smallSelector']); | ||
107 | + cart.html(small_html); | ||
108 | + } | ||
109 | + | ||
103 | public hideBasket() { | 110 | public hideBasket() { |
104 | $(this._settings['modalSelector']) | 111 | $(this._settings['modalSelector']) |
105 | .animate( | 112 | .animate( |
@@ -137,4 +144,24 @@ class ArtboxBasket { | @@ -137,4 +144,24 @@ class ArtboxBasket { | ||
137 | return '/'; | 144 | return '/'; |
138 | } | 145 | } |
139 | } | 146 | } |
147 | + | ||
148 | + public setBonus(bonus) { | ||
149 | + return $.post(this.getLanguagePath() + this._settings['url'] + '/add-bonus?bonus=' + bonus, function (data) { | ||
150 | + this._items = data.basket; | ||
151 | + this.updateModal(data.modal, data.cart, true); | ||
152 | + this.updateSmall(data.small); | ||
153 | + }.bind(this), 'json').fail(function (xhr, status, error) { | ||
154 | + console.error(error); | ||
155 | + }); | ||
156 | + } | ||
157 | + | ||
158 | + public deleteBonus(){ | ||
159 | + return $.post(this.getLanguagePath() + this._settings['url'] + '/delete-bonus', function (data) { | ||
160 | + this._items = data.basket; | ||
161 | + this.updateModal(data.modal, data.cart, true); | ||
162 | + this.updateSmall(data.small); | ||
163 | + }.bind(this), 'json').fail(function (xhr, status, error) { | ||
164 | + console.error(error); | ||
165 | + }); | ||
166 | + } | ||
140 | } | 167 | } |
141 | \ No newline at end of file | 168 | \ No newline at end of file |