I am know about CRON and how to create / manage it. But this issue was different.
I want to develop a module to delete any (unpaid) order that exceed the time frame given. Ex : I want to delete any unpaid order that has not been paid for 2 days after the order was placed.
I want to use existed model in opencart (and not use a new one). Lets say the module URL would be : http://www.yourstore.com/admin/index.php?route=module/modulename/function And will be called from CRON, and then all any unpaid order will be disappeared.
But the main problem is : when CRON want to access that URL, it need a security token or it will never be executed.
My question is : how to execute that module from CRON without security token (in case just for that module)?
Please help me, if you have a better idea or more clean way, I would say many thanks to you.
For admin related cron jobs, Do like this.
Copy the
admin/index.php
toadmin/index_for_cron.php
Now, in the
admin/index_for_cron.php
, search for these 2 lines and comment them out which are responsible for the login & the permissions.Now use this url for your cron job.
http://www.yourstore.com/admin/index_for_cron.php?route=module/modulename/function
NOTE: it is highly recommended to changes the name of
index_for_cron.php
into an ugly, unpredictable name for the security reasons.Hope this helps :)
I know this is a very old question, but I spent quite a long time trying to figure how to do the same in opencart version 2.x which works different. So I share here my solution.(based on Mike T approach)
1 - Create cli folder adjacent to admin and catalog.
2 - In this same folder create a file which you will run via cron or comandline, for example runcron.php
3 - In the same folder create the cli_dispatch.php file which is a copy of the index.php file in admin folder with some changes (Note in this is installation there is VQMOD activated, which may not be your case)
4 - Now create the file upload/system/config/cli.php which will be the one that opencart will use to read the configuration of your new cli bootrasp from file upload/system/framework.php
As you can see there i've commented all the Session and Actions lines related to permissions.
You will ave to edit the line
changing 'sale/yourscript' with the path and filename of your controller.
In the example, runnunig the runcron.php file will execute the index funcion in
I've done something similar to IJas. Adjacent to admin and catalog, I've created a new folder called "cli".
This folder contains a php file for a specific function to be performed by cli (executing scripts via crontab on a set schedule, or manually in the command line), as well as a "bootstrap" of sorts for these types of scripts. The bootstrap is essentially a copy of the "index" found in catalog or admin, and includes some checks and removes the permission checking and some other unnecessary items. It calls whatever controller/action is set forth in the calling specific function script (in the example below, it calls the index method of the class defined in /admin/controller/common/cli_some_function.php).
Function-Specific Script:
CLI "Bootstrap"/Dispatcher:
Using this scheme, I can ensure the script won't be called from the web, and I can have it fired off automatically from the server itself using a cron job (eg: 0 1 0 0 0 /path/to/php /path/to/opencart/cli/cli_some_function.php)
Note that the error_handler function is using some config options that aren't out-of-the-box. You can either set those up or put your own check there.
EDIT made some changes for the error handling
As I had a similar requirement several times, I put my ideas into a lightweight commandline tool called OCOK.
Especially the Cli Task Command allows you to call Opencart controllers via the commandline and thus lets you call them as cron jobs. Simply create a controller like this and save it as
admin/controller/task/example.php
:Via the commandline it can be called with parameters:
The above stated command would output:
Adding this to crontab is as easy as adding the following line to your cron file:
the respective paths need to be set correctly of course.
Installation available with composer. All further documentation can be found inside the docs: OCOK
By default opencart doesn't allow to access admin pages without login. The login and token validations are checked in
login()
method inadmin/controller/common/home.php
.it cant be set on frontend coz the model is in admin area.
- You may create a new controller and model for frontend with the same functionality in admin panel and use it for cronjob.Opencart has got usergroups which sets access rights for the users. So the admin pages will not get loaded for the users without permission. Hence you may need to modify the core files very much for setting cronjob in admin panel which may lead to severe security issues.
I suggest a frontend controller and model file for cronjob. For additional security you can pass a particular key parameter in url and write a condition to verify it.
Have a nice day !!
In 2.3.0.2 a very simple way I found was to add your controller function path into the ignored paths settings for login and permission restrictions. Then just add a url password or other check in that controller function to lock it down.
So first in admin/controller/startup/login.php add your controller function path to both $ignore arrays, eg 'common/cron/action'
And then in admin/controller/startup/permissions.php you want just the controller path, eg 'common/cron'
And then finally at start of your action() function do like:
Then i just added this to my cron: