Magento how do I programmatically ship orders?
Asked Answered
R

2

7

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);
Remonstrant answered 29/1, 2012 at 19:13 Comment(0)
C
6

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.

Corder answered 29/1, 2012 at 23:55 Comment(13)
Many thanks @ShaunOReilly. It does pass the canship() ... but a next error occurs: order_not_exists code Trace:Order/Shipment/Api.php(142): Mage_Api_Model_Resource_Abstract->_fault('order_not_exist...') Sales/OrderController.php(45): Mage_Sales_Model_Order_Shipment_Api->create('121', Array, 'Shipment create...', true, true) codeRemonstrant
So now it crashes on $shipment->create. I tried both $order and orderId. Both not working. $order results in access violation $shipmentId = $shipment->create($order, array(),Remonstrant
Still error: order_not_exists. Strange, how can it be that we cannot find the right referenceRemonstrant
I don't understand how you can get a list of order_Ids, that do not exist. how many order ids's does the following return? $this->getRequest()->getPost('order_ids');Corder
array(1) { [0]=> string(3) "121" } - the order number that I see in the backend screen is 100000108. $order = Mage::getModel('sales/order')->loadByIncrementId($this->$orderId); var_dump($order); exit; shows error like object(Mage_Sales_Model_Order)#459 (29), etcRemonstrant
Answer to your question: array(1) { [0]=> string(3) "121" } - the order number that I see in the backend screen is 100000108. **************************** I tried this: $order = Mage::getModel('sales/order')->load($orderId); // var_export ($order); exit; if($order->canShip()) $shipmentId = $shipment->create($order, $itemQty, 'Shipment created through ShipMailInvoice', true, true); But error SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntaxRemonstrant
ok, can you step to the point where the sql error accors. we are getting closerCorder
Is there a way to export or package the local module? Maybe I can send it (only if you want) ...Remonstrant
Found interesting link in German, chekcing it out!! avs-webentwicklung.de/blog/artikel/…Remonstrant
It might be that shipping form OrderController is not easy because there is another class that executes this? Order/ShipmentController.phpRemonstrant
Got it! Need to get the Order Increment ID from Order. $shipment = Mage::getModel('sales/service_order', $order)->prepareShipment($itemQty); $shipment = new Mage_Sales_Model_Order_Shipment_Api(); // API nees OrderIncrementId, so change $lastOrderId = $order->getIncrementId(); $shipmentId = $shipment->create($lastOrderId, Now it works, thx!Remonstrant
You are getting order does not exist because the method used to get the order increment id is incorrect, it should be $order->getIncrementId() not $order->getorderIncrementIDWoald
@Corder can this automatically called shippment dispatch event ??Chairman
H
3
$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();

Working code for anyone who would like to:

  1. Create a shipment.
  2. Add shipping tracking information.
  3. And, change the shipment status to complete.

A lot thanks to ShaunOReilly.

Hardunn answered 14/5, 2013 at 11:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.