0
$shipment = Mage::getModel('sales/service_order', $order)->prepareShipment($itemQty);

$shipment = new Mage_Sales_Model_Order_Shipment_Api();
$shipmentId = $shipment->create($orderId);

3 Answers 3

0

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

0

There isn't the same code

Only similar

https://github.com/DominicWatts/AutoShipment/blob/d5a36a6b88c2753ef484b1a17a757985672af7b5/Helper/Shipment.php#L147-L189

    /**
     * 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;
    }
0

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()));
}
2
  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.