cancel
Showing results for 
Search instead for 
Did you mean: 

Programmatically Complete An Order

Programmatically Complete An Order

Guys, can any of you point out how we can "complete" an Order programmatically?  Basically,  create both the order's invoice and shipment.  

 

I found this code, I would like to know instead if this is the correct way of completing an order:

$order = $this->getOm()->create('Magento\Sales\Model\Order')->load($order_id);
if ($order->getId()) {
    $order->setStatus('complete')->save();
}

Its based on the order cancellation code below:

 

$order = $this->getOm()->create('Magento\Sales\Model\Order')->load($order_id);
if ($order->getId()) {
    $order->cancel()->save();
}

I am not quite sure how or what the process is to complete an Order.  I also need to add a comment onto the Invoice as well as the shipment for the said order.  

2 REPLIES 2

Re: Programmatically Complete An Order

How does \Magento\Sales\Model\ResourceModel\Order\Handler\State::check() work? https://mage2.pro/t/1409

Re: Programmatically Complete An Order

Hello rayzorMamon,

According to your example, you want your answer for Magento 1.9.x.

Here is example check it out and let me know if there is any issue.

  

$order  = Mage::getModel('sales/order')->load($orderId);
			$totalpaid = $order->getGrandTotal();	
			$order->setTotalDue(0);
			
			$orders = Mage::getModel('sales/order_invoice')->getCollection()
			->addAttributeToFilter('order_id', array('eq'=>$order->getId()));
			$orders->getSelect()->limit(1);
			
			if ((int)$orders->count() !== 0) {
				return $this;
			}
			
			if ($order->getState() == Mage_Sales_Model_Order::STATE_NEW || $order->getState() == Mage_Sales_Model_Order::STATE_PROCESSING) {
				try {
					if(!$order->canInvoice()) {
						$order->addStatusHistoryComment('Order cannot be invoiced.', false);
						$order->save();
					}
			
					//START Handle Invoice
					$invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice();
			
					$invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_OFFLINE);
					$invoice->register();
			
					$invoice->getOrder()->setCustomerNoteNotify(false);
					$invoice->getOrder()->setIsInProcess(true);
					$order->addStatusHistoryComment('Automatically INVOICED by Automation tool.', false);
			
					$transactionSave = Mage::getModel('core/resource_transaction')
					->addObject($invoice)
					->addObject($invoice->getOrder());
			
					$transactionSave->save();
					//END Handle Invoice
			
					//START Handle Shipment
					$shipment = $order->prepareShipment();
					$shipment->register();
			
					$order->setIsInProcess(true);
					$order->addStatusHistoryComment('Automatically SHIPPED by Automation tool.', false);
			
					$transactionSave = Mage::getModel('core/resource_transaction')
					->addObject($shipment)
					->addObject($shipment->getOrder())
					->save();
					//END Handle Shipment
				} catch (Exception $e) {
					$order->addStatusHistoryComment('Exception occurred during automaticallyInvoiceShipCompleteOrder action. Exception message: '.$e->getMessage(), false);
					$order->save();
				}

 Manish Kumar