Search code examples
cronprestashop

Which hook should be used to run a cron job in prestashop?


I am making a prestashop module in which I need to run cron job after every 20 minutes. I didn't find any hook for that. All I found is "Cron task manager" module but I don't want to use a module for that.


Solution

  • There is nothing like this in the Prestashop Core and module, but anyway, things can be simply done :

    • Call a function in module constructor, so it will be executed each time

      $this->mySuperCron();
      
    • Then store a time and just check time before executing your request :

      private function mySuperCron() {
           $check_time = strtotime('now - 20 minutes');
      
           if ( (int) Configuration::get('MYSUPERMODULETIMER') < (int) $check_time ) {
             // Make your cron here by either calling functions here or do it with file_get_contents / curl or echo an ajax which will be executed in backoffice
             Configuration::updateValue('MYSUPERMODULETIMER', (int) $check_time);
           }
      
      }