diff --git a/common/modules/product/helpers/ProductHelper.php b/common/modules/product/helpers/ProductHelper.php index 1e0eb83..04c063c 100755 --- a/common/modules/product/helpers/ProductHelper.php +++ b/common/modules/product/helpers/ProductHelper.php @@ -99,4 +99,39 @@ class ProductHelper extends Object { } return Product::find()->joinWith('variants')->where($data)->andWhere(['!=', ProductVariant::tableName() .'.stock', 0])->limit($count)/*->orderBy($sort)*/->all(); } + + public static function getSimilarProducts($product, $count = 10) { + if (!is_object($product)) { + $product = Product::findOne($product); + } + + if (!$product->properties) { + return []; + } + $query = Product::find() + ->select('product.product_id') + ->innerJoinWith('variant') + ->where(['!=', 'product_variant.stock', 0]); +// $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; + } } \ No newline at end of file diff --git a/common/modules/product/models/Product.php b/common/modules/product/models/Product.php index afe6dcc..af8ecb0 100755 --- a/common/modules/product/models/Product.php +++ b/common/modules/product/models/Product.php @@ -27,6 +27,9 @@ use yii\web\UploadedFile; * @property boolean $is_top * @property boolean $is_new * @property boolean $akciya + * @property array $properties + * @property ProductVariant $enabledVariant + * @property array $enabledVariants */ class Product extends \yii\db\ActiveRecord { @@ -159,6 +162,7 @@ class Product extends \yii\db\ActiveRecord { return $this->hasOne(ProductVariant::className(), ['product_id' => 'product_id'])->andOnCondition(['!=', ProductVariant::tableName() .'.stock', 0]); } + public function getVariantPrice() { return $this->variant->price; } diff --git a/common/modules/product/widgets/similarProducts.php b/common/modules/product/widgets/similarProducts.php new file mode 100644 index 0000000..aebd754 --- /dev/null +++ b/common/modules/product/widgets/similarProducts.php @@ -0,0 +1,35 @@ +product, $this->count); + + if (!$this->title) { + $this->title = Yii::t('product', 'Similar products'); + } + + return $this->render('products_block', [ + 'title' => $this->title, + 'class' => 'similar-products', + 'products' => $products, + ]); + } +} \ No newline at end of file diff --git a/common/translation/ru/product.php b/common/translation/ru/product.php index f2910c1..262de9a 100755 --- a/common/translation/ru/product.php +++ b/common/translation/ru/product.php @@ -11,6 +11,7 @@ return [ 'Promo products' => 'Акционные товары', 'New products' => 'Новинки', 'Top products' => 'Популярные', + 'Similar products' => 'Похожие товары', 'Brands' => 'Бренды', 'Brand' => 'Бренд', 'Categories' => 'Категории', diff --git a/composer.json b/composer.json index 2f74ae4..18d45ec 100755 --- a/composer.json +++ b/composer.json @@ -35,6 +35,7 @@ "unclead/yii2-multiple-input": "~1.0", "codeception/codeception":"*", "phpmailer/phpmailer": "^5.2", + "league/oauth2-client": "^1.3", "kartik-v/yii2-grid": "@dev", "kartik-v/yii2-mpdf": "@dev", "kartik-v/yii2-widget-fileinput": "@dev", diff --git a/frontend/views/catalog/product.php b/frontend/views/catalog/product.php index 3de62d1..6bbde34 100755 --- a/frontend/views/catalog/product.php +++ b/frontend/views/catalog/product.php @@ -184,6 +184,7 @@ $this->registerJs ("
+ $product])?> 'promo'])?> 'new'])?> 'top'])?> -- libgit2 0.21.4