How to make order shipment from code

As most of you know, there is an Order interface in Magento administration from where you can trigger order shipping. However, while developing certain module, you might wish to trigger this event from the code. I hope that this code snippet will help you in this process. In my case I have used this code to make shipment from cron after I got confirmation from fulfillment center
We will use Magento API to achieve it.
First, load order. I’ll use load “loadByIncrementId” method.
$order = Mage::getModel('sales/order')->loadByIncrementId($orderId);
Now I let’s make shipment:
if($order->canShip())
{
$itemQty = $order->getItemsCollection()->count();
$shipment = Mage::getModel('sales/service_order', $order)->prepareShipment($itemQty);
$shipment = new Mage_Sales_Model_Order_Shipment_Api();
$shipmentId = $shipment->create($orderId);
}
Order is now complete and order items statuses are “mixed”-> invoiced / shipped:
I think, code is quite self-explanatory, and I hope you will find it usefull, however,
remember not to use it on production sites without further testing.
10 comments
“Hello. It s a very interesting idea to create such extension. Well by the way, I find another way to do such functional. But it can be inconvenient ?? some cases.
First of all I use http://amasty.com/order-attributes.html extension for adding some attributes to the order.
And I add attribute “”Delivery Date”” on Shipping section on my checkout page. Of course there are no any restrictions and rules. Customer can just set a date which will suite him for delivery
I now, it sounds foolishly but it helps me 🙂
Thank you
Bob
Concrete and useful! So good 😀
Just saved me hours! Thanks!
For the people looking to add tracking you can use the following after you have run line 6 above:
Where ‘usps’ is the shipping method used (note this must be a method magneto already knows about) and ‘Tracking number’ is any descriptive text you want to use.
Like wise this will add a comment (the fase on the end prevents emails from being sent out)
Hopefully someone will find that useful too!
– Oliver
The last line “$shipmentId = $shipment->create($orderId);” leads to an error: There has been an error processing your request: order_not_exists
Is the OrderID the right variable the API wants?
nope, it’s $order->getIncrementId()
Hi,
I was reading up on the last three articles on how you do the following:
“Programmatically create order in Magento”
“How to make order shipment from code”
“How to extend Magento Order Grid?”
My dilemma is I am trying to create the following my already existing Delivery Date column which displays delivery date that is generated by by shipping method as attribute to populate in a new column called “Ship-Date” Now Ship Date needs to have a different set a rules which I wrote so a quick example of that would be if Next was ordered on Monday than is “Ship-Date” is Monday to be delived on Tuesday Not the greatest example there are other variables like Business days weekends 2 day shipping holidays no delivery monday’s etc but sort of an idea of what I was trying to accomplish….Do you have any thoughts?
Hi Thomas,
Great post. One question though, does the script include putting tracking number on it?
Thanks,
Janzell
Very Good Article.
Thanks
BharatMatrimony
Hi there, thank for the great post. Pardon me for asking if you have a download for this feature? Because I do not know why to put these codes and how to activate it using cron. Your reply is very much appreciated, thanks in advance.
What is the use of affecting “Mage::getModel(‘sales/service_order’, $order)->prepareShipment($itemQty);” to “$shipment” and just after affecting to the SAME variable a “new Mage_Sales_Model_Order_Shipment_Api();” ?
The “prepared shipment” looks not to be used…
Thanks.