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 | 137 | ); |
138 | 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 | 316 | { |
317 | 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 | 345 | \ No newline at end of file | ... | ... |
web/js/artbox_basket.js
1 | 1 | ///<reference path="node_modules/@types/jquery/index.d.ts" /> |
2 | -var ArtboxBasket = (function () { | |
2 | +var ArtboxBasket = /** @class */ (function () { | |
3 | 3 | function ArtboxBasket(settings) { |
4 | 4 | if (settings === void 0) { settings = {}; } |
5 | 5 | this._defaults = { |
... | ... | @@ -7,7 +7,8 @@ var ArtboxBasket = (function () { |
7 | 7 | url: 'basket', |
8 | 8 | initError: 'Basket cannot be init', |
9 | 9 | modalSelector: '.basket_modal', |
10 | - cartSelector: '.basket_wrapper' | |
10 | + cartSelector: '.basket_wrapper', | |
11 | + smallSelector: '.small_basket_page' | |
11 | 12 | }; |
12 | 13 | this._settings = {}; |
13 | 14 | this._settings = ArtboxBasket.mergeObjects(this._defaults, settings); |
... | ... | @@ -46,6 +47,7 @@ var ArtboxBasket = (function () { |
46 | 47 | configurable: true |
47 | 48 | }); |
48 | 49 | ArtboxBasket.prototype.init = function (update_modal, update_cart) { |
50 | + this.deleteBonus(); | |
49 | 51 | $.get(this.getLanguagePath() + this._settings['url'], function (data) { |
50 | 52 | this._items = data.basket; |
51 | 53 | if (update_modal) { |
... | ... | @@ -100,6 +102,10 @@ var ArtboxBasket = (function () { |
100 | 102 | var cart = $(this._settings['cartSelector']); |
101 | 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 | 109 | ArtboxBasket.prototype.hideBasket = function () { |
104 | 110 | $(this._settings['modalSelector']) |
105 | 111 | .animate({ |
... | ... | @@ -137,5 +143,23 @@ var ArtboxBasket = (function () { |
137 | 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 | 164 | return ArtboxBasket; |
141 | 165 | }()); | ... | ... |
web/js/artbox_basket.ts
... | ... | @@ -7,7 +7,8 @@ class ArtboxBasket { |
7 | 7 | url: 'basket', |
8 | 8 | initError: 'Basket cannot be init', |
9 | 9 | modalSelector: '.basket_modal', |
10 | - cartSelector: '.basket_wrapper' | |
10 | + cartSelector: '.basket_wrapper', | |
11 | + smallSelector: '.small_basket_page' | |
11 | 12 | }; |
12 | 13 | private _settings: Object = {}; |
13 | 14 | |
... | ... | @@ -41,6 +42,7 @@ class ArtboxBasket { |
41 | 42 | } |
42 | 43 | |
43 | 44 | public init(update_modal, update_cart) { |
45 | + this.deleteBonus(); | |
44 | 46 | $.get(this.getLanguagePath() + this._settings['url'], function (data) { |
45 | 47 | this._items = data.basket; |
46 | 48 | if (update_modal) { |
... | ... | @@ -100,6 +102,11 @@ class ArtboxBasket { |
100 | 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 | 110 | public hideBasket() { |
104 | 111 | $(this._settings['modalSelector']) |
105 | 112 | .animate( |
... | ... | @@ -137,4 +144,24 @@ class ArtboxBasket { |
137 | 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 | 168 | \ No newline at end of file | ... | ... |