Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento how do I programmatically ship orders?

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);
like image 746
snh_nl Avatar asked Jan 29 '12 19:01

snh_nl


People also ask

Which button is responsible for creating shipment in Magento?

Create a shipment On the Admin sidebar, go to Sales > Orders. Find the order in the grid and open it. If the order is paid, invoiced, and ready to ship, click Ship.

What is shipping method in Magento?

There are 3 basic Magento 2 shipping methods that you can choose for your eCommerce retail: Free Shipping, Flat Rate, and Table Rates. In general, Magento shipping methods are the price and method of shipping products to customers in the order management process.

How do I add multiple shipping methods in Magento 2?

Go to Store -> Configuration -> Sale -> Shipping Methods -> Magento Shipping. Set Yes at Enabled box to enable Table Rates shipping method. Into Title box enter the name of the shipping method which is visible to customers. Set Enabled API Logging field yes to define which API events are logged.


2 Answers

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.

like image 193
ShaunOReilly Avatar answered Oct 17 '22 14:10

ShaunOReilly


$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.

like image 20
Qin Wang Avatar answered Oct 17 '22 13:10

Qin Wang