Commit e9f291a5e732fbe370433e83f9584ad442c2b80c

Authored by Karnovsky A
1 parent 2e9ea16e

Similar products widget

common/modules/product/helpers/ProductHelper.php
... ... @@ -99,4 +99,39 @@ class ProductHelper extends Object {
99 99 }
100 100 return Product::find()->joinWith('variants')->where($data)->andWhere(['!=', ProductVariant::tableName() .'.stock', 0])->limit($count)/*->orderBy($sort)*/->all();
101 101 }
  102 +
  103 + public static function getSimilarProducts($product, $count = 10) {
  104 + if (!is_object($product)) {
  105 + $product = Product::findOne($product);
  106 + }
  107 +
  108 + if (!$product->properties) {
  109 + return [];
  110 + }
  111 + $query = Product::find()
  112 + ->select('product.product_id')
  113 + ->innerJoinWith('variant')
  114 + ->where(['!=', 'product_variant.stock', 0]);
  115 +// $query->andWhere(['>=', 'product_variant.price', $product->enabledVariant->price * 0.7]);
  116 +// $query->andWhere(['<=', 'product_variant.price', $product->enabledVariant->price * 1.3]);
  117 + foreach($product->properties as $group) {
  118 + $where = [];
  119 + foreach ($group->_options as $option) {
  120 + $where[] = $option->tax_option_id;
  121 + }
  122 + if (!$where) {
  123 + continue;
  124 + }
  125 + $query->innerJoin('product_option to'. $group->tax_group_id, 'to'. $group->tax_group_id .'.product_id = product.product_id');
  126 + $query->andWhere(['to'. $group->tax_group_id .'.option_id' => $where]);
  127 + }
  128 + $query->andWhere(['!=', 'product.product_id', $product->product_id]);
  129 + $query->groupBy('product.product_id');
  130 + $query->limit($count);
  131 + $products = $query->asArray()->all();
  132 + foreach ($products as &$_product) {
  133 + $_product = Product::findOne($_product['product_id']);
  134 + }
  135 + return $products;
  136 + }
102 137 }
103 138 \ No newline at end of file
... ...
common/modules/product/models/Product.php
... ... @@ -27,6 +27,9 @@ use yii\web\UploadedFile;
27 27 * @property boolean $is_top
28 28 * @property boolean $is_new
29 29 * @property boolean $akciya
  30 + * @property array $properties
  31 + * @property ProductVariant $enabledVariant
  32 + * @property array $enabledVariants
30 33 */
31 34 class Product extends \yii\db\ActiveRecord
32 35 {
... ... @@ -159,6 +162,7 @@ class Product extends \yii\db\ActiveRecord
159 162 {
160 163 return $this->hasOne(ProductVariant::className(), ['product_id' => 'product_id'])->andOnCondition(['!=', ProductVariant::tableName() .'.stock', 0]);
161 164 }
  165 +
162 166 public function getVariantPrice() {
163 167 return $this->variant->price;
164 168 }
... ...
common/modules/product/widgets/similarProducts.php 0 → 100644
  1 +<?php
  2 +
  3 +namespace common\modules\product\widgets;
  4 +
  5 +use common\modules\product\helpers\ProductHelper;
  6 +use common\modules\product\models\Category;
  7 +use yii\base\Widget;
  8 +use Yii;
  9 +
  10 +class similarProducts extends Widget {
  11 + public $count = 10;
  12 +
  13 + public $title;
  14 +
  15 + public $product;
  16 +
  17 + public function init()
  18 + {
  19 + parent::init(); // TODO: Change the autogenerated stub
  20 + }
  21 +
  22 + public function run() {
  23 + $products = ProductHelper::getSimilarProducts($this->product, $this->count);
  24 +
  25 + if (!$this->title) {
  26 + $this->title = Yii::t('product', 'Similar products');
  27 + }
  28 +
  29 + return $this->render('products_block', [
  30 + 'title' => $this->title,
  31 + 'class' => 'similar-products',
  32 + 'products' => $products,
  33 + ]);
  34 + }
  35 +}
0 36 \ No newline at end of file
... ...
common/translation/ru/product.php
... ... @@ -11,6 +11,7 @@ return [
11 11 'Promo products' => 'Акционные товары',
12 12 'New products' => 'Новинки',
13 13 'Top products' => 'Популярные',
  14 + 'Similar products' => 'Похожие товары',
14 15 'Brands' => 'Бренды',
15 16 'Brand' => 'Бренд',
16 17 'Categories' => 'Категории',
... ...
composer.json
... ... @@ -35,6 +35,7 @@
35 35 "unclead/yii2-multiple-input": "~1.0",
36 36 "codeception/codeception":"*",
37 37 "phpmailer/phpmailer": "^5.2",
  38 + "league/oauth2-client": "^1.3",
38 39 "kartik-v/yii2-grid": "@dev",
39 40 "kartik-v/yii2-mpdf": "@dev",
40 41 "kartik-v/yii2-widget-fileinput": "@dev",
... ...
frontend/views/catalog/product.php
... ... @@ -184,6 +184,7 @@ $this-&gt;registerJs (&quot;
184 184 </div>
185 185 <div class="both"></div>
186 186  
  187 + <?= \common\modules\product\widgets\similarProducts::widget(['product' => $product])?>
187 188 <?= \common\modules\product\widgets\specialProducts::widget(['type' => 'promo'])?>
188 189 <?= \common\modules\product\widgets\specialProducts::widget(['type' => 'new'])?>
189 190 <?= \common\modules\product\widgets\specialProducts::widget(['type' => 'top'])?>
... ...