In a loop creating shipments for orders. only for one order it is creating shipment after that throwing error like “registry key” already exists. How to overcome from this error.
$curentOrder = $this->orderFactory->create()->loadByIncrementId($mgOrdrNbr);
$shipment = $this->objectManagerInterface->create(’\Magento\Shipping\Controller\Adminhtml\Order\ShipmentLoader’);
$shipment->setOrderId($curentOrder->getId());
$shipment->setShipment($shipProducts);
$shipment->setTracking($shipmentTracking);
$loadShipment = $shipment->load();
$transaction = $this->objectManagerInterface->create(
‘Magento\Framework\DB\Transaction’
);
$transaction->addObject(
$loadShipment
)->addObject(
$loadShipment->getOrder()
)->save();
foreach ($curentOrder->getAllItems() as $_eachItem) {
foreach ($shipmentsInfo as $shippedProduct) {
if ($_eachItem->getSku() == $shippedProduct['InventoryId']) {
if ($_eachItem->getParentItemId()) {
$shipProducts['items'][$_eachItem->getParentItemId()] = $shippedProduct['ShippedQty'];
} else {
$shipProducts['items'][$_eachItem->getId()] = $shippedProduct['ShippedQty'];
}
if ($_eachItem->canShip()) {
$tobeShipped = $_eachItem->getQtyShipped() + $shippedProduct['ShippedQty'];
$_eachItem->setQtyShipped($tobeShipped);
$_eachItem->save();
}
$i++;
}
}
}
$curentOrder->save();