Core objects
Products, categories, customers, carts and orders in the core are the same kind of ObjectModel models as AcmeReview. Their classes live in the classes folder and are available in a module without any include. A module reads shop data through them: a product name for a review, a customer name, the contents of an order.
Product
$product = new Product(1, false, $this->context->language->id);
$name = $product->name;The first argument is the product id, the third is the language id. With a language id $product->name holds a string, for example "T-shirt". Without it you get an array of names in all languages.
| Property | What is in it |
|---|---|
name | The name |
description_short | The short description |
reference | The reference (SKU) |
price | The price without tax and discounts |
active | Whether the product is enabled |
id_category_default | The id of the default category |
link_rewrite | A part of the page address |
Price
The price for a customer depends on tax, discounts and the customer group, so it is not taken from a property but calculated by a method:
$price = Product::getPriceStatic(1);
$priceWithoutTax = Product::getPriceStatic(1, false);
$text = $this->context->getCurrentLocale()->formatPrice($price, $this->context->currency->iso_code);The first line returns a number — the price with tax and discounts. formatPrice() turns the number into a string like "€29.00" following the rules of the current language and currency.
Links and images
Page addresses are built by the $this->context->link object:
$link = $this->context->link;
$productUrl = $link->getProductLink($product);
$cover = Product::getCover($product->id);
$imageUrl = $link->getImageLink($product->link_rewrite, $cover['id_image'], 'home_default');getCover() returns an array with the id of the product's main image. The third argument of getImageLink() is the image size name; the set of sizes is defined by the theme, in classic these are small_default, home_default, large_default.
| Method | Address |
|---|---|
getProductLink($product) | A product page |
getCategoryLink($category) | A category page |
getPageLink('cart') | A built-in page: cart, order, my-account, index |
getModuleLink('acmehello', 'reviews') | A module page — covered in the next chapter |
Category
$category = new Category((int) $product->id_category_default, $this->context->language->id);
$categoryName = $category->name;
$products = $category->getProducts($this->context->language->id, 1, 10);getProducts() returns an array of the category's products: here the first page of ten items.
Customer
$customer = $this->context->customer;
if ($customer->isLogged()) {
$fullName = $customer->firstname . ' ' . $customer->lastname;
$email = $customer->email;
}In the front office the current customer is already in the context. isLogged() tells a logged-in customer from a guest. A customer is loaded by id the same way as a product: new Customer($id).
Cart
$cart = $this->context->cart;
$items = $cart->getProducts();
$total = $cart->getOrderTotal(true, Cart::BOTH);getProducts() returns an array of cart lines with the keys id_product, name, cart_quantity, total_wt. getOrderTotal() calculates the total: true means with tax, Cart::BOTH means products together with shipping.
Order
$order = new Order($orderId);
$reference = $order->reference;
$paid = $order->total_paid;
$items = $order->getProducts();
$customer = new Customer((int) $order->id_customer);An order stores the ids of related objects: id_customer, id_cart, id_currency. The object itself is obtained by creating it from that id.
Using it in the module
The product name in the reviews block:
// modules/acmehello/acmehello.php
public function hookDisplayFooterProduct(array $params): string
{
$productId = (int) Tools::getValue('id_product');
$product = new Product($productId, false, $this->context->language->id);
$this->context->smarty->assign([
'productName' => $product->name,
'reviews' => $this->getReviews($productId),
]);
return $this->fetch('module:acmehello/views/templates/hook/product-reviews.tpl');
}The $productName variable is now available in the template — the block heading can read "Reviews of T-shirt".
Common mistake
An object is created from a request id and its properties are read right away: (new Product($id))->name. If there is no product with that id, there is no error — you get an empty object and an empty page. After loading, check Validate::isLoadedObject($product) and return an empty block or a 404 error for a missing product.