danimohamadnejad's avatar

how to set similar items in ecommerce?

hello every one. how should I specify similar items of a product in ecommerce website?

0 likes
6 replies
bobbybouwmann's avatar

This is a really really broad question. Can you be more specific?

danimohamadnejad's avatar

honestly I dont have much information. I want a list of similar products to be displayed in comparison section of product details page. I need some general help in how to database should change and if I need any new table? or they are aquired on the fly?

Sergiu17's avatar
Sergiu17
Best Answer
Level 60

@danimohamadnejad You don't need new tables, you just need a new query based on the viewed product's parameters.

Let's say you view a notebook

  • category: Laptops
  • sub-category: Apple
  • ram: 8gb
  • colour: Silver

now you just need to take products from laptops category, apple sub-category`, or where laptops have 8gb of ram, or colour silver. obviously it depends on your project structure. hope it helps a little)

1 like
danimohamadnejad's avatar

interesting but I got confused. following is my data base:

products -->> id name

categories -->> id name parent_category_id

product_categories_pivot -->id productId categoryId

filters -->> id name

filter_options -->> id filterId value

product_options --->> id productId filterOptionId

now please guide more specifically in getting list of comparable products to a specific product.

thank you

Sergiu17's avatar

@danimohamadnejad I don't have a project to experiment with, but I'll try to give you some hints, also this article may help you

class ProductsController extends Controller
{
    public function show(Product $product)
    {
        // take current product category/details/parameters
        $categoryId = $product->category->id // or $product->categories->pluck('id')
        
        // take product's options
        $options = $product->options;

        // now just filter your Product model with above details
        $similarProducts = Product::whereHas([
            'category' => function($q) use ($categoryId) {
                $q->where('id', $categoryId);
            }
        ])
        ->orWhereHas() // ...
        ->orWhereHas(); // ..
        
        return view('products.show', compact('product', 'similarProducts'));
    }
}
1 like
danimohamadnejad's avatar

this is good solution. each product has many categories so I can easily get leaf category to get similar products. do you have any Idea to help me make the criteria more accurate like those whereHas which you have proposed. what should criteria be like? should I specify if the filter should be displayed in similar items when I am creating new filter???

Please or to participate in this conversation.