Magento how do I programmatically ship orders?

snh_nl picture snh_nl · Jan 29, 2012 · Viewed 10.1k times · Source

I am looking at some code to add MassAction in Magento and ship and complete multiple orders from sales_order/index

Somehow the orders are not being shipped.

It looks like (a perfectly normal order) is not passing the canship() test. Should it be passsed $order of $orderid?

Here's my code

//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);

Answer

ShaunOReilly picture ShaunOReilly · Jan 30, 2012

You need to load by ID, if you get orderID, or load by IncrementOrderId if, you actually get the Order incrementId.

Use this:

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

let us know if it worked.

And then :

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

Try that.