Commit b213b850acca3faea9ef53c0a44825efa9a68a48

Authored by Yarik
1 parent 183a715c

Basket

Showing 1 changed file with 33 additions and 9 deletions   Show diff stats
models/Basket.php
... ... @@ -33,28 +33,32 @@
33 33 }
34 34 parent::__construct($config);
35 35 }
36   -
  36 +
37 37 /**
38 38 * Increment product variant with $product_variant_id count by 1
39 39 *
40 40 * @param int $product_variant_id
  41 + * @param int $count
41 42 */
42   - public function add(int $product_variant_id)
  43 + public function add(int $product_variant_id, int $count)
43 44 {
44 45 $data = $this->getData();
45 46 if (array_key_exists($product_variant_id, $data)) {
46 47 if ($data[ $product_variant_id ][ 'count' ] <= 0) {
47   - $data[ $product_variant_id ] = 1;
  48 + $data[ $product_variant_id ][ 'count' ] = $count;
48 49 } else {
49   - $data[ $product_variant_id ][ 'count' ] += 1;
  50 + $data[ $product_variant_id ][ 'count' ] += $count;
50 51 }
51 52 } else {
52 53 if ($this->findModel($product_variant_id)) {
53 54 $data[ $product_variant_id ] = [
54   - 'count' => 1,
  55 + 'count' => $count,
55 56 ];
56 57 }
57 58 }
  59 + if($data[$product_variant_id]['count'] <= 0) {
  60 + unset($data[$product_variant_id]);
  61 + }
58 62 $this->setData($data);
59 63 }
60 64  
... ... @@ -64,7 +68,7 @@
64 68 * @param int $product_variant_id
65 69 * @param int $count
66 70 */
67   - private function set(int $product_variant_id, int $count)
  71 + public function set(int $product_variant_id, int $count)
68 72 {
69 73 $data = $this->getData();
70 74 if (array_key_exists($product_variant_id, $data)) {
... ... @@ -141,6 +145,22 @@
141 145 }
142 146  
143 147 /**
  148 + * Get sum of product variants in basket
  149 + *
  150 + * @return float
  151 + */
  152 + public function getSum(): float
  153 + {
  154 + $data = $this->getData();
  155 + $models = $this->findModels(array_keys($data));
  156 + $sum = 0;
  157 + foreach ($models as $model) {
  158 + $sum += $model->price * $data[$model->id]['count'];
  159 + }
  160 + return $sum;
  161 + }
  162 +
  163 + /**
144 164 * Find Product Variant by $product_variant_id
145 165 *
146 166 * @param int $product_variant_id
... ... @@ -155,7 +175,7 @@
155 175 */
156 176 $model = ProductVariant::find()
157 177 ->where([ 'product_variant.id' => $product_variant_id ])
158   - ->joinWith('lang', true, 'INNER JOIN')
  178 + ->joinWith('lang')
159 179 ->one();
160 180 if (empty( $model )) {
161 181 throw new NotFoundHttpException(\Yii::t('app', 'Product not found'));
... ... @@ -173,15 +193,19 @@
173 193 */
174 194 public function findModels(array $product_variant_ids)
175 195 {
  196 + if (empty( $product_variant_ids )) {
  197 + return [];
  198 + }
176 199 return ProductVariant::find()
177 200 ->where([ 'product_variant.id' => $product_variant_ids ])
178   - ->joinWith('lang', true, 'INNER JOIN')
  201 + ->joinWith('lang', 'product')
179 202 ->with(
180 203 [
181   - 'product',
  204 + 'product.lang',
182 205 'image',
183 206 ]
184 207 )
  208 + ->indexBy('id')
185 209 ->all();
186 210 }
187 211  
... ...