Magento - Cron作业在添加订单状态更改功能时停止工作

时间:2016-07-19 12:54:28

标签: php magento cron

我正在使用Magento 1.9.2,我正在开发自定义扩展程序。

这是扩展程序的配置文件:

<?xml version="1.0"?>
<config>
<frontend>
    <layout>
        <updates>
            <automatedstatus>
                <file>automatedstatus.xml</file>
            </automatedstatus>
        </updates>
    </layout>
</frontend>
  <modules>
    <VivasIndustries_AutomatedStatus>
      <version>1.0.0</version>
    </VivasIndustries_AutomatedStatus>
  </modules>
  <global>
    <models>
        <automatedstatus>
            <class>VivasIndustries_AutomatedStatus_Model</class>
            <resourceModel>vivasindustries_automatedstatus_resource</resourceModel>
        </automatedstatus>
        <vivasindustries_automatedstatus_resource>
        <class>VivasIndustries_AutomatedStatus_Model_Resource</class>
        <entities>
            <automatedstatus>
            <table>VivasIndustries_AutomatedStatus</table>
            </automatedstatus>
        </entities>
        </vivasindustries_automatedstatus_resource>
    </models>
    <resources>
        <automatedstatus_setup>
            <setup>
                <module>VivasIndustries_AutomatedStatus</module>
            </setup>
            <connection>
                 <use>core_setup</use>
             </connection>
        </automatedstatus_setup>
        <automatedstatus_read>
            <connection>
                <use>core_read</use>
            </connection>
        </automatedstatus_read>
        <automatedstatus_write>
            <connection>
                <use>core_write</use>
            </connection>
        </automatedstatus_write>
    </resources>    
    <helpers>
        <automatedstatus>
            <class>VivasIndustries_AutomatedStatus_Helper</class>
        </automatedstatus>
    </helpers>
    <blocks>
        <automatedstatus>
             <class>VivasIndustries_AutomatedStatus_Block</class>
        </automatedstatus>
    </blocks>
  </global>
    <crontab>
        <jobs>
            <automatedstatus>
                <schedule>
                    <cron_expr>*/1 * * * *</cron_expr>
                </schedule>
                <run>
                    <model>automatedstatus/observer::setStatus</model>
                </run>
            </automatedstatus>
        </jobs>
    </crontab>  
  <adminhtml>
    <acl>
        <resources>
            <all>
                <title>Allow Everything</title>
            </all>
            <admin>
                <children>
                    <system>
                        <children>
                            <config>
                                <children>
                                    <automatedstatusadmin>
                                        <title>Vivas - All</title>
                                    </automatedstatusadmin>
                                </children>
                            </config>
                        </children>
                    </system>
                </children>
            </admin>
        </resources>
    </acl>  
    </adminhtml>
    <admin>
        <routers>
            <adminhtml>
                <args>
                    <modules>
                        <VivasIndustries_AutomatedStatus before="Mage_Adminhtml">VivasIndustries_AutomatedStatus_Adminhtml</VivasIndustries_AutomatedStatus>
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>
</config> 

出于开发目的,我已将cron作业设置为每分钟运行一次 这是我的observer.php     

class VivasIndustries_AutomatedStatus_Model_Observer
{
   public function setStatus() {
        Mage::log("The order status was changed!");
    }
}

就像每一分钟一样,我可以通过看到The order status was changed!文件中添加system.log消息来完成cron作业

但是,当我将其更改为:

<?php

class VivasIndustries_AutomatedStatus_Model_Observer
{
   public function setStatus() {
        $orderId = "100005082";
        $order = Mage::getModel('sales/order')->load($orderId);
        $order->setState(Mage_Sales_Model_Order::STATE_COMPLETE, true);
        $order->save();
        Mage::log("The order status was changed!");
    }
}

system.log文件中,不再添加The order status was changed!消息,订单状态也不会更改。

我很确定问题来自订单状态更改的代码。

我的错误在哪里可以解决?

提前致谢!

2 个答案:

答案 0 :(得分:0)

您必须使用loadByIncrementId()而不是load()。

$order = Mage::getModel('sales/order')->loadByIncrementId($orderId);

实际上,在Magento中,订单状态“完成”不得手动设置。它要求订单首先创建发票和发货。如果你真的想把它标记为完整,你可以按照以下步骤进行:

public function setStatus() {
    $orderId = "100005082";
    $order = Mage::getModel('sales/order')->loadByIncrementId($orderId);
    if (!$order->getId()) {
        return false;
    }

    if (!$order->canInvoice()) {
        return false;
    }

    $savedQtys = array();
    $invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice($savedQtys);
    if (!$invoice->getTotalQty()) {
        return false;
    }
    $invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_OFFLINE);
    //create invoice
    $invoice->register();

    $invoice->getOrder()->setCustomerNoteNotify(false);
    $invoice->getOrder()->setIsInProcess(true);

    $transactionSave = Mage::getModel('core/resource_transaction')
            ->addObject($invoice)
            ->addObject($invoice->getOrder());

    $transactionSave->save();

    //create shipment
    $shipment = $order->prepareShipment();
    if ($shipment) {
        $shipment->register();
        $order->setIsInProcess(true);

        $transaction_save = Mage::getModel('core/resource_transaction')
                ->addObject($shipment)
                ->addObject($shipment->getOrder())
                ->save();
    }
}

然后订单状态将自动设置为COMPLETE。

答案 1 :(得分:0)

当你使用$ order-&gt; setState(Mage_Sales_Model_Order :: STATE_COMPLETE,true); Magento抛出异常“订单状态”完成“不得手动设置”。

因此您需要手动更改设置状态的方式。请试试这个

$orderId = "100005082";
$order = Mage::getModel('sales/order')->loadByIncrementId($orderId);
$order->setData('state',Mage_Sales_Model_Order::STATE_COMPLETE);
$order->addStatusToHistory(Mage_Sales_Model_Order::STATE_COMPLETE);
$order->save();