OpenCart Global Functions – Developer Cheatsheet

 
 

In this blog post I will be showing you a complete list of all the useful OpenCart functions which can be called in all Controllers, Models and View Templates. Use this guide to discover more features you can implement in your designs and extend what you offer your customers in terms of customisations and features. I’ve separated them out into sections and given a brief description of what each of them do.

As ever, if you have any questions or want to add to the list, then leave a comment below.

Affiliate

 

$this->affiliate->login($email, $password);

This function logs the user into their Affiliate account, not their customer account. Incorporate this to build custom login pages for affiliates.

$this->affiliate->logout();

This function logs the user out of their Affiliate account.

$this->affiliate->isLogged();

This function checks to see if the Affiliate is logged in. Returns true or false. Can be used to put a login restriction on important Affiliate pages such as payment details which require them to be logged in.

$this->affiliate->getId();

Returns the Affiliate ID number.

$this->affiliate->getFirstName();

Returns the first name of the Affiliate.

$this->affiliate->getLastName();

Returns the last name of the Affiliate.

$this->affiliate->getEmail();

Returns the email address of the Affiliate.

$this->affiliate->getTelephone();

Returns the phone number of the Affiliate.

$this->affiliate->getFax();

Returns the fax number of the Affiliate.

$this->affiliate->getCode();

Returns the registered tracking code of the Affiliate.

Cache

 

$this->cache->get($key);

Grabs the requested cache file for faster local loading.

$this->cache->set($key, $value);

Use this to create a cache file for search results, images or other site data for faster loading.

$this->cache->delete($key);

Delete a local cache file from it’s key.

Captcha

 

$this->captcha->getCode();

Returns the correct code to match against user input to approve or fail a Captcha challenge.

$this->captcha->showImage();

Displays the Captcha image to the user.

Cart

 

$this->cart->getProducts();

Returns an array of all the contents in user’s basket along with other info like quantity, name, weights etc.

$this->cart->add($product_id, $quantity = 1, $option = array());

Add an item to the user’s cart with the code. First argument is the product ID, then the quantity, then the third option is an array for adding product options like colours, size, shape etc.

$this->cart->update($key, $quantity);

Updates the quantity of the products in a customer’s basket.

$this->cart->remove($key);

Remove a product from the user’s basket based on it’s key value in the getProducts() array.

$this->cart->clear();

Delete everything in the user’s basket

$this->cart->getWeight();

Get the total combined weight of everything in the user’s basket.

$this->cart->getSubTotal();

Get the subtotal of all the products in the user’s basket. This does not take into account extra order totals like handling fees, low order fees, shipping costs etc

$this->cart->getTaxes();

Get the total Tax payable on the products in the basket.

$this->cart->getTotal();

Get’s the final total for everything in the user’s basket. Including handling fees, low order fees and shipping costs (if set) etc.

$this->cart->countProducts();

Counts the number of products in the customer’s basket. This takes into account the quantity of the product so if a basket as 1 of Product A and 2 of Product B then the countProducts() will return 3.

$this->cart->hasProducts();

Checks to see if the user’s basket has anything in it. Returns true or false.

$this->cart->hasStock();

Checks to see if there is stock available for products in the user’s basket.

$this->cart->hasShipping();

Checks to see if there is a product in the user’s basket which requires shipping.

$this->cart->hasDownload();

Checks to see if there is a product in the user’s basket which is a downloadable product.

Config

 

$this->config->get($key);

Get a config setting from the database based on it’s key.

$this->config->set($key, $value);

Create a new config setting with a new key and value, used mainly for installing modules.

$this->config->has($key);

Checks to see if the config table has a setting with a particular key.

$this->config->load($filename);

Load a file within OpenCart.

Currency

 

$this->currency->set($currency);

Quickly set the user’s currency by the currency code with this.

$this->currency->format($number, $currency = ”, $value = ”, $format = true);

Used for formatting prices to match the layout of the selected currency.

$this->currency->convert($value, $from, $to);

Used for working out the value of something in one currency to another.

$this->currency->getId($currency = ”);

Gets the ID of the currency, leave blank to gett he ID of the selected currency or enter a currency code to choose.

