Ecommerce plugin development - how to periodically check the platform DB and call external API

时光怂恿深爱的人放手 提交于 2019-12-24 22:00:04

问题


I'm developing a module for Prestashop and I'll make a premise valid for Prestashop development environment: a module/plugin can do it's work thanks to some hooks, like Header, or leftBar, or BackOffice header loading, so apparently there is no way to do what I want to do: I want to periodically (let's say each day) check for abandoned cart in Prestashop database and send their information to external API.

I thought of a workaround which I don't like very much and it doesn't seem efficient to me: the plugin installs a custom database that will always contain one row: in that row there will be the current date. Whenever a user visits the website the plugin checks for that value on the DB: if the date is older than today then it updates it to today's date. If the module has just updated the value then I'll do my check on the DB and the API call, else I will not do anything (for the rest of the day, because every other check on the date will fail because it's already updated).

Is there some better way to do it?

********** UPDATE **********

So exists a way: cron tasks. Now my doubt is: is it possible to integrate the cron schedule inside my plugin? I need that when someone installs my plugin then he has nothing more to do: I don't want to delegate the configuration to him through the cron tasks manager integrated on the backoffice of Prestahsop. The problem with Prestashop seems to be that unlike Wordpress where exists a unique solution to do this (https://www.smashingmagazine.com/2013/10/schedule-events-using-wordpress-cron/), there is no way to do that for a general website target, so if you want to do that inside your custom module you have to choose one cron (https://www.prestashop.com/forums/topic/564504-how-create-cron-job-from-custom-module-all-by-code/)


回答1:


In my opinion the best way to do that is to use a cron or webcron.

You create a front controller on your module that do the work and you create a cron that execute the controller once a day.

How to create a front controller : http://doc.prestashop.com/display/PS16/Displaying+content+on+the+front+office#Displayingcontentonthefrontoffice-Embeddingatemplateinthetheme

if you call your controller cron, you can call it

  • by http for webcron: http://your.shop/module/[module-name]/cron
  • by shell for crontab :

    php -f [shop-folder]index.php "fc=module&module=[module-name]&controller=cron"
    

You can also use a cron-like system based on user visits but I will not recommend this solution if :

  • your task could be long : it could slow down the shop for the visitor and you are dependent of the web server time limit
  • the timing is important : your task will run when and if you have a visit on your site
  • the uniqueness is important : your task can be called twice if you have two visits at the same time

There is no built-in solution, there are modules for that but it will not solve your issue, the customer will have to install them.

my solution is to hook on the footer

public function hookFooter()
{
    // run cron every 12 hours
    $limit = time() - 12 * 60 * 60;
    $lastrun = Configuration::get('[module-name]-lastrun');
    if ($lastrun < $limit) {
        return '<script src="http://your.shop/module/[module-name]/cron" async></script>';
    }
}

and in your cron controller

$limit = time() - 12 * 60 * 60;
$lastrun = Configuration::get('[module-name]-lastrun');
if ($lastrun < $limit) {
    Configuration::updateValue('[module-name]-lastrun', time());
    // do stuff
}    


来源:https://stackoverflow.com/questions/47181893/ecommerce-plugin-development-how-to-periodically-check-the-platform-db-and-cal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!