Magento2 – How to Programmatically Update Shipment and Complete Order

magento-2.1magento2ordersshipmentshipping

I'm developing a procedure that has to programatically create shipment for orders that are already paid and invoiced.

The problem is that even after creating the shipment, the order status remains to 'processing' instead of going to 'complete'.
This does not happen if I manually Ship from the backend.

I found that the problem is that the quantity shipped for the order items is not updated, but remains 0 after saving the shipment and the order.

This is the procedure i'm using.
No exception is issued, and shipment is correctly created.

    $orders = $this->_orderCollectionFactory->create()
        ->addAttributeToSelect('*')
        ->addFieldToFilter( 'entity_id' , array('in' => $ordersIdsArr) )
        ->setOrder('created_at', 'desc' );   

    foreach ($orders as $index => $order) {
        if ($order->canShip()) { 
           $shipment = $this->_convertOrder->toShipment($order);;
           $orderItems = $order->getItemsCollection()->addAttributeToSelect('*')->load();

           foreach ($orderItems as $orderItem) {
                if (! $orderItem->getQtyToShip() || $orderItem->getIsVirtual()) {
                    continue;
                }
                $qtyShipped = $orderItem->getQtyToShip();
                $shipmentItem = $this->_convertOrder->itemToShipmentItem($orderItem)->setQty($qtyShipped);
                $shipment->addItem($shipmentItem); 
            }
            $shipment->register();
            $shipment->getOrder()->setIsInProcess(true);

            try {

                $saveTransaction = $this->_transactionFactory->create();
                $saveTransaction->addObject($shipment)
                    ->addObject($shipment->getOrder());
                $saveTransaction->save();
            } catch (\Exception $e) {

            }
        }
    }

    /*..........*/

Any clue?

Best Answer

After struggling for 2 days on this, trying to understand what the problem was, studying in deep also Magento core classes for module-sales, I found someone on Magento community who had similar problems with Magento API and developed a patch.

The problem is from one year ago, but doesn't seem to have been addressed in subsequent versions of Magento, so I decided to adopt the same solution as the extension does, hence forcing the shipped quantity of order items to be equal to the quantity to ship, save order rows and then, save the order again. Well, it is just a patch and dunno if it is a general problem, but to me it has been the only way to make this work, and get finally the order in status of 'Complete'.

I added this after the first save of the order:

try {
        $saveTransaction = $this->_transactionFactory->create();
        $saveTransaction->addObject($shipment)
             ->addObject($shipment->getOrder());
        $saveTransaction->save();

        $itemsCheck = $order->getItemsCollection()->addAttributeToSelect('*')->load();
        foreach ($itemsCheck as $item) {
            if (! $item->getQtyToShip() || $item->getIsVirtual()) { 
                   continue;
            }
            $item->setQtyShipped($item->getQtyToShip());
            $item->save();
            $Norder = $shipment->getOrder()->load( $shipment->getOrder()->getId() );
            $Norder->save();
        }
    } 

Hope it can be of help for someone else.

Related Topic