如何在cron作业中以编程方式更改订单状态?

时间:2012-06-18 09:17:15

标签: php magento magento-1.6

我正在使用cron作业将所有“待定”网上银行订单更改为“待付款” (这是为了解决我的问题:Why is state not transitioning to "payment_pending" for orders cancelled at gateway?

这是我的代码 - 的 [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;
}

我已经检查过cron正在运行。但是州/地位并没有改变。我现在收到错误。

[已编辑] 现在问题 - 我在代码中得到标记为“out1”的输出,但不是“out2”

在改变代码之后,我已经看到,只要if条件为真,就会出现问题(上面)。这指出了行$order->setState('pending_payment', true)->save();的问题(我在if注释掉了另一行,问题仍然存在,但是如果我注释掉这行out2就会打印出来)。好像执行在这一行(无限循环?或某些内部问题?)

$order->setState('pending_payment', true)->save();出了什么问题?还有其他方法来完成上述事情吗?

我是否也可以按订单“创建时间”进行过滤,这样我就不会更改订单的状态,这是在几秒钟前创建的。 [已解决]

谢谢!

3 个答案:

答案 0 :(得分:0)

试着放$order->setState('pending_payment'); $order->setStatus('pending_payment'); $order->save(); 我认为您不会改变可能产生问题的订单状态。

答案 1 :(得分:0)

终于解决了我的问题。现在工作完全符合预期!

这是工作程序 -

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

希望这有助于某人。

答案 2 :(得分:0)

我还没有掌握Zend查询,所以我总是在与数据库直接交互时编写正确的MySQL查询。请注意,订单状态在Magento的新版本之前是不灵活的(除非您购买扩展程序)。

mysql> select status from sales_flat_order group by status;
+-----------------+
| status          |
+-----------------+
| canceled        | 
| closed          | 
| complete        | 
| pending         | 
| pending_payment | 
| processing      | 
+-----------------+
6 rows in set (0.00 sec)
mysql> select state from sales_flat_order group by state;

+-----------------+
| state           |
+-----------------+
| canceled        | 
| closed          | 
| complete        | 
| new             | 
| pending_payment | 
| processing      | 
+-----------------+
6 rows in set (1.03 sec)

为了帮助自己理解表结构,我写了一个php script to make a CSV of all the tables with fields。所以你可以用SELECT entity_id, state, status, created_at, increment_id FROM sales_flat_order WHERE status="pending"获得第一位。我看起来并没有在我的1.4.2安装中出现的任何字段名中看到“cod_fee”。请注意上述查询的输出格式:

SELECT entity_id, state, status, created_at, increment_id FROM sales_flat_order WHERE status="pending";
+-----------+------------+---------+---------------------+--------------+
| entity_id | state      | status  | created_at          | increment_id |
+-----------+------------+---------+---------------------+--------------+
|      2493 | new        | pending | 2011-09-14 18:09:47 | 200025332    | 
|      2683 | complete   | pending | 2011-10-04 17:19:07 | 200025523    | 
|      2686 | new        | pending | 2011-10-04 20:43:52 | 200025526    | 
|      3022 | processing | pending | 2011-11-15 01:11:34 | 200025849    | 
|      3428 | complete   | pending | 2012-01-12 17:56:57 | 200026236    | 
|      4493 | processing | pending | 2012-04-11 16:16:55 | 200027230    | 
|      5071 | new        | pending | 2012-05-21 18:05:43 | 200027759    | 
|      5091 | new        | pending | 2012-05-22 17:48:11 | 200027779    | 
   ...
|      5399 | new        | pending | 2012-06-14 17:46:46 | 200028069    | 
|      5443 | new        | pending | 2012-06-18 18:50:55 | 200028111    | 
|      5486 | new        | pending | 2012-06-20 21:18:24 | 200028152    | 
|      5491 | new        | pending | 2012-06-20 23:54:53 | 200028157    | 
+-----------+------------+---------+---------------------+--------------+
23 rows in set (0.79 sec)