$this->currency->getSymbolLeft($currency = ”);

Gets the left symbol, if any, for displaying next to the price.

$this->currency->getSymbolRight($currency = ”);

Gets the right symbol, if any, for displaying next to the price.

$this->currency->getDecimalPlace($currency = ”);

Gets the symbol which represents a decimal place in the selected currency.

$this->currency->getCode();

Returns the ISO code of the currently selected currency.

$this->currency->getValue($currency = ”);

Returns the value of the selected currency. This is the value used to work out currency rates.

$this->currency->has($currency);

Checks to see that there are active currencies and one has been set.

Customer

 

$this->customer->login($email, $password, $override = false);

Log the customer into their account quickly with their email and password. Can be used for quick logins and custom login pages.

$this->customer->logout();

Log the customer out of their account.

$this->customer->isLogged();

Checks to see if the customer is logged in or a guest browser. Returns true or false.

$this->customer->getId();

Gets the customer ID number, if logged in.

$this->customer->getFirstName();

Returns the first name of the customer.

$this->customer->getLastName();

Returns the last name of the customer.

$this->customer->getEmail();

Returns the email address of the customer.

$this->customer->getTelephone();

Returns the phone number of the customer.

$this->customer->getFax();

Returns the fax number of the customer.

$this->customer->getNewsletter();

Checks whether the customer has subscribed to the newsletter or not.

$this->customer->getCustomerGroupId();

Returns the ID of the customer group to which the user belongs.

$this->customer->getAddressId();

Returns the Address ID number of the default address of the customer.

$this->customer->getBalance();

Returns the store credit balance of the customer, if any.

$this->customer->getRewardPoints();

Returns the number of reward points which the customer has available.

Database

 

$this->db->query($sql);

Fires a database query, the SQL code can be input between the brackets as normal.

$this->db->escape($value);

Escapes potentially harmful characters from posted data before entering them into the database. Useful for avoiding SQL injection attacks.

$this->db->countAffected();

Returns the number of affected rows after a query string.

$this->db->getLastId();

Get the last inserted ID number for a database.

Document

 

$this->document->setTitle($title);

Set the title of a page. Visible in the tab on a browser and the blue link in a search result set.

$this->document->getTitle();

Get the title of a page. Visible in the tab on a browser and the blue link in a search result set.

$this->document->setDescription($description);

Set the meta description for a page.

$this->document->getDescription();

Get the meta description for a page.

$this->document->setKeywords($keywords);

Set the meta keywords for a page.

$this->document->getKeywords();

Get the meta keywords for a page.

$this->document->addLink($link, $rel);

Add a new rel link into the header for a particular page. Useful for setting custom headers for a page but not for others.

$this->document->getLinks();

Returns rel links set for page.

$this->document->addStyle($href, $rel = ‘stylesheet’, $media = ‘screen’);

Set a new stylesheet for the page.

$this->document->getStyles();

Returns extra stylesheets set for a page.

$this->document->addScript($script);

Add a new script file, such as JavaScript, for a page.

$this->document->getScripts();

Returns the extra script files for a page.

Encryption

 

$this->encryption->encrypt($value);

Secure a string with OpenCart’s encryption settings.

$this->encryption->decrypt($value);

Decrypt a secured sting with OpenCart’s encryption settings.

Image

 

$this->image->create($image);

Used for image formatting, create the new image.

$this->image->save($file, $quality = 90);

Save an image file with the default quality setting.

$this->image->resize($width = 0, $height = 0, $default = ”);

Resize an image to certain dimensions.

$this->image->watermark($file, $position = ‘bottomright’);

Add a watermark to an image.

$this->image->crop($top_x, $top_y, $bottom_x, $bottom_y);

Crop an image to certain parameters.

$this->image->rotate($degree, $color = ‘FFFFFF’);

Rotate an image a certain amount of degrees and set a background colour (default white).

$this->image->filter($filter);

Add a filter to an image.

$this->image->text($text, $x = 0, $y = 0, $size = 5, $color = ‘000000’);

Add text to an image and position it. Last argument for is font colour.

