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);
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.
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.
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.
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.
$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:
A lot thanks to ShaunOReilly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With