Ashok Basnet
Ashok Basnet

Reputation: 180

Opencart list categories inside manufacturers

I have searched for hours but could not find an answer to this, or a module to help.

We are building a store and our client needs the ability to navigate the store by manufacturer. Is there any way that the manufacturer page can list the categories and subcategories.

There seems two ways to do it.

  1. Add brands while adding categories in admin section.
  2. Get all categories inside the brands by join operation while viewing the manufacturer.

Are there any modules available to link up categories with manufacturers so that I can display categories inside the manufacturer page.

Or the only way is to query all the products inside the manufacturer and get the categories out of it... I guess it is not a good solution.

So any suggestions would be a great help.

Thanks.

Upvotes: 5

Views: 3032

Answers (1)

Ashok Basnet
Ashok Basnet

Reputation: 180

I figured a way to find the categories that belongs to a manufacturer. The second options seems better.

Here is the function that I added to catalog/model/catalog/manufacturer.php

public function getManufacturerCategories($manufacturer_id) {
    $query = $this->db->query("
        SELECT 
        DISTINCT c.category_id,cd.name
        FROM
        ". DB_PREFIX . "manufacturer m 
        LEFT JOIN ". DB_PREFIX. "product p ON (m.manufacturer_id = p.manufacturer_id)
        LEFT JOIN ". DB_PREFIX. "product_to_category p2c ON (p2c.product_id = p.product_id)
        LEFT JOIN ". DB_PREFIX. "category c ON (c.category_id = p2c.category_id)
        LEFT JOIN ". DB_PREFIX. "category_description cd ON (cd.category_id = p2c.category_id)
        WHERE
        p.status = 1
        AND m.manufacturer_id = '".(int)$manufacturer_id."'
        AND c.status= 1
        ");

    return $query->rows;
}

Here is the output array

stdClass Object (
    [row] => Array
        (
            [category_id] => 20
            [name] => Desktops
        )

    [rows] => Array
        (
            [0] => Array
                (
                    [category_id] => 20
                    [name] => Desktops
                )

            [1] => Array
                (
                    [category_id] => 24
                    [name] => Phones & PDAs
                )

        )

    [num_rows] => 2 )

Upvotes: 2

Related Questions