$this->image->merge($file, $x = 0, $y = 0, $opacity = 100);

Merge an image.

$this->image->html2rgb($color);

Convert the colour type to RGB output with this function.

Language

 

$this->language->get($key);

Get the key of the currently selected language.

$this->language->load($filename);

Load the language file for the currently selected language.

 Length

 

$this->length->convert($value, $from, $to);

Convert one length value into another for conversions.

$this->length->format($value, $length_class_id, $decimal_point = ‘.’, $thousand_point = ‘,’);

Format the length to display properly with symbols.

$this->length->getUnit($length_class_id);

Get the base unit for a measurement i.e. cm.

Log

 

$this->log->write($message);

Write a custom message to OpenCart’s log file. Useful for debugging new features.

Mail

 

$this->mail->setTo($to);

Set the recipient’s email address.

$this->mail->setFrom($from);

Set the sender email address.

$this->mail->setSender($sender);

Set the name of the sender.

$this->mail->setSubject($subject);

Set the subject line for the email.

$this->mail->setText($text);

Set the text format version of the email content.

$this->mail->setHtml($html);

Set the HTML version of the email content.

$this->mail->addAttachment($filename);

Add an attachment to the email.

$this->mail->send();

Send the email.

Pagination

 

$this->pagination->render();

Display number tabs for multiple pages.

Request

 

$this->request->get[$key];

Request a variable from the URL.

$this->request->post[$key];

Request POST data.

$this->request->cookie[$key];

Request COOKIE value.

$this->request->files[$key];

Request FILE data.

$this->request->server[$key];

Request SERVER data.

Response

 

$this->response->addHeader($header);

Add a new PHP header to be sent to the user’s browser.

$this->response->redirect($url);

Redirect user to a certain URL, can be internal and external.

$this->response->setCompression($compression);

Set the compression level.

$this->response->setOutput($output);

Add data to the content to be sent to the user’s browser for display.

$this->response->compress($data, $level = 0);

Compress files with the compression level set in the OpenCart admin area.

$this->response->output();

Actually output the data.

Session

 

$this->session->getId();

Get the PHP session ID.

Tax

 

$this->tax->setShippingAddress($country_id, $zone_id);

Set the shipping address for working out Tax on destination.

$this->tax->setPaymentAddress($country_id, $zone_id);

Set the payment address for working out Tax on Payment country and zone.

$this->tax->setStoreAddress($country_id, $zone_id);

Set the location of the store for working out Tax on the physical location of the shop itself.

$this->tax->calculate($value, $tax_class_id, $calculate = true);

Calculate the amount of Tax payable.

$this->tax->getTax($value, $tax_class_id);

Return the amount of Tax to pay.

$this->tax->getRateName($tax_rate_id);

Return the name of the tax rate which is payable for displaying to the customer.

$this->tax->getRates($value, $tax_class_id);

Return the names of all the tax rates which are relevant to the customer.

$this->tax->has($tax_class_id);

Checks to see if there are Tax classes set up in the admin section of the store.

Template

 

$this->template->fetch($filename);

Load up the requested template filename.

URL

 

$this->url->addRewrite($rewrite);

ReWrite the URL for SEO.

$this->url->link($route, $args = ”, $connection = ‘NONSSL’);

Create a URL based on route and arguments. OpenCart auto-checks for an SEO-URL equivalent and returns that if possible.

User

 

$this->user->login($username, $password);

Login an admin user to their admin account.

$this->user->logout();

Log an admin user out of their account.

$this->user->hasPermission($key, $value);

Checks to see if the logged in user has permission to view or edit a particular admin page.

$this->user->isLogged();

Checks to see if the admin user is logged into their account.

$this->user->getId();

Gets the ID number of the administrator account.

$this->user->getUserName();

Returns the username of the logged in administrator.

Weight

 

$this->weight->convert($value, $from, $to);

Convert one weight value into another measurement.

$this->weight->format($value, $weight_class_id, $decimal_point = ‘.’, $thousand_point = ‘,’);

Display a formatted weight with symbols for the customer.

$this->weight->getUnit($weight_class_id);

Get the base unit of weight for the class i.e. kg.

Leave a Reply