Back to Top

How to write upgrade script in PrestaShop module

Updated 10 June 2022

In this blog, we are going to show you how to write an upgrade script in the PrestaShop module.

To easily update the module to a newer version without losing any data of the module.

How to write upgrade script in PrestaShop module
Module Structure

You have to create an upgrade folder inside your module as shown in the above image.

Let’s suppose your current module version is 1.0.0 and you are developing a new version of the same module suppose 1.1.0.

Now you need to alter a few columns in the previous DB table, need to register a new hook, have to create a new table or need to add any data in DB.

Searching for an experienced
Prestashop Company ?
Find out More

For upgrading modules without losing any data PrestaShop provides a solution to write your own upgrade script.

Firstly, you have to just create a folder name upgrade in the module folder having the next upcoming version upgrade file as upgrade-1.1.0.php

    <?php
    function upgrade_module_1_1_0($module)
    {
        $queries = array(
            "ALTER TABLE `"._DB_PREFIX_."wk_demo`
            `extra_column` varchar(255) DEFAULT NULL AFTER `old_column`"        
        );
        $db = Db::getInstance();
        $success = true;
        foreach ($queries as $query) {
            $success &= $db->execute($query);
        } 
        $module->registerHook('displayHeader');
        $module->registerHook('actionFrontControllerSetMedia');
        return true;
    }

Here we have shown an example for altering a table and adding a new column in DB and registering a few hooks which are not registered previously in the module.

At last, if you are releasing another updated version for the module you have to create the file as upgrade-2.0.0.php and follow the same rule.

When someone tries to update directly from version 1.0.0 to 2.0.0 then PrestaShop executes the upgrade file script one by one for each version in ascending order.

That’s all.

If you are facing any issues or doubts in the above process, please feel free to contact us through the comment section.

I would be happy to help.

Also, you can explore our PrestaShop Development Services and a large range of quality PrestaShop Modules.

For any doubt contact us at [email protected].

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home