如何在magento中以编程方式进行自动发货?

时间:2013-02-20 15:24:00

标签: php magento

我尝试使用以下观察员代码。

...
    public function automaticallyInvoiceShipCompleteOrder($observer)
    {
        $order = $observer->getEvent()->getOrder();

        $orders = Mage::getModel('sales/order_invoice')->getCollection()
                        ->addAttributeToFilter('order_id', array('eq'=>$order->getId()));
        $orders->getSelect()->limit(1);
        if ((int)$orders->count() !== 0) {
            return $this;
        }
        try {
               if($order->canShip())
                {
                $itemQty =  $order->getItemsCollection()->count();
                $items[] = $itemQty;
                // This first definition and 2nd look overlapping, our one is obsolete?
                $shipment = Mage::getModel('sales/service_order', $order)->prepareShipment($itemQty);
                $ship = new Mage_Sales_Model_Order_Shipment_Api();
                $shipmentId = $ship->create($order->getId(), $items, 'Shipment created through ShipMailInvoice', true, true); 
//getting Error here

                }
              }
} catch (Exception $e) {
                $order->addStatusHistoryComment(' Exception occurred during automaticallyInvoiceShipCompleteOrder action. Exception message: '.$e->getMessage(), false);
                $order->save();
            }
return $this;
    }
.....

当我下订单时,我可以使用观察者捕获订单成功事件。最后在ajax调用中获取“Fatal error: Maximum function nesting level of '100' reached, aborting!”。

我找不到解决方案。请对此提出一些建议

3 个答案:

答案 0 :(得分:2)

每次保存订单时,都会调用此观察者方法,由于try块中的某些错误,该方法会再次保存您的订单。这就是我认为它将无休止地执行的原因,并且在第100次之后将会抛出致命错误。

在试用版的$ship->create()中,您需要传递Order Increment ID而不是Order Entity ID

答案 1 :(得分:1)

请参阅此link

几个月前我试过了它,它对我有用......这个家伙得到了一些关于magento的好教程......

HTH

答案 2 :(得分:0)

我尝试使用以下代码,

   public function automaticallyInvoiceShipCompleteOrder($observer)
{
    //$order = $observer->getEvent()->getOrder();
    $incrementid = $observer->getEvent()->getOrder()->getIncrementId();      
    $order = Mage::getModel('sales/order')->loadByIncrementId($incrementid);
        try {
            // Is the order shipable?
            if($order->canShip())
            {
             $shipmentid = Mage::getModel('sales/order_shipment_api')->create($order->getIncrementId(), array());
            }
            //END Handle Shipment
        } catch (Exception $e) {
            $order->addStatusHistoryComment(' Exception occurred during automaticallyInvoiceShipCompleteOrder action. Exception message: '.$e->getMessage(), false);
        }
        return $this;
}  

发货现已创建......