Magento我如何以编程方式发送订单?

时间:2012-01-29 19:13:28

标签: php magento magento-1.5

我正在查看一些代码,以便在Magento中添加MassAction并从sales_order/index发送和完成多个订单

不知何故订单没有发货。

看起来(完全正常的顺序)没有通过canship()测试。它是否应该$order传递$orderid

这是我的代码

//Get orderids
$orderIds = $this->getRequest()->getPost('order_ids');

//verify if the array is not empty
if (!empty($orderIds)) {
//loop through orders
foreach ($orderIds as $orderId) {

// Dont know what this does
$order = Mage::getModel('sales/order')->loadByIncrementId($orderId);

// Is the order shipable?
if($order->canShip())
{
$itemQty =  $order->getItemsCollection()->count();
// This first definition and 2nd look overlapping, our one is obsolete?
$shipment = Mage::getModel('sales/service_order', $order)->prepareShipment($itemQty);
$shipment = new Mage_Sales_Model_Order_Shipment_Api();

// But still, no shipment, why?
$shipmentId = $shipment->create($orderId, array(), 'Shipment created through ShipMailInvoice', true, true);

2 个答案:

答案 0 :(得分:6)

您需要按ID加载,如果您获得orderID,或者通过IncrementOrderId加载,如果您实际获得Order incrementId。

使用此:

  

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

让我们知道它是否有效。

然后:

$shipmentId = $shipment->create($order->getIncrementId(), $itemQty, 'Shipment created through ShipMailInvoice', true, true);

试试。

答案 1 :(得分:1)

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

//create shipment
$itemQty =  $order->getItemsCollection()->count();
$shipment = Mage::getModel('sales/service_order', $order)->prepareShipment($itemQty);
$shipment = new Mage_Sales_Model_Order_Shipment_Api();
$shipmentId = $shipment->create( $order->getIncrementId(), array(), 'Shipment created through ShipMailInvoice', true, true);

//add tracking info
$shipment_collection = Mage::getResourceModel('sales/order_shipment_collection');
$shipment_collection->addAttributeToFilter('order_id', $orderId);
foreach($shipment_collection as $sc)
{
$shipment = Mage::getModel('sales/order_shipment');
$shipment->load($sc->getId());
                                if($shipment->getId() != '')
                                { 
                                try
                                {
                                     Mage::getModel('sales/order_shipment_track')
                                     ->setShipment($shipment)
                                     ->setData('title', 'carrier')
                                     ->setData('number', $trackInfo)
                                     ->setData('carrier_code', 'custom')
                                     ->setData('order_id', $shipment->getData('order_id'))
                                     ->save();

                                }catch (Exception $e)
                                {
                                    Mage::getSingleton('core/session')->addError('order id '.$orderId.' no found');
                                }
                                }
                        }
// change order status to complete
                        $order->addStatusToHistory(Mage_Sales_Model_Order::STATE_COMPLETE);
                        $order->setData('state', Mage_Sales_Model_Order::STATE_COMPLETE);
                        $order->save();

任何想要的人的工作代码:

  1. 创建货件。
  2. 添加送货跟踪信息。
  3. 并将货件状态更改为完成。
  4. 非常感谢ShaunOReilly。