Skip to content

Instantly share code, notes, and snippets.

@sukanta1983
Created March 21, 2015 05:32
Show Gist options
  • Save sukanta1983/f9c89561360013933504 to your computer and use it in GitHub Desktop.
Save sukanta1983/f9c89561360013933504 to your computer and use it in GitHub Desktop.
Create shipment programatically in magento
<?php
require_once 'app/Mage.php';
umask(0);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$userModel = Mage::getModel('admin/user');
$userModel->setUserId(0);
$orderId = $_GET['order_id'];
$trackno = $_GET['tracking_no'];
$carrier_code = $_GET['carrier_code'];
$shipmentCarrierTitle = $_GET['ship_title'];
$customerEmailComments = $_GET['comment'];
// $orderId = 22;
// $trackno = '1123445567';
// $carrier_code = 'usp'; //Carrier Codes can be like "ups" / "fedex" / "custom"
// $shipmentCarrierTitle = 'SPECIFIC_CARRIER_TITLE';
// $customerEmailComments = 'ANY COMMENTS FOR THE ORDER RECIPIENT CUSTOMER';
$order = Mage::getModel('sales/order')->load($orderId);
//$order = Mage::getModel('sales/order')->loadByIncrementId($orderIncrementId);
if (!$order->getId()) {
Mage::throwException("Order does not exist, for the Shipment process to complete");
}
if ($order->canShip()) {
try {
$shipment = Mage::getModel('sales/service_order', $order)
->prepareShipment(_getItemQtys($order));
$arrTracking = array(
'carrier_code' => isset($carrier_code) ? $carrier_code : $order->getShippingCarrier()->getCarrierCode(),
'title' => isset($shipmentCarrierTitle) ? $shipmentCarrierTitle : $order->getShippingCarrier()->getConfigData('title'),
'number' => $trackno,
);
$track = Mage::getModel('sales/order_shipment_track')->addData($arrTracking);
$shipment->addTrack($track);
// Register Shipment
$shipment->register();
// Save the Shipment
_saveShipment($shipment, $order, $customerEmailComments);
// Finally, Save the Order
_saveOrder($order);
echo "The order no" .$orderId. "has been shipped";
} catch (Exception $e) {
throw $e;
}
}
/**
* Get the Quantities shipped for the Order, based on an item-level
* This method can also be modified, to have the Partial Shipment functionality in place
*
* @param $order Mage_Sales_Model_Order
* @return array
*/
function _getItemQtys(Mage_Sales_Model_Order $order)
{
$qty = array();
foreach ($order->getAllItems() as $_eachItem) {
if ($_eachItem->getParentItemId()) {
$qty[$_eachItem->getParentItemId()] = $_eachItem->getQtyOrdered();
} else {
$qty[$_eachItem->getId()] = $_eachItem->getQtyOrdered();
}
}
return $qty;
}
function _saveShipment(Mage_Sales_Model_Order_Shipment $shipment, Mage_Sales_Model_Order $order, $customerEmailComments = '')
{
$shipment->getOrder()->setIsInProcess(true);
$transactionSave = Mage::getModel('core/resource_transaction')
->addObject($shipment)
->addObject($order)
->save();
$emailSentStatus = $shipment->getData('email_sent');
$ship_data = $shipment->getOrder()->getData();
$customerEmail = $ship_data['customer_email'];
if (!is_null($customerEmail) && !$emailSentStatus) {
$shipment->sendEmail(true, $customerEmailComments);
$shipment->setEmailSent(true);
}
return $this;
}
function _saveOrder(Mage_Sales_Model_Order $order)
{
$order->setData('state', Mage_Sales_Model_Order::STATE_COMPLETE);
$order->setData('status', Mage_Sales_Model_Order::STATE_COMPLETE);
$order->save();
return $this;
}
?>
@tarikuli
Copy link

tarikuli commented Nov 7, 2017

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment