Ayyaz Zafar
Ayyaz Zafar

Reputation: 2163

Open Cart: How to get Products of Specific Category

I have following code in template/module/latest.tpl

      <?php foreach ($products as $product) { 

       }

      ?>

This code is showing all products but I want to display the products of category id 63.

Please someone guide me. Thank you

Upvotes: 1

Views: 3690

Answers (1)

David S&#225;nchez
David S&#225;nchez

Reputation: 634

I hope you already understood that that question is quite wrong.

Basically you want to sort by category ID inside the .TPL!!! Although is possible, is not the correct way to do it.

"OpenCart is designed to follow an MVC design pattern. The components of MVC (Model View Controller) can be broken down as follows."

M - Model This is where you will interact directly with your database, pulling data out and restructuring it to a format that is suitable for your frontend. This will usually mainly consist of DB queries, and little more. If you are used to writing mySQL queries, you will enjoy the way OpenCart provides access to continue to do just that. OpenCart does not use an ORM, but allows you to write direct database queries.

V - View (WHAT YOU ARE USING). This is the display side of the MVC pattern. The idea of the M and C is to pull as much logic out of the view as possible, meaning simpler templates. In order to redesign your whole store, you simply modify the View component, the M, C and L would remain the same. The view files in OpenCart have the .tpl suffix.

C - Controller This is where you will pull together the data from the Model, any config settings saved with your install or modules, and then render it by choosing the appropriate View file(s).

Seeing what I put in BOLD should give you the idea. If is still not clear, try to understand this sentence poorly written:

"The model is to get the information from the DB, the controller is the guy in charge of putting together the data in the appropriate variables, and the View, is in charge...OF THE VIEW. Basically the view should JUST PRESENT THE DATA YOUR PREPARE IN THE CONTROLLER, OBTAINED FROM THE MODEL".

source: http://docs.opencart.com/display/opencart/Introduction+to+MVC-L

Now, for solving your question, i will give you an advice

Add a function inside your controller/catalog/x.php,

$data = array(
        'filter_category_id' => $selected_category 
    );
$results = $this->model_catalog_product->getProducts($data);

and that should do the trick. Tell me if was working.

Upvotes: 4

Related Questions