Partial Magento order item shipment

<?php

ini_set(‘display_errors’, 1);
ini_set(‘display_startup_errors’, 1);
error_reporting(-1);

require_once ‘../../../app/Mage.php’;
Mage::app();
 
function GetOrderItemId($order_items, $sku) {
    
    $itemInfo=array();
    foreach ($order_items as $order_item) {
        if ($order_item->getSku() == $sku) {
            $itemID  = $order_item->getItemId();
            $itemQty = $order_item->getQtyOrdered();
            $itemInfo[]=$itemID;
            $itemInfo[]=$itemQty;
            return $itemInfo;
        }   
    }
    return 0;
}
 
function CreateShipment($order_number, $allocations)
{
    // Init
    $order = null;
 
    // Anticipate Errors
    try
    {
        // Load Order
        $order = Mage::getModel(‘sales/order’)->loadByIncrementId($order_number);
 
        // Get Order Items
        $order_items = $order->getItemsCollection();
 
        // Parepare Item Qtys For Shipment
        $item_qtys = array();
        $item_qtys = array();
        foreach ($allocations as $allocation) {
        
        $itemInfo=GetOrderItemId($order_items, $allocation[‘sku’]);
        
            $item_qtys[$itemInfo[0]] = $itemInfo[1];
        }
        
        
        
      
 
        // Create Shipment
        $shipment = $order->prepareShipment($item_qtys);
        $shipment->register();
        $shipment->sendEmail(false)
                 ->setEmailSent(false)
                 ->save();
        $transactionSave = Mage::getModel(‘core/resource_transaction’)
                           ->addObject($shipment)
                           ->addObject($shipment->getOrder())
                           ->save();
 
        // Update Magento Order State/Status to Processing/Sent To Picking
        $order->setStatus(‘sent_to_picking’)
              ->save();
        $order->addStatusHistoryComment(‘Sent to warehouse for picking.’)
              ->save();
 
        // Success
        return $shipment->getIncrementId();
    }
    catch (Exception $e)
    {
        // Log Error On Order Comment History
        if (null !== $order->getIncrementId()) {
            $order->addStatusHistoryComment(‘Failed to create shipment – ‘. $e->getMessage())
                  ->save();
        }
 
        // Error
        return false;
    }
}
 
// TEST
$order_number = ‘100012516’;
$allocations = array(
    array(‘sku’ => ‘WILD-125’, ‘allocated_qty’ => 1),
    array(‘sku’ => ‘RD02SPRUCE’, ‘allocated_qty’ => 1),
    
    
);
var_dump(CreateShipment($order_number, $allocations));
 
?>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s