Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Contents

Core models: products, categories, images, prices

Queries for products, categories and images do not have to be written — the core already has models for them. Your controller loads a core model exactly like its own. This lesson shows a product list on the page information/hello.

One product

// catalog/controller/information/hello.php
$this->load->model('catalog/product');
 
$product = $this->model_catalog_product->getProduct(42);
 
$data['product_name'] = $product ? $product['name'] : '';

getProduct() returns an array with the fields of the product in the current language, or an empty array if there is no such product or it is turned off. The model file is catalog/model/catalog/product.php — open it to see all methods.

A list of products

$products = $this->model_catalog_product->getProducts(['start' => 0, 'limit' => 4]);

The argument is an array of filters. The most used keys:

KeyMeaning
filter_category_idOnly products of this category
sort, orderSort field and direction
start, limitWhich rows to return

The full set of filter keys depends on the release — look at the method in your copy.

Images

A product stores the path of the original picture. A copy of the needed size is made by the model tool/image:

$this->load->model('tool/image');
 
$thumb = $this->model_tool_image->resize($product['image'], 200, 200);

resize() creates the file in image/cache/ on the first call and returns its full address.

Price with tax and currency

Prices are stored in the default currency of the shop and without tax. Two registry objects turn the number into text for the customer:

$price = $this->currency->format(
    $this->tax->calculate($product['price'], $product['tax_class_id'], $this->config->get('config_tax')),
    $this->session->data['currency']
);

tax->calculate() adds the tax if the shop shows prices with tax. currency->format() converts to the currency chosen by the customer and adds its sign: $8.00 or 7.50€.

Everything together

$data['products'] = [];
 
foreach ($this->model_catalog_product->getProducts(['start' => 0, 'limit' => 4]) as $result) {
    $data['products'][] = [
        'name' => $result['name'],
        'thumb' => $this->model_tool_image->resize($result['image'] ?: 'placeholder.png', 200, 200),
        'price' => $this->currency->format(
            $this->tax->calculate($result['price'], $result['tax_class_id'], $this->config->get('config_tax')),
            $this->session->data['currency']
        ),
        'href' => $this->url->link('product/product', 'language=' . $this->config->get('config_language') . '&product_id=' . $result['product_id']),
    ];
}
{% for product in products %}
  <a href="{{ product.href }}"><img src="{{ product.thumb }}" alt="{{ product.name }}"> {{ product.name }}</a> — {{ product.price }}
{% endfor %}

Open index.php?route=information/hello — the page shows four products with pictures, prices and links.

Categories

$this->load->model('catalog/category');
 
$categories = $this->model_catalog_category->getCategories(0);

The argument is the id of the parent category; 0 returns the top level.

Common mistake

The price from the database is printed as is: {{ product.price }} shows 8.0000, without tax and in the wrong currency. Every price shown to the customer goes through $this->tax->calculate() and $this->currency->format().

Thanks for reading!

Donate to the site — we work hard for you.