【发布时间】:2012-06-18 09:17:14
【问题描述】:
我正在使用 cron 作业将所有“待处理”网上银行订单更改为“待付款”
(这是为了解决我的问题:Why is state not transitioning to "payment_pending" for orders cancelled at gateway?)
这是我的代码- [已编辑]
const MINUTES_DELAY = 15; //Orders younger than this are not changed
public function run() {
// date_default_timezone_set('Asia/Kolkata');
$old_time = time() - (self::MINUTES_DELAY*60);
$out = date('d.m.y h:i:s A', $old_time)."\n";
$out .= date('d.m.y h:i:s A')."\n";
file_put_contents('/home/vinayak/cron.txt', '1'.$out, FILE_APPEND); //Out1
$orders = Mage::getModel('sales/order')->getCollection()
->addFieldToFilter('status', 'pending')
->addFieldToFilter('cod_fee', array('null' => true))
->addAttributeToSelect('customer_email')
->addAttributeToSelect('created_at')
;
foreach ($orders as $order) {
if (strtotime($order->getCreatedAt()) < $old_time){
$order->setState('pending_payment', true)->save();
$out .= $order->getCustomerEmail()." @ ".$order->getCreatedAt()."\n";
}
}
file_put_contents('/home/vinayak/cron.txt', '2'.$out, FILE_APPEND); //Out2
return true;
}
我已检查 cron 是否正常工作。但是状态/状态没有改变。我现在没有出错了。
[EDITED] 现在的问题 - 我在代码中将输出标记为“out1”,而不是“out2”
更改代码后,我发现,只要if 条件为真,就会出现问题(如上)。这指出了$order->setState('pending_payment', true)->save(); 行的问题(我已经注释掉了if 中的另一行,问题仍然存在,但是如果我注释掉这一行,out2 就会被打印出来)。似乎执行被卡在了这一行(无限循环?还是一些内部问题?)
$order->setState('pending_payment', true)->save(); 有什么问题?还有其他方法可以完成上述事情吗?
我是否也可以按订单“创建时间”进行过滤,这样我就不会更改几秒钟前创建的订单的状态。[已解决]
谢谢!
【问题讨论】:
-
如果你在没有 cron 的情况下运行脚本,它会改变状态吗?如果不能,你能缩小导致问题的那条线吗?我认为问题可能出在 getModel 或 setState 上。
-
@Rachael:编辑了问题。问题在于
setState。 -
你在哪里添加这段代码?
-
@LouisW:我为 Cron 作业创建了一个模块。 IIRC 我在 /app/code/local/SOMENAME/Cron/Model/Observer.php 中添加了这段代码。但我不再从事这方面的工作,所以我记不太清了。
标签: php magento magento-1.6