Jump to content

how to disable a module on specific day


Ehsanai

Recommended Posts

  • 2 weeks later...

You can achieve this by simple running a deactivate and activate SQL query. You can run this query automatic using a cronjob.
Just use the cron service of your Server, if you do not have access to this feature you can write a small PHP script and use a service like https://cron-job.org/ to run your query every X time. 

Deactivate module

// change id_module to the module id
UPDATE ps_module SET active = 0 WHERE id_module = 12;

Activate module

// change id_module to the module id
UPDATE ps_module SET active = 1 WHERE id_module = 12;

For example a simple PHP script can be like:

<?php
    // Check if _PS_ADMIN_DIR_ is defined, if not define.
    if (!defined('_PS_ADMIN_DIR_')) {
        define('_PS_ADMIN_DIR_', getcwd());
    }

    // Setup connection with config.inc.php (required for database connection, ...)
    include(_PS_ADMIN_DIR_.'/../config/config.inc.php');

    // Add a simple file protection method to prevent unwanted access. 
    $crezzur_key = 'ed3fa1ce558e1c2528cfbaa3f99403';

    // Check if correct crezzur_key is used when accessing page.
    // If the crezzur_key is not set our not equal the crezzur_key the page will stop running.
    // URL example: www.yourstore.com/yourbackoffice/yourpagename.php?crezzur_key=ed3fa1ce558e1c2528cfbaa3f99403
    if(!Tools::getValue('crezzur_key') || Tools::getValue('crezzur_key') != $secure_key) {
        die('UNAUTHORIZED: We dont want you on this page!');
    }

    $current_date = date('N');   // Display the current day in number format (0, 1, ..., 6)
    $module_id = 12;             // Module id to disable/enable?
    $disable_days = array(5, 6); // 0 = Monday, 1 = Tuesday, 2 = Wednesday, 3 = Thursday, 4 = Friday, 5 = Saturday, 6 = Sunday

    if (in_array($current_date, $disable_days)) {
        echo 'INFO: Current day is '. $current_date .', module will be <b>disabled</b>';
        Db::getInstance()->execute('UPDATE `'. _DB_PREFIX_ .'module` SET active = 0 WHERE id_module = '. (int)$moduleid);
    } else {
        echo 'INFO: Current day is '. $current_date .', module will be <b>enabled</b>';
        Db::getInstance()->execute('UPDATE `'. _DB_PREFIX_ .'module` SET active = 1 WHERE id_module = '. (int)$moduleid);
    }

 

Edited by Crezzur (see edit history)
  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...