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?