Blame view

models/Basket.php 6.46 KB
8a7e6ecf   Yarik   Namespaces
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
  <?php
      
      namespace artweb\artbox\ecommerce\models;
      
      use yii\base\Component;
      use yii\web\NotFoundHttpException;
      
      /**
       * Class Basket to work with basket
       *
       * @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', []);
              }
              parent::__construct($config);
          }
b213b850   Yarik   Basket
36
      
8a7e6ecf   Yarik   Namespaces
37
38
39
40
          /**
           * Increment product variant with $product_variant_id count by 1
           *
           * @param int $product_variant_id
b213b850   Yarik   Basket
41
           * @param int $count
8a7e6ecf   Yarik   Namespaces
42
           */
b213b850   Yarik   Basket
43
          public function add(int $product_variant_id, int $count)
8a7e6ecf   Yarik   Namespaces
44
45
46
47
          {
              $data = $this->getData();
              if (array_key_exists($product_variant_id, $data)) {
                  if ($data[ $product_variant_id ][ 'count' ] <= 0) {
b213b850   Yarik   Basket
48
                      $data[ $product_variant_id ][ 'count' ] = $count;
8a7e6ecf   Yarik   Namespaces
49
                  } else {
b213b850   Yarik   Basket
50
                      $data[ $product_variant_id ][ 'count' ] += $count;
8a7e6ecf   Yarik   Namespaces
51
52
53
54
                  }
              } else {
                  if ($this->findModel($product_variant_id)) {
                      $data[ $product_variant_id ] = [
b213b850   Yarik   Basket
55
                          'count' => $count,
8a7e6ecf   Yarik   Namespaces
56
57
58
                      ];
                  }
              }
b213b850   Yarik   Basket
59
60
61
              if($data[$product_variant_id]['count'] <= 0) {
                  unset($data[$product_variant_id]);
              }
8a7e6ecf   Yarik   Namespaces
62
63
64
65
66
67
68
69
70
              $this->setData($data);
          }
          
          /**
           * Set product variant with $product_variant_id to $count
           *
           * @param int $product_variant_id
           * @param int $count
           */
b213b850   Yarik   Basket
71
          public function set(int $product_variant_id, int $count)
8a7e6ecf   Yarik   Namespaces
72
73
74
75
76
77
78
79
80
81
82
83
84
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
147
          {
              $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);
          }
          
          /**
           * Get count of product variants in basket
           *
           * @return int
           */
          public function getCount(): int
          {
              $data = $this->getData();
              return count($data);
          }
          
          /**
b213b850   Yarik   Basket
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
           * 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) {
                  $sum += $model->price * $data[$model->id]['count'];
              }
              return $sum;
          }
          
          /**
8a7e6ecf   Yarik   Namespaces
164
165
166
167
168
169
170
171
172
173
174
175
176
177
           * 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 ])
b213b850   Yarik   Basket
178
                                     ->joinWith('lang')
8a7e6ecf   Yarik   Namespaces
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
                                     ->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
196
197
198
              if (empty( $product_variant_ids )) {
                  return [];
              }
8a7e6ecf   Yarik   Namespaces
199
200
              return ProductVariant::find()
                                   ->where([ 'product_variant.id' => $product_variant_ids ])
b213b850   Yarik   Basket
201
                                   ->joinWith('lang', 'product')
8a7e6ecf   Yarik   Namespaces
202
203
                                   ->with(
                                       [
b213b850   Yarik   Basket
204
                                           'product.lang',
8a7e6ecf   Yarik   Namespaces
205
206
207
                                           'image',
                                       ]
                                   )
b213b850   Yarik   Basket
208
                                   ->indexBy('id')
8a7e6ecf   Yarik   Namespaces
209
210
211
212
213
214
215
216
217
218
219
220
221
                                   ->all();
          }
          
          /**
           * Clear basket
           */
          public function clear()
          {
              $this->setData([]);
          }
          
      }