4

I try to create programmatically a shipment of an order with a track code.
This is my current code :

$order = Mage::getModel('sales/order')->loadByIncrementId("100014930");
$shipment = Mage::getModel('sales/service_order', $order)->prepareShipment($order->getItemQtys());

$arrTracking = array(
                'carrier_code' => $order->getShippingCarrier()->getCarrierCode(),
                'title' => $order->getShippingCarrier()->getConfigData('title'),
                'number' => "1234",
            );

$track = Mage::getModel('sales/order_shipment_track')->addData($arrTracking);
$shipment->addTrack($track);

$shipment->register();
$order->save(); 

With this, I have no error while I execute it.
In the Magento panel, you can't ship the order, but the order stay in pending and the shipment doesn't appear.
Please help

Problem solved with this solution

  • Without any errors is hard to help you. Check all logs, also if logging is on and make sure display_errors is enabled – Sander Mangel Sep 8 '15 at 14:34
1

I think I've found your error. getItemQtys is not a valid function on any Magento class, so in Magento magic methods it returns null. Essentially you're trying to ship null number of items. You can't do that. Yes, it fails silently.

Some controllers implement a _getItemQtys method to return the values you're looking for; however none of these are plug and play. Instead I suggest you create this as it's just a keyed array in the format of:

array(
  'order_item_id'=>$qty,
  'order_item_id'=>$qty,
  'order_item_id'=>$qty
)

However, if you're always shipping all items in a shipment, without exception there is an even easier way:

$converter=Mage::getModel('sales/convert_order');
$shipment=$converter->toShipment($order);

Please read this answer here for more information on how to create shipments programmatically:

https://magento.stackexchange.com/a/3345/336

| improve this answer | |
  • I replaced the getItemsQty line with converter. When I execute, I got a Mage_Core_Exception "Cannot create an empty shipment" but when I var_dump the shipment (juste before saving it) I have all the information about it. Is it because loading the order like I did does make a Varien Object ? – grewn0uille Sep 8 '15 at 15:12
  • You needn't do anything (including save) just create the shipment with the $order. – philwinkle Sep 8 '15 at 15:17
  • So i don't keep the line with $shipment->register() ? Do i keep the addTrack() things ? Now just nothing is happening, no error, no shipment created. – grewn0uille Sep 8 '15 at 15:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.