Blame view

common/modules/product/helpers/ProductHelper.php 5.53 KB
3f2bc3d0   Administrator   first commit
1
2
3
4
5
6
7
  <?php

  

  namespace common\modules\product\helpers;

  

  use common\modules\product\models\Brand;

  use common\modules\product\models\Category;

  use common\modules\product\models\Product;

d48d8bc0   Karnovsky A   -
8
  use common\modules\product\models\ProductVariant;

3f2bc3d0   Administrator   first commit
9
10
11
12
  use yii\base\Object;

  use Yii;

  

  class ProductHelper extends Object {

c7852657   Karnovsky A   -
13
14
15
16
17
18
19
      const PRODUCT_TAX_GROUP_ID_TARGET = 20;

      const PRODUCT_TAX_GROUP_ID_YEAR = 21;

      const PRODUCT_TAX_GROUP_ID_SEX = 22;

  

      const PRODUCT_VARIANT_TYPE_COLOR = 1;

      const PRODUCT_VARIANT_TYPE_SIZE = 2;

  

3f2bc3d0   Administrator   first commit
20
      public static function getCategories() {

c7852657   Karnovsky A   -
21
          return Category::find()->getTree(); // with('categoryName')->

3f2bc3d0   Administrator   first commit
22
23
24
      }

  

      public static function getBrands() {

c7852657   Karnovsky A   -
25
          return Brand::find(); // ->with('brandName')

3f2bc3d0   Administrator   first commit
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
      }

  

      /*

       * Return custom filter-option link

       * @var array $filter

       * @var array $options

       * @return array

       */

      public static function getFilterForOption($filter, $key, $value, $remove = false) {

          $result = $filter;

          if (is_array($value)) {

              foreach($value as $value_key => $value_items) {

                  if (!is_array($value_items)) {

                      $value_items = [$value_items];

                  }

                  foreach($value_items as $value_item) {

                      if ($remove && isset($result[$key]) && ($i = array_search($value_item, $result[$key][$value_key])) !== FALSE) {

                          unset($result[$key][$value_key][$i]);

                          if (empty($result[$key][$value_key])) {

                              unset($result[$key][$value_key]);

                          }

                      } else {

                          if (!isset($result[$key][$value_key]) || array_search($value_item, $result[$key][$value_key]) === FALSE) {

                              $result[$key][$value_key][] = $value_item;

                          }

                      }

                  }

              }

          } else {

              if ($remove && isset($result[$key]) && ($i = array_search($value, $result[$key])) !== FALSE) {

                  unset($result[$key][$i]);

                  if (empty($result[$key])) {

                      unset($result[$key]);

                  }

              } else {

                  if (!isset($result[$key]) || array_search($value, $result[$key]) === FALSE) {

                      $result[$key][] = $value;

                  }

              }

          }

          return $result;

      }

  

      public static function addLastProsucts($product_id) {

          $last_products = self::getLastProducts();

          if (!in_array($product_id, $last_products)) {

2848e106   Karnovsky A   Fix last-products...
72
73
74
75
              $last_products[] = intval($product_id);

              if (count($last_products) > 16) {

                  array_shift($last_products);

              }

3f2bc3d0   Administrator   first commit
76
77
78
79
80
81
82
              Yii::$app->session->set('last_products', $last_products);

          }

      }

  

      public static function getLastProducts($as_object = false) {

          $last_products = Yii::$app->session->get('last_products', []);

          if ($as_object) {

b238d463   Karnovsky A   -
83
              $last_products = Product::find()->joinWith(['variant'])->where([Product::tableName() .'.product_id' => $last_products])->andWhere(['!=', ProductVariant::tableName() .'.stock', 0])->all();

3f2bc3d0   Administrator   first commit
84
          }

2848e106   Karnovsky A   Fix last-products...
85
          return array_reverse($last_products);

3f2bc3d0   Administrator   first commit
86
      }

dc2cd017   Karnovsky A   -
87
88
89
90
91
92
93
94
95
96
97
98
99
  

      public static function getSpecialProducts($type, $count, $sort = null) {

          switch($type) {

              case 'top':

                  $data = ['is_top' => true];

                  break;

              case 'new':

                  $data = ['is_new' => true];

                  break;

              case 'promo':

                  $data = ['akciya' => true];

                  break;

          }

d48d8bc0   Karnovsky A   -
100
          return Product::find()->joinWith('variants')->where($data)->andWhere(['!=', ProductVariant::tableName() .'.stock', 0])->limit($count)/*->orderBy($sort)*/->all();

dc2cd017   Karnovsky A   -
101
      }

e9f291a5   Karnovsky A   Similar products ...
102
103
104
105
106
107
108
109
110
  

      public static function getSimilarProducts($product, $count = 10) {

          if (!is_object($product)) {

              $product = Product::findOne($product);

          }

  

          if (!$product->properties) {

              return [];

          }

eaaf879f   Karnovsky A   Similar products ...
111
112
113
114
          $product_categories = [];

          foreach ($product->categories as $category) {

              $product_categories[] = $category->category_id;

          }

e9f291a5   Karnovsky A   Similar products ...
115
116
117
          $query = Product::find()

              ->select('product.product_id')

              ->innerJoinWith('variant')

eaaf879f   Karnovsky A   Similar products ...
118
119
120
              ->joinWith('category')

              ->where(['!=', 'product_variant.stock', 0])

              ->andWhere(['product_category.category_id' => $product_categories]);

e9f291a5   Karnovsky A   Similar products ...
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
  //        $query->andWhere(['>=', 'product_variant.price', $product->enabledVariant->price * 0.7]);

  //        $query->andWhere(['<=', 'product_variant.price', $product->enabledVariant->price * 1.3]);

          foreach($product->properties as $group) {

              $where = [];

              foreach ($group->_options as $option) {

                  $where[] = $option->tax_option_id;

              }

              if (!$where) {

                  continue;

              }

              $query->innerJoin('product_option to'. $group->tax_group_id, 'to'. $group->tax_group_id .'.product_id = product.product_id');

              $query->andWhere(['to'. $group->tax_group_id .'.option_id' => $where]);

          }

          $query->andWhere(['!=', 'product.product_id', $product->product_id]);

          $query->groupBy('product.product_id');

          $query->limit($count);

          $products = $query->asArray()->all();

          foreach ($products as &$_product) {

              $_product = Product::findOne($_product['product_id']);

          }

          return $products;

      }

3f2bc3d0   Administrator   first commit
143
  }