Alexander Kolovsky
Alexander Kolovsky

Reputation: 105

How to display items of a certain category in OpenCart 2?

I need to display items of a certain category on homepage in OpenCart 2. How can I do that? My code from controller:

   $products_info = $this->model_catalog_product->getProducts();
   $data['products'] = $products_info;

Upvotes: 0

Views: 245

Answers (1)

You Old Fool
You Old Fool

Reputation: 22931

Assuming you've first loaded the model you're calling:

$this->load->model('catalog/product');

All you need to do is define an array of settings to pass to getProducts(). In this case you can get away with only sending the category ID you want to get products for:

$filter_data = array(
    'filter_category_id' => $category_id,
);

Then call the function as you did:

$results = $this->model_catalog_product->getProducts($filter_data);

Then iterate over the products just as if you were in a category viewto pass data to the view:

foreach ($results as $result) {
    if ($result['image']) {
        $image = $this->model_tool_image->resize($result['image'], $this->config->get('config_image_product_width'), $this->config->get('config_image_product_height'));
    } else {
        $image = $this->model_tool_image->resize('placeholder.png', $this->config->get('config_image_product_width'), $this->config->get('config_image_product_height'));
    }

    if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
        $price = $this->currency->format($this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax')));
    } else {
        $price = false;
    }

    if ((float)$result['special']) {
        $special = $this->currency->format($this->tax->calculate($result['special'], $result['tax_class_id'], $this->config->get('config_tax')));
    } else {
        $special = false;
    }

    if ($this->config->get('config_tax')) {
        $tax = $this->currency->format((float)$result['special'] ? $result['special'] : $result['price']);
    } else {
        $tax = false;
    }

    if ($this->config->get('config_review_status')) {
        $rating = (int)$result['rating'];
    } else {
        $rating = false;
    }

    $data['products'][] = array(
        'product_id'  => $result['product_id'],
        'thumb'       => $image,
        'name'        => $result['name'],
        'description' => utf8_substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, $this->config->get('config_product_description_length')) . '..',
        'price'       => $price,
        'special'     => $special,
        'tax'         => $tax,
        'minimum'     => $result['minimum'] > 0 ? $result['minimum'] : 1,
        'rating'      => $result['rating'],
        'href'        => $this->url->link('product/product', 'path=' . $this->request->get['path'] . '&product_id=' . $result['product_id'] . $url)
    );
}

Now you have an array of products to display however you'd like in your tpl.

Upvotes: 1

Related Questions