Blame view

models/Basket.php 8.24 KB
8a7e6ecf   Yarik   Namespaces
1
2
3
4
5
  <?php
      
      namespace artweb\artbox\ecommerce\models;
      
      use yii\base\Component;
363d62a0   Yarik   Added basket cookies
6
7
      use yii\helpers\Json;
      use yii\web\Cookie;
8a7e6ecf   Yarik   Namespaces
8
9
10
11
12
      use yii\web\NotFoundHttpException;
      
      /**
       * Class Basket to work with basket
       *
015cc683   Yarik   Credits v2
13
       * @property bool isCredit
8a7e6ecf   Yarik   Namespaces
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
       * @package artweb\artbox\ecommerce\models
       */
      class Basket extends Component
      {
          /**
           * Session object
           *
           * @var \yii\web\Session
           */
          public $session;
          
          /**
           * Basket constructor.
           * Check for basket variable in session and set it to empty array if not exist.
           *
           * @param array $config
           */
          public function __construct(array $config = [])
          {
              $this->session = \Yii::$app->session;
              if (!$this->session->has('basket')) {
                  $this->session->set('basket', []);
015cc683   Yarik   Credits v2
36
              } elseif (!empty( $this->session->get('basket') )) {
363d62a0   Yarik   Added basket cookies
37
38
39
40
41
42
43
44
45
                  $cookies = \Yii::$app->response->cookies;
                  $cookies->add(
                      new Cookie(
                          [
                              'name'  => 'basket',
                              'value' => Json::encode($this->session->get('basket')),
                          ]
                      )
                  );
8a7e6ecf   Yarik   Namespaces
46
47
48
              }
              parent::__construct($config);
          }
04e8796d   Yarik   Not in stock
49
          
8a7e6ecf   Yarik   Namespaces
50
51
52
53
          /**
           * Increment product variant with $product_variant_id count by 1
           *
           * @param int $product_variant_id
b213b850   Yarik   Basket
54
           * @param int $count
8a7e6ecf   Yarik   Namespaces
55
           */
b213b850   Yarik   Basket
56
          public function add(int $product_variant_id, int $count)
8a7e6ecf   Yarik   Namespaces
57
58
59
60
          {
              $data = $this->getData();
              if (array_key_exists($product_variant_id, $data)) {
                  if ($data[ $product_variant_id ][ 'count' ] <= 0) {
b213b850   Yarik   Basket
61
                      $data[ $product_variant_id ][ 'count' ] = $count;
8a7e6ecf   Yarik   Namespaces
62
                  } else {
b213b850   Yarik   Basket
63
                      $data[ $product_variant_id ][ 'count' ] += $count;
8a7e6ecf   Yarik   Namespaces
64
65
66
67
                  }
              } else {
                  if ($this->findModel($product_variant_id)) {
                      $data[ $product_variant_id ] = [
b213b850   Yarik   Basket
68
                          'count' => $count,
8a7e6ecf   Yarik   Namespaces
69
70
71
                      ];
                  }
              }
04e8796d   Yarik   Not in stock
72
73
              if ($data[ $product_variant_id ][ 'count' ] <= 0) {
                  unset( $data[ $product_variant_id ] );
b213b850   Yarik   Basket
74
              }
8a7e6ecf   Yarik   Namespaces
75
76
77
78
79
80
81
82
83
              $this->setData($data);
          }
          
          /**
           * Set product variant with $product_variant_id to $count
           *
           * @param int $product_variant_id
           * @param int $count
           */
b213b850   Yarik   Basket
84
          public function set(int $product_variant_id, int $count)
8a7e6ecf   Yarik   Namespaces
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
          {
              $data = $this->getData();
              if (array_key_exists($product_variant_id, $data)) {
                  $data[ $product_variant_id ][ 'count' ] = $count;
                  if ($data[ $product_variant_id ][ 'count' ] <= 0) {
                      unset( $data[ $product_variant_id ] );
                  }
              } elseif ($count > 0) {
                  if ($this->findModel($product_variant_id)) {
                      $data[ $product_variant_id ] = [
                          'count' => $count,
                      ];
                  }
              }
              $this->setData($data);
          }
          
          /**
           * Delete product variant with $product_variant_id from basket
           *
           * @param int $product_variant_id
           */
          public function delete(int $product_variant_id)
          {
              $this->set($product_variant_id, 0);
          }
          
          /**
           * Get basket data
           *
           * @return array
           */
          public function getData(): array
          {
              return $this->session->get('basket');
          }
          
          /**
           * Get basket item with $product_variant_id. Returns false if item not in basket.
           *
           * @param int $product_variant_id
           *
           * @return bool|array
           */
          public function getItem(int $product_variant_id)
          {
              $data = $this->getData();
              if (!empty( $data[ $product_variant_id ] )) {
                  return $data[ $product_variant_id ];
              } else {
                  return false;
              }
          }
          
          /**
           * Set basket data
           *
           * @param array $data
           */
          public function setData(array $data)
          {
              $this->session->set('basket', $data);
363d62a0   Yarik   Added basket cookies
147
              $cookies = \Yii::$app->response->cookies;
015cc683   Yarik   Credits v2
148
              if (empty( $data )) {
363d62a0   Yarik   Added basket cookies
149
                  $cookies->remove('basket');
015cc683   Yarik   Credits v2
150
                  $cookies->remove('isCredit');
363d62a0   Yarik   Added basket cookies
151
152
153
154
155
156
157
158
159
160
              } else {
                  $cookies->add(
                      new Cookie(
                          [
                              'name'  => 'basket',
                              'value' => Json::encode($data),
                          ]
                      )
                  );
              }
8a7e6ecf   Yarik   Namespaces
161
162
163
164
165
166
167
168
169
170
171
172
173
174
          }
          
          /**
           * Get count of product variants in basket
           *
           * @return int
           */
          public function getCount(): int
          {
              $data = $this->getData();
              return count($data);
          }
          
          /**
b213b850   Yarik   Basket
175
176
177
178
179
180
181
182
183
184
           * Get sum of product variants in basket
           *
           * @return float
           */
          public function getSum(): float
          {
              $data = $this->getData();
              $models = $this->findModels(array_keys($data));
              $sum = 0;
              foreach ($models as $model) {
04e8796d   Yarik   Not in stock
185
                  $sum += $model->price * $data[ $model->id ][ 'count' ];
b213b850   Yarik   Basket
186
187
188
189
190
              }
              return $sum;
          }
          
          /**
8a7e6ecf   Yarik   Namespaces
191
192
193
194
195
196
197
198
199
200
201
202
203
204
           * Find Product Variant by $product_variant_id
           *
           * @param int $product_variant_id
           *
           * @return \artweb\artbox\ecommerce\models\ProductVariant
           * @throws \yii\web\NotFoundHttpException
           */
          public function findModel(int $product_variant_id): ProductVariant
          {
              /**
               * @var ProductVariant $model
               */
              $model = ProductVariant::find()
                                     ->where([ 'product_variant.id' => $product_variant_id ])
04e8796d   Yarik   Not in stock
205
206
207
208
209
210
211
                                     ->andWhere(
                                         [
                                             '>',
                                             'product_variant.stock',
                                             0,
                                         ]
                                     )
b213b850   Yarik   Basket
212
                                     ->joinWith('lang')
8a7e6ecf   Yarik   Namespaces
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
                                     ->one();
              if (empty( $model )) {
                  throw new NotFoundHttpException(\Yii::t('app', 'Product not found'));
              } else {
                  return $model;
              }
          }
          
          /**
           * Find all Product Variants filtered by $product_variant_ids
           *
           * @param array $product_variant_ids
           *
           * @return ProductVariant[]
           */
          public function findModels(array $product_variant_ids)
          {
b213b850   Yarik   Basket
230
231
232
              if (empty( $product_variant_ids )) {
                  return [];
              }
8a7e6ecf   Yarik   Namespaces
233
234
              return ProductVariant::find()
                                   ->where([ 'product_variant.id' => $product_variant_ids ])
b213b850   Yarik   Basket
235
                                   ->joinWith('lang', 'product')
8a7e6ecf   Yarik   Namespaces
236
237
                                   ->with(
                                       [
b213b850   Yarik   Basket
238
                                           'product.lang',
8a7e6ecf   Yarik   Namespaces
239
240
241
                                           'image',
                                       ]
                                   )
b213b850   Yarik   Basket
242
                                   ->indexBy('id')
8a7e6ecf   Yarik   Namespaces
243
244
245
246
247
248
249
250
251
                                   ->all();
          }
          
          /**
           * Clear basket
           */
          public function clear()
          {
              $this->setData([]);
015cc683   Yarik   Credits v2
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
              \Yii::$app->response->cookies->remove('isCredit');
          }
          
          /**
           * Check if is credit cookie flag set
           *
           * @return bool
           */
          public static function getIsCredit(): bool
          {
              // Get cookies from global in order to skip yii2 cookie validation
              $cookies = $_COOKIE;
              if (isset( $cookies[ 'isCredit' ] )) {
                  return true;
              } else {
                  return false;
              }
8a7e6ecf   Yarik   Namespaces
269
270
271
272
          }
          
      }