Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in /public_html/opencart/system/database/mysql.php on line 6
API

Documentation - API

API

THIS IS A WORK IN PROGRESS! IT IS NOT FINISHED AND ONLY JUST BEGUN

 

OpenCart is rapidly developing and often changes occur that break some of the API. While we are trying to improve the structure of our libraries and backward compatibility, the following should help assist you interfacing any developed modules with opencart.

Global Library Methods

These are functions and methods that can be called directly from almost any location (controller, model, or view). By our MVC standards, these are typically called in the controller only, any results are passed as a separate variable to the view. These are all found in the /system/library folder

CACHE - [system/library/cache.php]

The cache class is used to reduce the strain on the database by serializing and storing the data in a flat file. This speeds up the loading of many queries throughout the store. Cache must be cleared whenever a change is made to the releveant data. Cache data is stored in the /system/cache folder as serialized data.

Available methods

$this->cache->get( $key );  $key is the content type (product, category, country, zone, etc)

$this->cache->set( $key, $value );  $key is the content type (product, category, country, zone, etc), $value is the unserialized data

$this->cache->delete( $key );  $key is the content type (product, category, country, zone, etc). This is called when the data has changed and the cache needs to be reconstructed by a an actual database query.

The cached data is called by the model lookup functions if it exists. Otherwise, the cache file can be created after the db query has been performed

Example without cache (this pulls data from the database everytime, even when it hasn't changed):

public function getCountries() {
    $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "country WHERE status = '1' ORDER BY name ASC");
   
    return $query->rows;
}

 

Example with cache (this pulls data from the database once, and then stores that info in a flat file to be used in subsequent queries until deleted):

public function getCountries() {
        $country_data = $this->cache->get('country');
       
        if (!$country_data) {
            $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "country WHERE status = '1' ORDER BY name ASC");
   
            $country_data = $query->rows;
       
            $this->cache->set('country', $country_data);
        }

        return $country_data;
    }

CART - [system/library/cart.php]

The cart class is used as the common "brain" for all things added to the cart. The products, options, quantities, totals, discounts, taxes, etc are all interfaced through the cart class. These can be called from anywhere.

Available methods

$this->cart->getProducts() - Gets all products currently in the cart including options, discounted prices, etc.
$this->cart->add( $product_id, $qty = 1, $options = array()) - Allows you to add a product to the cart
$this->cart->remove( $key ) - Allows you to remove a product from the cart
$this->cart->clear() - Allows you to remove all products from the cart
$this->cart->getWeight() - Sum of the weight of all products in the cart that have require shipping set to Yes
$this->cart->getSubTotal() - returns the subtotal of all products added together before tax
$this->cart->getTotal() - returns the total of all products added together after tax
$this->cart->countProducts() - returns the count of all product in the cart
$this->cart->hasProducts() - returns true if there is at least one item in the cart
$this->cart->hasStock() - returns false if there is at least one item in the cart that is out of stock
$this->cart->hasProducts() - returns true if there is at least one item in the cart that requires shipping
$this->cart->hasDownload() - returns true if there is at least one item in the cart that has a download
associated

 

 

Hébergé par Easy Hébergement