Override Engine for OpenCart 1.5.x

  • Override Engine for OpenCart 1.5.x
This package provides a replacement system engine for OpenCart. It uses the same MVC architecture as the standard OpenCart framework.

All instances of controller, model, or library classes are created by a special factory class.

An additional override feature is supported. This allows 3rd party addons to do changes to the OpenCart core classes, templates, and language files. Core controller, model and library classes can be extended and their methods be overridden in a normal object oriented programming manner.

 

Installation

  1. Simply upload the folders and files from the 'upload' directory to your OpenCart server's main directory.

    The following OpenCart files are replaced by the upload:

    admin/index.php
    catalog/model/checkout/order.php
    catalog/model/checkout/voucher.php
    system/engine/action.php
    system/engine/controller.php
    system/engine/front.php
    system/engine/loader.php
    system/library/language.php
    system/startup.php
    index.php

    And the following new files are added by the upload:

    override/bugfixes/*   (OpenCart 1.5.4.x only)
    override/meta_keywords/*
    system/engine/factory.php
    system/helper/modifier.php
    system/library/variable_stream.php


     
  2. If you have VQmod pre-installed on your OpenCart server then you need to run the VQmod installer again:

    http://www.your-store.com/vqmod/install/index.php

     

Frequently Asked Questions

  • Q1: Where do overrides go?
  • All overrides go into a new directory named 'override'.  It uses the following directory structure, only files which are to be modified in the OpenCart core are needed.

      - override
        - <addon-1>
          - admin
            - controller
              + ...
            - language
              + ...
            - model
              + ...
          - catalog
            - controller
              + ...
            - language
              + ...
            - model
              + ...
          - system
            - library
              + ...
        + <addon-2>
          ...
        + <addon-n>

          ...

     
  • Q2: Can I have an example please?
  • Sure, why not:

    For example, if addon-x needs a new field named myfield on the frontend product page, it typically needs to modify the following core class files:

    catalog/controller/product/product.php
    catalog/language/english/product/product.php
    catalog/model/catalog/product.php

    We therefore define the following override files (extended classes):

    override/addon-x/catalog/controller/product/product.php
    override/addon-x/catalog/language/english/product/product.php
    override/addon-x/catalog/model/catalog/product.php

    Assuming the database has a new field named 'myfield' in the 'product' table, then the controller extension may look something like this:

    <?php
    class addon_x_ControllerProductProduct extends ControllerProductProduct {

        /* overridden method */
        public function index() {
            $this->load->language('product/product');
            $this->data['text_myfield'] = $this->language->get('text_myfield');
            if (isset($this->request->get['product_id'])) {
                $product_id = $this->request->get['product_id'];
                $this->load->model( 'catalog/product' );
                $product = $this->model_catalog_product->getProduct($product_id);
                $this->data['myfield'] = $product['myfield'];
            } else {
                $this->data['myfield'] = '';
            }
            parent::index();
        }
    }
    ?>

    The model extension looks something like this:

    <?php
    class addon_x_ModelCatalogProduct extends ModelCatalogProduct {

        /* overridden method */
        public function getProduct($product_id) {
            $product = parent::getProduct( $product_id );
            if ($product) {
                $sql  = "SELECT myfield FROM `".DB_PREFIX.product."` ";
                $sql .= "WHERE product_id='".(int)$product_id."'";
                $result = $this->db->query($sql);
                $product['myfield'] = $result->row['myfield'];
            } else {
                $product['myfield'] = '';
            }
            return $product;
        }
    }
    ?>

    The language override might look like this:

    <?php
    // new texts
    $_['text_myvar'] = 'This is the new field:';
    ?>

     

  • Q3: Can I modify core templates?
  • You sure can!

    However, in most cases you'd want to use your own web theme rather than OpenCart's 'default' theme. And if you use your own web theme, you can simply add any new fields directly into the template file of your new theme. This way, only the corresponding controller class needs to be extended with overridden methods which can populate your new variables for your template.

    In case you think you have to modify a core OpenCart template file, you can extend the controller class and override a method called 'preRender', as in the following example:

    <?php
    class addon_x_ControllerProductProduct extends ControllerProductProduct {

        // overridden method
        protected function preRender( $templateBuffer ) {
            if(strpos($this->template,'/template/product/product.tpl')) {
                $templateBuffer = str_replace(
                    '<div class="description">',
                    '<div class="description">' .
                    '<?php echo $text_myfield; ?>:<?phpecho $myfield; ?>',
                    $templateBuffer
                );
            }
            return parent::preRender( $templateBuffer );
        }
    }
    ?>

    Method preRender is a newly added hook which is always called just before rendering a template. Always be nice and call the parent method, in order to give other addons a chance to do their template modifications, too.

  • Q4: Can multiple addons modify the same core file?
  • Absolutely!

    As part of the new OpenCart core engine, a special factory class is used for loading (possibly extended) classes. The nature of external addons is that they are not aware of each other. Each addon may extend and override the same OpenCart core class and method. However, if 2 or more addons do this, the new OpenCart factory loads the original core class and all extended classes and then modifies the parent class of each addon's extended class, so they all end up in a chain of class extensions.

    For example, if the addons addon_1, addon_2 and addon_3 all extend the same core class ControllerProductProduct, their original class definitions

    class addon_1_ControllerProductProduct extends ControllerProductProduct {
        ....
    }
    class addon_2_ControllerProductProduct extends ControllerProductProduct {
        ....
    }
    class addon_1_ControllerProductProduct extends ControllerProductProduct {
        ....
    }

    are automatically modified by the Override Engine so as to become a chain of class extensions, before loading them:

    class addon_1_ControllerProductProduct extends ControllerProductProduct {
        ....
    }
    class addon_2_ControllerProductProduct extends addon_1_ControllerProductProduct {
        ....
    }
    class addon_3_ControllerProductProduct extends addon_2_ControllerProductProduct {
        ....
    }
    All classes modified this way are loaded, and then a new instance of 'addon_3_ControllerProductProduct' is created.

    Every overridden method in an extended class always has to call the parent method, otherwise the chain gets broken! But other than that, there are no restrictions.

     
  • Q5: Is VQmod supported?
  • Yes, it is. Just re-run the VQmod installer after having uploaded this new override engine.

    The Override Engine uses object orient programming techniques to do changes to OpenCart core classes, whereas VQmod uses dynamic search and replace operations applied to the core files.

  • Q6: Why does the Controller class have the new method preRender?
  • This is actually a newly introduced hook function.

    Any controller class can be extended and method preRender can then be overridden. Method preRender is called just before the selected template is rendered. You can do 3 things in here:

    1. Add new controller variables to the $this->data array as needed by the modified template.

    2. Modify the template buffer.

    3. Do any other changes before rendering the template.

 

Further help and customized versions

 

This addon has been successfully tested for the standard OpenCart versions 1.5.4.x, 1.5.5.x or 1.5.6.x. Don't use other Opencart versions with it.

If you need a customized version of this addon, let us know and we can create one for a charge.

Write a review

For support and technical enquiries please use our regular Contact Us page. This is for Reviews only!

Note: HTML is not translated!
    Bad           Good
Captcha


  • Availability: In Stock
  • £0.00


Available Options


Related Products