Commit 66ff49d7ed230119efe817339056d030ee3daa69
1 parent
8e356b8c
-Sizes alpha test
Showing
5 changed files
with
95 additions
and
71 deletions
Show diff stats
controllers/BrandSizeController.php
... | ... | @@ -8,6 +8,7 @@ |
8 | 8 | use artweb\artbox\ecommerce\models\BrandSize; |
9 | 9 | use artweb\artbox\ecommerce\models\BrandSizeSearch; |
10 | 10 | use yii\helpers\ArrayHelper; |
11 | + use yii\helpers\VarDumper; | |
11 | 12 | use yii\web\Controller; |
12 | 13 | use yii\web\NotFoundHttpException; |
13 | 14 | use yii\filters\VerbFilter; |
... | ... | @@ -126,7 +127,7 @@ |
126 | 127 | public function actionUpdate($id) |
127 | 128 | { |
128 | 129 | $model = $this->findModel($id); |
129 | - | |
130 | + | |
130 | 131 | $categories = ArrayHelper::map( |
131 | 132 | Category::find() |
132 | 133 | ->with('lang') | ... | ... |
models/BrandSize.php
... | ... | @@ -69,7 +69,7 @@ class BrandSize extends \yii\db\ActiveRecord |
69 | 69 | */ |
70 | 70 | public function getBrand() |
71 | 71 | { |
72 | - return $this->hasOne(Brand::className(), ['id' => 'brand_id']); | |
72 | + return $this->hasOne(Brand::className(), ['id' => 'brand_id'])->with('lang'); | |
73 | 73 | } |
74 | 74 | |
75 | 75 | /** | ... | ... |
models/Product.php
... | ... | @@ -105,7 +105,7 @@ |
105 | 105 | class Product extends ActiveRecord |
106 | 106 | { |
107 | 107 | public $option_id; |
108 | - | |
108 | + | |
109 | 109 | public $min; |
110 | 110 | public $max; |
111 | 111 | |
... | ... | @@ -283,20 +283,17 @@ |
283 | 283 | { |
284 | 284 | return $this->getVariant(); |
285 | 285 | } |
286 | - | |
287 | - | |
288 | - | |
289 | - | |
290 | - | |
286 | + | |
291 | 287 | /** |
292 | 288 | * @return \yii\db\ActiveQuery |
293 | 289 | */ |
294 | - public function getEvents(){ | |
295 | - return $this->hasMany(Event::className(), ['id' => 'event_id'])->viaTable('events_to_products', ['product_id' => 'id'])->where(['status'=>Event::ACTIVE]); | |
290 | + public function getEvents() | |
291 | + { | |
292 | + return $this->hasMany(Event::className(), [ 'id' => 'event_id' ]) | |
293 | + ->viaTable('events_to_products', [ 'product_id' => 'id' ]) | |
294 | + ->where([ 'status' => Event::ACTIVE ]); | |
296 | 295 | } |
297 | - | |
298 | - | |
299 | - | |
296 | + | |
300 | 297 | /** |
301 | 298 | * Get ProductVariant query fetching only available in stock to current Product |
302 | 299 | * |
... | ... | @@ -332,21 +329,20 @@ |
332 | 329 | ] |
333 | 330 | ); |
334 | 331 | } |
335 | - | |
336 | - | |
337 | - public function getMaxPrice(){ | |
332 | + | |
333 | + public function getMaxPrice() | |
334 | + { | |
338 | 335 | $price = 0; |
339 | - if (!empty( $this->enabledVariants )) { | |
340 | - foreach ( $this->enabledVariants as $variant){ | |
341 | - if($variant->price > $price){ | |
336 | + if (!empty($this->enabledVariants)) { | |
337 | + foreach ($this->enabledVariants as $variant) { | |
338 | + if ($variant->price > $price) { | |
342 | 339 | $price = $variant->price; |
343 | 340 | } |
344 | 341 | } |
345 | 342 | } |
346 | 343 | return $price; |
347 | 344 | } |
348 | - | |
349 | - | |
345 | + | |
350 | 346 | /** |
351 | 347 | * Get random ProductVariant price or 0 if not exist |
352 | 348 | * |
... | ... | @@ -357,7 +353,7 @@ |
357 | 353 | */ |
358 | 354 | public function getVariantPrice(bool $exception = false): float |
359 | 355 | { |
360 | - if (!empty( $this->variant )) { | |
356 | + if (!empty($this->variant)) { | |
361 | 357 | return $this->variant->price; |
362 | 358 | } elseif ($exception) { |
363 | 359 | throw new NotFoundHttpException('Product with ID ' . $this->id . ' hasn\'t got variants'); |
... | ... | @@ -376,7 +372,7 @@ |
376 | 372 | */ |
377 | 373 | public function getEnabledVariantPrice(bool $exception = false): float |
378 | 374 | { |
379 | - if (!empty( $this->enabledVariant )) { | |
375 | + if (!empty($this->enabledVariant)) { | |
380 | 376 | return $this->enabledVariant->price; |
381 | 377 | } elseif ($exception) { |
382 | 378 | throw new NotFoundHttpException('Product with ID ' . $this->id . ' hasn\'t got enabled variants'); |
... | ... | @@ -384,39 +380,53 @@ |
384 | 380 | return 0; |
385 | 381 | } |
386 | 382 | } |
387 | - | |
388 | 383 | |
389 | 384 | /** |
390 | 385 | * Get Product name concatenated with Brand name |
391 | 386 | * |
392 | 387 | * @return string |
393 | 388 | */ |
394 | - public function getFullName():string | |
389 | + public function getFullName(): string | |
395 | 390 | { |
396 | 391 | $name = ''; |
397 | - $groupName = (new Query())->select(['tax_option.*','tax_option_lang.*']) | |
398 | - ->from([ 'tax_option' ]) | |
399 | - ->innerJoin('tax_group', 'tax_group.id = tax_option.tax_group_id') | |
400 | - ->innerJoin('tax_option_lang', 'tax_option.id = tax_option_lang.tax_option_id') | |
401 | - ->innerJoin('tax_group_to_category', 'tax_group.id = tax_group_to_category.tax_group_id') | |
402 | - ->where(['tax_group_to_category.category_id' => $this->category->id, 'tax_group.use_in_name'=>1, 'tax_option.id' => ArrayHelper::getColumn($this->options,'id')]) | |
403 | - ->one(); | |
404 | - | |
405 | - | |
406 | - if($groupName != null ){ | |
407 | - $groupName = $groupName['value']; | |
392 | + $groupName = ( new Query() )->select( | |
393 | + [ | |
394 | + 'tax_option.*', | |
395 | + 'tax_option_lang.*', | |
396 | + ] | |
397 | + ) | |
398 | + ->from([ 'tax_option' ]) | |
399 | + ->innerJoin('tax_group', 'tax_group.id = tax_option.tax_group_id') | |
400 | + ->innerJoin('tax_option_lang', 'tax_option.id = tax_option_lang.tax_option_id') | |
401 | + ->innerJoin( | |
402 | + 'tax_group_to_category', | |
403 | + 'tax_group.id = tax_group_to_category.tax_group_id' | |
404 | + ) | |
405 | + ->where( | |
406 | + [ | |
407 | + 'tax_group_to_category.category_id' => $this->category->id, | |
408 | + 'tax_group.use_in_name' => 1, | |
409 | + 'tax_option.id' => ArrayHelper::getColumn( | |
410 | + $this->options, | |
411 | + 'id' | |
412 | + ), | |
413 | + ] | |
414 | + ) | |
415 | + ->one(); | |
416 | + | |
417 | + if ($groupName != NULL) { | |
418 | + $groupName = $groupName[ 'value' ]; | |
408 | 419 | } else { |
409 | 420 | $groupName = ''; |
410 | 421 | } |
411 | - | |
412 | - | |
413 | - if (!empty( $this->category->lang->category_synonym )) { | |
414 | - $name = $name . ($groupName ? $groupName : $this->category->lang->category_synonym) . ' '; | |
422 | + | |
423 | + if (!empty($this->category->lang->category_synonym)) { | |
424 | + $name = $name . ( $groupName ? $groupName : $this->category->lang->category_synonym ) . ' '; | |
415 | 425 | } else { |
416 | - $name = $name . ($groupName ? $groupName : $this->category->lang->title) . ' '; | |
426 | + $name = $name . ( $groupName ? $groupName : $this->category->lang->title ) . ' '; | |
417 | 427 | } |
418 | - | |
419 | - if (!empty( $this->brand )) { | |
428 | + | |
429 | + if (!empty($this->brand)) { | |
420 | 430 | $name = $name . $this->brand->lang->title . ' '; |
421 | 431 | } |
422 | 432 | $name .= $this->lang->title; |
... | ... | @@ -485,11 +495,10 @@ |
485 | 495 | public function getOptions() |
486 | 496 | { |
487 | 497 | return $this->hasMany(TaxOption::className(), [ 'id' => 'option_id' ]) |
488 | - ->viaTable('product_option', [ 'product_id' => 'id' ]); | |
489 | - | |
498 | + ->viaTable('product_option', [ 'product_id' => 'id' ]); | |
499 | + | |
490 | 500 | } |
491 | - | |
492 | - | |
501 | + | |
493 | 502 | /** |
494 | 503 | * Get TaxOptions query for current Product that will by used in filter |
495 | 504 | * |
... | ... | @@ -497,13 +506,17 @@ |
497 | 506 | */ |
498 | 507 | public function getFilterOptions() |
499 | 508 | { |
500 | - | |
509 | + | |
501 | 510 | return $this->getOptions() |
502 | - ->joinWith(['lang','taxGroup']) | |
503 | - ->where(['is_filter' => true]); | |
511 | + ->joinWith( | |
512 | + [ | |
513 | + 'lang', | |
514 | + 'taxGroup', | |
515 | + ] | |
516 | + ) | |
517 | + ->where([ 'is_filter' => true ]); | |
504 | 518 | } |
505 | - | |
506 | - | |
519 | + | |
507 | 520 | /** |
508 | 521 | * Get TaxGroup for current Product |
509 | 522 | * |
... | ... | @@ -511,11 +524,10 @@ |
511 | 524 | */ |
512 | 525 | public function getGroups() |
513 | 526 | { |
514 | - return $this->hasMany(TaxGroup::className(), [ 'id' => 'tax_group_id' ]) | |
515 | - ->via('options'); | |
527 | + return $this->hasMany(TaxGroup::className(), [ 'id' => 'tax_group_id' ]) | |
528 | + ->via('options'); | |
516 | 529 | } |
517 | - | |
518 | - | |
530 | + | |
519 | 531 | /** |
520 | 532 | * Get TaxOptions query for current Product joined with TaxGroups |
521 | 533 | * |
... | ... | @@ -552,7 +564,7 @@ |
552 | 564 | /** |
553 | 565 | * @var TaxGroup $group |
554 | 566 | */ |
555 | - if (!empty( $options[ $group->id ] )) { | |
567 | + if (!empty($options[ $group->id ])) { | |
556 | 568 | $group->customOptions = $options[ $group->id ]; |
557 | 569 | $groups[] = $group; |
558 | 570 | } |
... | ... | @@ -593,7 +605,7 @@ |
593 | 605 | * @see Product::getProductStocks() |
594 | 606 | * @return int |
595 | 607 | */ |
596 | - public function getQuantity():int | |
608 | + public function getQuantity(): int | |
597 | 609 | { |
598 | 610 | return $this->getProductStocks() |
599 | 611 | ->sum('quantity'); |
... | ... | @@ -607,10 +619,8 @@ |
607 | 619 | public function afterSave($insert, $changedAttributes) |
608 | 620 | { |
609 | 621 | parent::afterSave($insert, $changedAttributes); |
610 | - | |
611 | - | |
612 | - | |
613 | - if (!empty( $this->categories )) { | |
622 | + | |
623 | + if (!empty($this->categories)) { | |
614 | 624 | $categories = Category::findAll($this->categories); |
615 | 625 | $this->unlinkAll('categories', true); |
616 | 626 | foreach ($categories as $category) { |
... | ... | @@ -618,7 +628,7 @@ |
618 | 628 | } |
619 | 629 | } |
620 | 630 | |
621 | - if (!empty( $this->options )) { | |
631 | + if (!empty($this->options)) { | |
622 | 632 | $options = TaxOption::findAll($this->options); |
623 | 633 | $this->unlinkAll('options', true); |
624 | 634 | foreach ($options as $option) { |
... | ... | @@ -664,7 +674,20 @@ |
664 | 674 | |
665 | 675 | public function getSize() |
666 | 676 | { |
667 | - $size = BrandSize::find()->joinWith('categories')->createCommand()->rawSql; | |
677 | + $subQuery = Product::find() | |
678 | + ->select('category.id as x') | |
679 | + ->joinWith('categories') | |
680 | + ->where([ 'product.id' => $this->id, ]); | |
681 | + $size = BrandSize::find() | |
682 | + ->joinWith('categories') | |
683 | + ->innerJoin([ 's' => $subQuery ], 's.x = category.id') | |
684 | + ->where( | |
685 | + [ | |
686 | + 'brand_size.brand_id' => $this->brand->id, | |
687 | + ] | |
688 | + ) | |
689 | + ->one(); | |
690 | + return $size; | |
668 | 691 | } |
669 | 692 | |
670 | 693 | /** |
... | ... | @@ -686,17 +709,17 @@ |
686 | 709 | { |
687 | 710 | $this->options = $values; |
688 | 711 | } |
689 | - | |
712 | + | |
690 | 713 | public function beforeSave($insert) |
691 | 714 | { |
692 | 715 | if (parent::beforeSave($insert)) { |
693 | - | |
716 | + | |
694 | 717 | return true; |
695 | 718 | } else { |
696 | 719 | return false; |
697 | 720 | } |
698 | 721 | } |
699 | - | |
722 | + | |
700 | 723 | public function beforeDelete() |
701 | 724 | { |
702 | 725 | if (parent::beforeDelete()) { | ... | ... |
views/brand-size/index.php