Magento 添加计划任务

Magento 添加计划任务
2016年5月13日 浏览量 26 1 min read
Magento 添加计划任务

通常我们需要在magento网站中添加一个计划任务,来定时自动执行一些特殊任务,比如定时清理网站日志,定时给客户发送邮件等等,那么我们在今天这篇博客中就来看一下如何给magento添加一个计划任务。

其实在magento中创建一个计划任务是一件非常容易的事情。下面我们来看一下具体的操作步骤:首先我们需要创建一个计划任务依托的模块(如何创建模块这里就不说了,如需帮助,请查看这篇文章)然后我们在配置文件config.xml中添加如下代码:

<crontab>
<jobs>
<clearorder_detect>
<schedule>
<cron_expr>0 1 * * *</cron_expr>
</schedule>
<run>
<model>clearorder/cron::detect</model>
</run>
</clearorder_detect>
</jobs>
</crontab>

下一步,我们需要在model下创建一个cron.php文件,并且声明detect()方法。

<?php

class Me_Clearorder_Model_Cron {

public function detect() {
Mage::log('order detection running...', null, 'magease.log');
$orderCollection = Mage::getResourceModel('sales/order_collection');
$orderCollection->addFieldToFilter('status', 'pending_payment')
->addFieldToFilter('created_at', array('lt' => new Zend_Db_Expr("DATE_ADD('" . now() . "', INTERVAL -'30:00' HOUR_MINUTE)")))
->getSelect()
->order('e.entity_id')
->limit(10);
$orders = "";
foreach ($orderCollection->getItems() as $order) {
$orderModel = Mage::getModel('sales/order');
$orderModel->load($order['entity_id']);
if (!$orderModel->canCancel())
continue;
$orderModel->cancel();
$orderModel->setStatus('cancled_overtime');
$orderModel->save();
}
}

}

接下来,我们创建的这个计划任务将会在每天凌晨01:00执行一次。

温馨提示:想要让计划任务按计划顺利执行,必须将服务器配置好计划任务功能,(www.***.com/cron.php)否则我们的计划是不能准确执行的。

Previous article:
Next article:

是否允许我们在发布新内容或者进行促销活动向您发送消息?

Remind me later

Thank you! Please check your email inbox to confirm.

Oops! Notifications are disabled.

© 2014-2023 www.magease.com. All Rights Reserved. 寰云网络 版权所有    鲁ICP备 14014975号-1