OpenCart 3 Library Global objects Methods – OpenCart video training

Hey everyone! Rupak Nepali again. In our last video, we talked about “How to create database table?“, Today we are talking about “OpenCart Library Predefined Objects’ Methods”.

An object has functions associated with it that are known as the object’s methods. Here we show predefined objects that OpenCart provided for which you don’t need to create or initialize the objects, you can use it directly. So better to know them so that it helps in achieving DRY which means Don’t Repeat Yourself.

Don’t write code unless you should. Write code only what you need, if you missed these predefined objects’ methods then you may repeat codes.

Opencart has many predefined objects’ methods that can be called in Controller and Model.  Like in the admin section we can see predefined objects like config, log, event, load, request, response, DB, session, cache, URL, language, open-bay, document, customer, currency, tax, weight, length, cart, encryption, model_setting_event, the user.

Let see an example of a database object:
The database object of OpenCart is $this->db, which has the following methods. All SQL queries are run from $this->db->query method which looks like below. For Select, replace, delete, update we use it like this in OpenCart and this returns three properties:

  • Database Object of Opencart is
    $this->db
  • Methods are:
    $this->db->escape($value) – mysql_real_escape_string() on the value passed
    $this->db->countAffected() – rows affected by an UPDATE query
    $this->db->getLastId() – last auto increment id same as mysql_insert_id()
    $this->db->connected() – Pings a server connection, or tries to reconnect if the connection has gone down
  • $this->db->query($sql)
  • All SQL queries are run from $this->db->query method which looks like:

    $query = $this->db->query(“SELECT * FROM ” . DB_PREFIX . “category WHERE
    $this->db->query(“REPLACE INTO `” . DB_PREFIX . category_path` SET …);
    $this->db->query(“INSERT INTO `” . DB_PREFIX . category_path` SET …);
    $this->db->query(“DELETE FROM ” . DB_PREFIX . “category WHERE …);
    $this->db->query(“UPDATE ” . DB_PREFIX . “category SET image = …);

  • $this->db->query method returns three properties:
    $result->num_rows = $query->num_rows; – gets the number of rows in a results
    $result->row = isset($data[0]) ? $data[0] : array(); – get the first row data
      $result->rows = $data; – get an array of row results

$this reference in the controller:

Go to admin/controller/extension/module account.php and var_dump the $this,

echo “<pre>”;
var_dump($this);
echo “</pre>”;
exit();

$this is a reference to the calling object, usually the object to which the method belongs. You can see the protected modifier registry which has config, log, event, load, request, response, etc.

Now let’s take config object:

echo “<pre>”;
$class_methods = get_class_methods($this->config);
print_r($class_methods);
echo “</pre>”;

Now you can see the list of method that you can use in your config object.

Let’s see the parameters that you can use in the get and set methods:
echo
“<pre>”;
var_dump($this->config);
echo “</pre>”;
exit();

Now let’s say we want the site URL then we can easily get it as:

print_r($this->config->get(‘site_url’));
exit();

In this way, you can keep on using other objects and methods that OpenCart provide.

Let’s take another example of the customer predefined object methods provided by OpenCart.

For that we can see at system/library/cart/customer.php:

You can see the namespace Cart which is like a directory, in OpenCart 3 Customer is now under “Cart” namespace. Customer object is instantiated as:

$customer = new Cart\Customer($this->registry);

So, in Customer class there are private properties and public methods login, logout, islogged, getId, getFirstName, getLastName, getGroupId, getEmail, getTelephone, getNewsletter, getAddressId, getBalance and getRewardPoints.

These public methods are available in the Controller and Model class of modules which I already showed for config object and it’s similar for other objects as well.

You can see the login method of customer object is used in catalog/controller/account/login.php,

You can see the $this->customer->login validate() method

 Like this, we use the predefined method wherever we need it.

This login method is used to provide the authentication for customer section of OpenCart. Customer session is activated for customer_id, first name, last name, customer_group_id, email, telephone, newsletter and address_id is assigned.

If you check the customer object without login all value assigned are null,

echo “<pre>”;
var_dump($this->customer);
echo “</pre>”;

 once you logged in values, you can see these values.

Now in code you can get those values through public methods provided by customer object to get the first name we have getFirstName method:

echo “<pre>”;
print_r($this->customer->getFirstName());
echo “</pre>”;

If you are confused of how to get the class methods name then PHP have get_class_method function:

echo “<pre>”;
$class_methods = get_class_methods($this->config);
print_r($class_methods);
echo “</pre>”;

With this logout method is used to unset the customer session and assign empty valued.

isLogged method of customer object is used to check whether customer_id is active or not

getId to get the customer_id

getFirstName method is used to return active customer first name

getLastName method is used to return active customer last name.

getEmail is used to return active customer email

getTelephone returns active customer telephone number.

Similarly, the customer object has the following other public methods:

getnewsletter

getAdddressId

getBalance

getRewardPoints

Some other predefined objects provided by OpenCart are the following:

Pre defined objects in Opencart

Cart, you can see the system/library/cart/cart.php where you can see the public methods that can be accessed from your modules. You can see here getProducts method, add method, remove method and so on.

Namespace in OpenCart

  • A namespace is like a directory and by adding ‘namespace’, Affiliate is now under ‘Cart’.
  • To use ‘Affiliate’, we call or instantiate as new Cart\Affiliate()
  • Adding a ‘namespace’ to a class is like organizing files from one directory into a bunch of sub-directories.
  • The use statement lets us call class by a nickname.

Affiliate

  • $this->affiliate->login($email, $password);
    You can find code used at catalog/controller/affiliate/login.php validate() method.
    Affiliate login script at system/library/cart/affiliate.php is used to provide the authentication for the Affiliate section of OpenCart. Affiliate session is activated for affiliate_id, firstname, lastname, email, telephone, fax, and code.
  • $this->affiliate->logout()
    You can find the code used at catalog/controller/affiliate/logout.php index() method.
    Affiliate logout script at system/library/cart/affiliate.php  is used to unset the affiliate session and assign empty value to affiliate_id, firstname, lastname, email, telephone, and fax of Affiliate. By this Affiliate is logged out.
  • $this->affiliate->isLogged()
    You can find the code used at catalog/controller/affiliate/account.php index() method.
    Affiliate isLogged script at system/library/cart/affiliate.php is used to check whether affiliate_id is active or not.
  • $this->affiliate->getId()
    You can find code used at catalog/model/affiliate/affiliate.php editAffiliate($data) method.
    Affiliate getId script at system/library/cart/affiliate.php  is used to return active affiliate_id.
  • $this->affiliate->getFirstName()
    You can find code used at catalog/controller/affiliate/login.php index() method.
    Affiliate getFirstName script at system/library/cart/affiliate.php is used to return active affiliate first name.
  • $this->affiliate->getLastName()
    You can find code used at catalog/controller/affiliate/login.php index() method.
    Affiliate getLastName script at system/library/cart/affiliate.php  is used to return active affiliate last name.
  • $this->affiliate->getEmail()
    You can find code used at catalog/controller/affiliate/edit.php validate() method.
    Affiliate getEmail script at system/library/cart/affiliate.php is used to return active affiliate email.
  • $this->affiliate->getTelephone()
    Not used in OpenCart but you can use it J.
    Affiliate getTelephone script at system/library/cart/affiliate.php  is used to return active affiliate Telephone number.
  • $this->affiliate->getCode()
    You can find code used at catalog/controller/affiliate/tracking.php index() method.
    Affiliate getCode script at system/library/cart/affiliate.php is used to return active affiliate tracking code which is used to track referrals.
  • $this->affiliate->getFax()
    Not used in OpenCart but you can use it J.
    Affiliate getFax script at system/library/cart/affiliate.php  is used to return the active affiliate Fax number.

Other Global Methods

  • Cart
  • Currency
  • Customer
  • Length
  • Tax
  • User
  • Weight
  • Config
  • DB
  • Document
  • Encryption
  • Image
  • Language
  • Log
  • Mail
  • Openbay
  • Pagination
  • Request
  • Response
  • Session
  • Template
  • URL

For Detail, the description read “OpenCart Theme and Module Development” book

OpenCart theme and module development

https://www.packtpub.com/web-development/opencart-theming

So, you get where to see the OpenCart library predefined objects’ methods which we can use in our Model and controller classes. Keep digging.

So, for a detailed description of predefined methods of OpenCart read the “OpenCart Theme and module development” book.

Like this, we completed our fifth video “OpenCart library Predefined Objects’ Methods”, in our next video we will show “OpenCart Code flow. Request and response in OpenCart”. Keep on tuning.

As always please don’t forget to post your questions or comments so that I can add extra topics or things that I need to develop which will help to develop the quality of videos series. You can follow at my twitter account @rupaknpl and subscribe to our youtube channel Opencart tutorials. Similarly, keep on visiting my personal blog where you will find lots of free modules.

See you in the next video and happy learning and thanks for watching videos.

Previous articleExploring the code of default Featured Module
Next articleNews Ticker Opencart Module for free Opencart 2

LEAVE A REPLY

Please enter your comment!
Please enter your name here