Last Updated:

Creating a module to clear the OpenCart 3 cache

Hello. In the last article "Creating a Cache Cleanup Extension for OpenCart 3," you and I created an extension for OpenCart 3 that clears the entire cache. Everything works well. But there's a problem. If we update our OpenCart, our functionality will disappear. This will happen due to the fact that the system files to which we made changes will be overwritten. In order to prevent this from happening, we need to put all the changes to the system files in the so-called modifier.

What is the OpenCart modifier?

OpenCart modifier.

The OpenCart modifier is an xml file that describes the files that need to be modified. When you apply the modifier, a copy of the modified file will be created in a special folder. For example, in OpenCart 3, all modified files will be in storage/modification.

Let's create an install file.xml

<?xml version="1.0" encoding="utf-8"?>

<modification>

<name>Test</name>

<code>Test</code>

<version>1.0</version>

<author>Zloykolobok</author>

<link>https://p-qc.com</link>

<file path="admin/controller/common/dashboard.php">

<operation>

<search><![CDATA[

$this->response->setOutput

]]></search>

<add position="before"><![CDATA[

$data['linkClearCache'] = $this->url->link('extension/total/cache','user_token=' . $this->session->data['user_token'],true);

]]></add>

</operation>

</file>

<file path="admin/view/template/common/dashboard.twig">

<operation>

<search><![CDATA[

<button type="button" id="button-setting" title="{{ button_setting }}" data-loading-text="{{ text_loading }}" class="btn btn-info"><i class="fa fa-cog"></i></button>

]]></search>

<add position="before"><![CDATA[

<a href="{{ linkClearCache }}" class="btn btn-success">Clear cache</a>

]]></add>

</operation>

</file>

<file path="system/library/cache/file.php">

<operation>

<search><![CDATA[

public function get($key)

]]></search>

<add position="before"><![CDATA[

public function deleteAll()

{

$files = glob(DIR_CACHE . 'cache.*');

if($files){

foreach ($files as $file) {

if(file_exists($file)){

unlink($file);

}

}

}

}

]]></add>

</operation>

</file>

<file path="system/library/cache.php">

<operation>

<search><![CDATA[

public function delete($key)

]]></search>

<add position="before"><![CDATA[

public function deleteAll()

{

$this->adaptor->deleteAll();

}

]]></add>

</operation>

</file>

</modification>

Let's now look at this file in more detail. (Tags: xml, modification, name, code, version, author, link I will not consider, everything is clear here)

But in the file tag in path, we specify the path for the file to be modified.

Next, in the <operation></operation> tags, we conclude the operations that we want to perform on the file.

In the <search></search tags> specify the data to search.

And in the tags <add></add> specify what should be done with this data (replace or insert)

So, for example, in the file

admin/controller/common/dashboard.php

we are looking for data

<![CDATA[

$this->response->setOutput

]]>

And before this data, we insert (if they are found) our own:

<add position="before"><![CDATA[

$data['linkClearCache'] = $this->url->link('extension/total/cache','user_token=' . $this->session->data['user_token'],true);

]]></add>

Here, note the position parameter, which specifies what to do with the new data. This parameter can take several values, here are the main ones:

  • before – inserts data before the search string.
  • after – insert data after the search string.
  • replace – insert data instead of the desired string.

Excellent, modifier created.

Create a folder upload/admin/controller/extension/total and in total create a cache.php file with the following code:

<?php

class ControllerExtensionTotalCache extends Controller {

private $error = array();

public function index()

{

if($this->validate()){

$this->clearAll();

}

return $this->response->redirect($this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true));

}

public function clearAll() {

$this->cache->deleteAll();

return $this->response->redirect($this->url->link('common/dashboard','user_token='.$this->session->data['user_token'], true));

}

protected function validate() {

if (!$this->user->hasPermission('modify', 'tool/cache')) {

$this->error['warning'] = 'Ooops';

}

return !$this->error;

}

}

Now the upload folder (with all attachments) and the xml file need to be packed into a zip archive with the name <program name>.ocmod.zip

You can proceed with the installation of this module.

Installing the module for OpenCart 3

Go to the admin panel. Go to "Modules/Extensions" -> "Install extensions" and click "Download"

Select our archive with the module (in my case install.ocmod.zip) and install it.

 

Our module is installed.

Go to "Modules/Extensions" -> "Modifiers" and click "Update"

Excellent let's check the folder with the storage/modification modifiers

As you can see, the modified files have appeared. Let's check the performance of our module.

But before that, we need to give view and edit access to the Administrator group. To do this, go to "System" -> "Users" -> "User Groups". Click edit next to the "Administrator" group and allow "extension/total/cache"

Now you can check the functionality of the module. Go to the dashboard

 

And click «Clear cache»

Clearing the cache was successful.

Conclusion.

You and I have created a module for complete cache cleaning. Yes, the module is still raw: there are no language files, no message is displayed about the completion of cache cleaning, full cache cleaning works only for the cache stored in the files. But the purpose of the article was to show how you can use a modifier file to modify OpenCart system files.