A
A
Alexander Nevsky2018-11-14 21:19:53
Yii
Alexander Nevsky, 2018-11-14 21:19:53

How to display a selection of similar products?

Site on YII2.
I need to display similar products, I can’t figure out how to do this at all. All products have categories, genres and tags, basically you need to make a selection based on them.
And I can't figure out how to compare current genres and tags with everyone else. At the same time, it would not be easy, if 1 genre matches, then output, namely that several genres or tags match.
Please explain the logic.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2018-11-14
@slo_nik

Good evening.
Write a separate method that will take similar products from the database.
At the same time, pass in the request all the id of the genres and tags that you want to display.
Something like this:

public function getSemilarProducts()
{
    if(($models = Products::find()->where(['in', 'id_genre', [1,4,5])->andWhere(['in', 'id_tags', [2,6,5])->limit(5)->all()) !== null){
      return $models;
   }
   else{
        throw new NotFoundHttpException('The requested page does not exist.');
   }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question