Skip to content

Instantly share code, notes, and snippets.

@shyim
Created October 4, 2016 12:09
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shyim/51ffba2719d5223c65fa6917b94f8a1e to your computer and use it in GitHub Desktop.
Save shyim/51ffba2719d5223c65fa6917b94f8a1e to your computer and use it in GitHub Desktop.
Simple Cron in Shopware 5.2 Pluginsystem
<?php
namespace ShyimCron;
use Shopware\Components\Plugin;
use Shopware\Components\Plugin\Context\InstallContext;
use Shopware\Components\Plugin\Context\UninstallContext;
class ShyimCron extends Plugin {
public static function getSubscribedEvents()
{
return [
'Shopware_CronJob_MyCoolCron' => 'MyCoolCronRun'
];
}
public function install(InstallContext $context)
{
$this->addCron();
}
public function uninstall(UninstallContext $context)
{
$this->removeCron();
}
public function addCron()
{
$connection = $this->container->get('dbal_connection');
$connection->insert(
's_crontab',
[
'name' => 'MyCoolCron',
'action' => 'MyCoolCron',
'next' => new \DateTime(),
'start' => null,
'`interval`' => '100',
'active' => 1,
'end' => new \DateTime(),
'pluginID' => null
],
[
'next' => 'datetime',
'end' => 'datetime',
]
);
}
public function removeCron()
{
$this->container->get('dbal_connection')->executeQuery('DELETE FROM s_crontab WHERE `name` = ?', [
'MyCoolCron'
]);
}
public function MyCoolCronRun(\Shopware_Components_Cron_CronJob $job)
{
return 'Yes its running!';
}
}
@adeel-raza
Copy link

adeel-raza commented Aug 24, 2017

Thanks a lot for the snippet.
return true needs to be added in this method, else It does not uninstall the plugin properly.

public function uninstall(UninstallContext $context)
    {
        $this->removeCron();
       return true; 
    }

@nitinbute
Copy link

Thank you!

@maryisdead
Copy link

Also be aware of the new XML mechanism explained here: https://developers.shopware.com/developers-guide/plugin-system/#plugin-cronjob

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment