Override Engine for OpenCart 2.x

  • Override Engine for OpenCart 2.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

Before proceeding with the installation: Log into the admin backend,  proceed to Extensions > Modifications, and click on the Clear button located near the top right corner.

Now simply upload the folders and files from the 'upload' directory via FTP to your OpenCart server's main directory. Some of the OpenCart core files are replaced, others are newly added. For each core file replacement a backup is created, too.

Finally, go to Extensions > Modifications again, and click on the Refresh button located near the top right corner.
 

 

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 extended controller may look something like this:

    <?php
    class addon_x_ControllerProductProduct extends ControllerProductProduct {

        /* overridden method, this newly introduced function is always called
           before the final rendering
        */
        public function preRender( $template_buffer, $template_name, &$data ) {
            if ($template_name != $this->config->get('config_template').'/template/product/product.tpl') {
                return parent::preRender( $template_buffer, $template_name, $data );
            }
           
            // add new controller variables
            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);
                $data['myfield'] = $product['myfield'];
                $this->load->language( 'product/product' );
                $data['text_myfield'] = $this->language->get( 'text_myfield' );
            } else {
                $this->data['myfield'] = '';
                $this->data['text_myfield'] = '';
            }

            // modify template file
            $template_buffer = str_replace(
                '<?php echo $description; ?>',
                '<?php echo $description; ?><br /><?php echo $text_myfield; ?>:<?php echo $myfield; ?>',
                $template_buffer
            );

            // call parent method
            return parent::preRender( $template_buffer, $template_name, $data );
        }
    }
    ?>

    The model extension looks something like this:

    <?php
    class addon_x_ModelCatalogProduct extends ModelCatalogProduct {
     
      /* override 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 previous example.

    Method preRender is a newly added core controller function which is always called just before rendering a template. Always be nice and call the parent method, 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_3_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: Are OCmod, VQmod or Event handlers supported?
  • Yes, they are.

    The Override Engine can co-exist with OpenCart's XML-based dynamic source code modifications like OCmod and VQmod.

    And the Override Engine can even be used to modify Event handlers, or simply co-exist with Event handlers.

  • 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 $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 2.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


  • Availability: In Stock
  • £0.00


Available Options


Related Products