How to create vQmod file

vQmod

What is vQmod and why you need it you can read here.

For create vQmod file you need any simple text editor (better with the code highlighter). Create a file with “xml” extension, for example a2bizz_vqmod.xml. Here is a listing of this file:

<?xml version="1.0" encoding="UTF-8"?>
 
<modification>
 
  <!-- Modification id -->
  <id>My first fix</id>
 
  <!-- Modification version -->
  <version>1.0.0</version>
 
  <!-- vQmod version for this modification -->
  <vqmver>2.3.0</vqmver>
 
  <!-- The author of the modification -->
  <author><a href="mailto:blog.a2bizz@gmail.com">blog.a2bizz@gmail.com</a></author>
 
  <comment><![CDATA[
    Leave some comment here
  ]]></comment>
 
  <!-- File to change. Such files can be several in one xml file -->
  <file name="admin/model/catalog/product.php">
 
    <!-- Operation for one change. Such operations can be several for this file -->
    <operation>
 
      <!-- Exact place where you need to add your change. You can set position position for it (in our case "after") and the code between <![CDATA[...]]> It means that "after" this code you will add some your code. If you want to add your code before you need to set "before" position. You can also "replace" some code with your code. Important! The code to change must to be exactly like in the file. One space or tab more or less between words and vQmod will find nothing. -->
      <search position="after"><![CDATA[$product_id = $this->db->getLastId();]]></search>
 
      <!-- Here we write our code what we want to add after, before, or instead of the existing code -->
      <add><![CDATA[
$this->load->model('module/my_module');
$this->model_module_my_module->action($product_id);
      ]]></add>
    </operation>
  </file>
</modification>

As you can see above xml, it starts with the standard <?xml> tag declaration followed by a <modification>tag. Other tags include <version>,<id> , and <author>. You should not change <vqmver> as it indicates the vQmod version.

Work starts with the <file> tag. The name attribute indicates the file name that will be patched. It could be that you need multiple modifications to the same file. So that’s where the <operation> tag comes into play. Each modification will be wrapped by the <operation> tag. The info attribute allows you to add some useful message.

In this particular example, the <search> tag is used for the replace operation. You can use the position attribute to tell vQmod which operation will be performed. The content which is enclosed inside the <search> tag will be replaced with the content which is enclosed within the <add> tag.

As you may have noticed, the content for search and replace is enclosed within a CDATA tag, which means that the content won’t be interpreted as a markup but as character data.

Other options available for the position attribute:

  • before is used to insert the content before the search string.
  • after is used to insert the content after the search string.
  • top is used to insert the data at the top of the file. In this case, there’s no need for the <search> tag. Even if you’ve used it, it’ll be ignored.
  • bottom is used to insert the data at the bottom of the file. In this case, there’s no need for the <search> tag. Even if you’ve used it, it’ll be ignored.
  • In the case of ibefore, the data will be appended before the search data in the same line.
  • In the case of iafter, the data will be appended after the search data in the same line.

There are also a couple of optional attributes available for the <search> tag. Let’s have a quick look at these.

  • offset is an attribute designed to work in conjunction with the position attribute. So, for example, if position is set to before and offset is set to 3, it means that the content will be inserted before the three lines of the searched data.
  • index: Sometimes you want to replace only couple of instances of a particular string, not all the instances of that string in the search data. Say, for example, there are five instances of the $abc variable in your search data, but you only want to replace the first two instances of $abc with $def. In this case you need to specify index to 1,2.
  • The regex attribute is useful if you want to perform a regular expression based search for your operations. In that case, you’ll need to set regex to TRUE.

 

  • Download and extract the OpenCart-specific vQmod library.
  • Upload the vqmod directory to the root of your OpenCart installation.
  • Please make sure the vqcache directory that is under the vqmod directory is writable by the web server.
  • Visit http://www.mysite.com/vqmod/install and you should be presented with a success message. If that’s not the case, it’s likely a permissions issue.

vQmod (OCMod)

“vQmod” (Virtual Quick Mod) is an override system designed to avoid having to change core files. The concept is quite simple… Instead of making changes to the core files directly, the changes are created as xml search/replace script files. These script files are parsed during page load as each “source” core file is loaded with the “include” or “require” php functions. The source is then patched with the script file changes, and saved to a temp file. That temp file is then substituted for the original during execution. The original source file is never altered. This results in a “virtual” change to the core during execution without any actual modification to the core files

How it works ?

Instead of modifying actual files to add custom modifications, source files are parsed “on-the-fly” right before any of the follow functions are called :

  • include()
  • include_once()
  • require()
  • require_once()

The source is then cloned to a temp file and modifications read from an external script file are made to that temp file. The temp file is then substituted for the real file in the include path. Now the modification is in place while the original file has not actually been altered. Remove the script file and the original source is loaded.

vQmod class is currently written in PHP and script files are xml based, but the concept could be ported to any language and parse could be created for any format. Opencart is only fully tested and working platform that vQmod works with, but this is a concept that can work for other projects and platforms that are controller-based.

There are some files that cannot be used with vQmod :

  • index.php – this is the main file of the Opencart site, it has to load vQmod first for it to work on other pages. So you can’t put the cart in front of the horse.
  • Standalone files – Files that are just standalone and don’t extend or have no hierarchy will not work with vQmod. vQmod works by intercepting the “include” functions. So if file isn’t being included or required, then it cannot be vQmodded
  • css & js files – These files are rendered at the browser level, not at the server level, so vQmod has no effect on these. You can, however, create new files and use vQmod to alter the tpl files to point to these new css/js files. Or you can put.

 

Create a vqmod file from here