如何在cron作业中以编程方式更改订单状态? [英] How to change the order state programmatically inside a cron job?

查看:128
本文介绍了如何在cron作业中以编程方式更改订单状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用cron作业将所有待处理"的网上银行订单更改为待付款"
(这是为了解决我的问题:为什么对于在网关处取消的订单,状态为何不转换为"payment_pending"?) /p>

这是我的代码-

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是否正常工作.但是状态/状态没有改变.我现在没有遇到错误.

现在的问题- 我在代码中得到的输出标记为"out1",而不是"out2"

更改代码后,我发现,只要if条件为true,就会出现问题(上述).这指出了行$order->setState('pending_payment', true)->save();的问题(我已经注释掉了if中的另一行,并且问题仍然存在,但是如果我注释掉了,则会打印出out2行).似乎执行被卡在了这一行(无限循环?或某些内部问题?)

$order->setState('pending_payment', true)->save();怎么了?还有其他方法可以完成所说的事情吗?

我还可以按订单的创建时间"进行筛选,以便不更改几秒钟前创建的订单状态. [已解决]

谢谢!

解决方案

最终解决了我的问题.现在,这项工作完全可以按预期进行了!

这是工作程序-

  const MINUTES_DELAY = 15; //Orders younger than this are not changed
  const OUT_FILE = '/home/vinayak/cron.txt';

  public function run() {
    $old_time = time() - (self::MINUTES_DELAY*60);

    $out = "-------------------------------------------\n";
    $out .= date('d.m.y h:i:s A')."\n";

    $orders = Mage::getModel('sales/order')->getCollection()
      ->addFieldToFilter('status', 'pending')
      ->addFieldToFilter('cod_fee', array('null' => true))
      ;

    foreach ($orders as $order) {
      if (strtotime($order->getCreatedAt()) < $old_time)  {
        try{
          $id = $order->getIncrementId();

          Mage::getModel('sales/order')
            ->loadByIncrementId($id)
            ->setState('pending_payment', true)
            ->save();

          $out .= $id."\n";
        } catch (Exception $e)  {
          $out .= "Caught exception : ".$e->getMessage()."\n";
        }
      }
    }
    file_put_contents(self::OUT_FILE, $out, FILE_APPEND);
    return true;
  }

希望这对某人有帮助.

I am using a cron job to change all "pending" net-banking orders to "payment-pending"
(this is to solve my problem :
Why is state not transitioning to "payment_pending" for orders cancelled at gateway?)

This is my code- [EDITED]

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;
}

I have checked that cron is working. But the state/status is not changing. I am not getting error now.

[EDITED] Problem Now - I am getting output marked as "out1" in the code, but not the "out2"

After varying the code, I have seen that, whenever if condition is true the problem (above) occurs. This points out the problem with the line $order->setState('pending_payment', true)->save(); (I have commented out the other line in if, and the problem persists, but if I comment out this line out2 gets printed). It seems as if the execution gets stuck up at this line (infinite loop? or some internal problem?)

What's wrong with $order->setState('pending_payment', true)->save();? Any other way to accomplish the said thing?

Can I also filter by order "creation time", so that I don't change the state for order, which was created seconds ago.[SOLVED]

Thanks!

解决方案

Finally solved my problem. Now the job works exactly as intended!

This is the working program-

  const MINUTES_DELAY = 15; //Orders younger than this are not changed
  const OUT_FILE = '/home/vinayak/cron.txt';

  public function run() {
    $old_time = time() - (self::MINUTES_DELAY*60);

    $out = "-------------------------------------------\n";
    $out .= date('d.m.y h:i:s A')."\n";

    $orders = Mage::getModel('sales/order')->getCollection()
      ->addFieldToFilter('status', 'pending')
      ->addFieldToFilter('cod_fee', array('null' => true))
      ;

    foreach ($orders as $order) {
      if (strtotime($order->getCreatedAt()) < $old_time)  {
        try{
          $id = $order->getIncrementId();

          Mage::getModel('sales/order')
            ->loadByIncrementId($id)
            ->setState('pending_payment', true)
            ->save();

          $out .= $id."\n";
        } catch (Exception $e)  {
          $out .= "Caught exception : ".$e->getMessage()."\n";
        }
      }
    }
    file_put_contents(self::OUT_FILE, $out, FILE_APPEND);
    return true;
  }

Hope this helps someone.

这篇关于如何在cron作业中以编程方式更改订单状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