$shipment = Mage::getModel('sales/service_order', $order)->prepareShipment($itemQty);
$shipment = new Mage_Sales_Model_Order_Shipment_Api();
$shipmentId = $shipment->create($orderId);
3 Answers
I'm bad at translations, maybe this guide here https://www.scommerce-mage.com/blog/magento-2-how-to-create-shipment-programatically.html can help you
There isn't the same code
Only similar
/**
* Mark order as shipped
* @param \Magento\Sales\Model\Order $order
* @param boolean $doNotify
* @return bool
*/
public function markAsShipped($order, $doNotify = true)
{
if (!$order || !$order->canShip()) {
return;
}
try {
$orderShipment = $this->convertOrder->toShipment($order);
foreach ($order->getAllItems() as $orderItem) {
if (!$orderItem->getQtyToShip() || $orderItem->getIsVirtual()) {
continue;
}
$shipmentItem = $this->convertOrder
->itemToShipmentItem($orderItem)
->setQty($orderItem->getQtyToShip());
$orderShipment->addItem($shipmentItem);
}
$orderShipment->register();
$orderShipment->getOrder()->setIsInProcess(true);
$orderShipment->save();
$orderShipment->getOrder()->save();
if ($doNotify) {
$this->objectManager->create(\Magento\Shipping\Model\ShipmentNotifier::class)
->notify($orderShipment);
$orderShipment->save();
}
$order->addStatusToHistory($order->getStatus(), 'Order has been marked as complete');
$order->save();
return true;
} catch (\Exception $e) {
$this->logger->critical($e);
}
return false;
}
Try this code.
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$order = $objectManager->create('Magento\Sales\Model\Order')->load('1');
$convertOrder = $objectManager->create('Magento\Sales\Model\Convert\Order');
$shipment = $convertOrder->toShipment($order);
foreach ($order->getAllItems() AS $orderItem) {
if (! $orderItem->getQtyToShip() || $orderItem->getIsVirtual()) {
continue;
}
$qtyShipped = $orderItem->getQtyToShip();
$shipmentItem = $convertOrder->itemToShipmentItem($orderItem)->setQty($qtyShipped);
$shipment->addItem($shipmentItem);
}
$shipment->register();
$shipment->getOrder()->setIsInProcess(true);
try {
$shipment->save();
$shipment->getOrder()->save();
$shipment->save();
} catch (\Exception $e) {
throw new \Magento\Framework\Exception\LocalizedException(__($e->getMessage()));
}
-
devdocs.magento.com/guides/v2.3/extension-dev-guide/… Magento prohibits the direct use of the ObjectManager in your code because it hides the real dependencies of a class. Direct use of the create function prevents type validation and type hinting that a factory class provides. Feb 28, 2020 at 22:55
-
@Dominic Xigen thanks for details, thought its just a sample code for actual implementation Mar 1, 2020 at 16:04