Ship and Complete an Order

$orderId = 100000105;
completeAndShip($orderId);
  function completeAndShip($orderId){
        $email = true;
        $trackingNum = '';
        $carrier = 'custom';
        $includeComment = false;
        $comment = "Order Completed And Shipped Automatically via Automation Routines";
        $order = Mage::getModel('sales/order')->loadByIncrementId($orderId);
        
        $convertor = Mage::getModel('sales/convert_order');
        $shipment = $convertor->toShipment($order);
        foreach ($order->getAllItems() as $orderItem) {
            
            if (!$orderItem->getQtyToShip()) {
                continue;
            }
            if ($orderItem->getIsVirtual()) {
                continue;
            }
            $item = $convertor->itemToShipmentItem($orderItem);
            $qty = $orderItem->getQtyToShip();
            $item->setQty($qty);
            $shipment->addItem($item);
        }
        $carrierTitle = NULL;
        if ($carrier == 'custom') {
            $carrierTitle = 'Playtimes';
        }
        $data = array();
        $data['carrier_code'] = $carrier;
        $data['title'] = $carrierTitle;
        $data['number'] = $trackingNum;
        $track = Mage::getModel('sales/order_shipment_track')->addData($data);
        $shipment->addTrack($track);
        $shipment->register();
        $shipment->addComment($comment, $email && $includeComment);
        $shipment->setEmailSent(true);
        $shipment->getOrder()->setIsInProcess(true);
        $transactionSave = Mage::getModel('core/resource_transaction')
            ->addObject($shipment)
            ->addObject($shipment->getOrder())
            ->save();
        $shipment->sendEmail($email, ($includeComment ? $comment : ''));
        $order->setStatus('Complete');
        $order->addStatusToHistory($order->getStatus(), 'Order Completed And Shipped Automatically via Automation Routines', false);
        
        $shipment->save();
}
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