How To Create Shipments Programmatically in Magento 2

Shipping is an important factor in any online store because it has a trong impact on customer experience ad conversions. Convenient shipping methods will be an incentive for customers to add products to the cart and complete the checkout.

In Magento 2, creating a shipment programmatically is the convenient way to add the new shipment as you need. Creating shipment sounds complex but if you use programatic way, it’s simple and time-saving. To do that, the following code is all required things you have to run on Magento 2 console.

Let’s follow the instruction and see how the results come out.

// Load the order increment ID
$order = $this->_objectManager->create('Magento\Sales\Model\Order')->loadByIncrementID($incrementid);

// OR
$order = $this->_objectManager->create('Magento\Sales\Model\Order')
    ->loadByAttribute('increment_id', '000000001');


//load by order 
$order = $this->_objectManager->create('Magento\Sales\Model\Order')
    ->load('1');

// Check if order can be shipped or has already shipped
if (! $order->canShip()) {
    throw new \Magento\Framework\Exception\LocalizedException(
                    __('You can\'t create an shipment.')
                );
}

// Initialize the order shipment object
$convertOrder = $this->_objectManager->create('Magento\Sales\Model\Convert\Order');
$shipment = $convertOrder->toShipment($order);

// Loop through order items
foreach ($order->getAllItems() AS $orderItem) {
    // Check if order item has qty to ship or is virtual
    if (! $orderItem->getQtyToShip() || $orderItem->getIsVirtual()) {
        continue;
    }

    $qtyShipped = $orderItem->getQtyToShip();

    // Create shipment item with qty
    $shipmentItem = $convertOrder->itemToShipmentItem($orderItem)->setQty($qtyShipped);

    // Add shipment item to shipment
    $shipment->addItem($shipmentItem);
}

// Register shipment
$shipment->register();

$shipment->getOrder()->setIsInProcess(true);

try {
    // Save created shipment and order
    $shipment->save();
    $shipment->getOrder()->save();

    // Send email
    $this->_objectManager->create('Magento\Shipping\Model\ShipmentNotifier')
        ->notify($shipment);

    $shipment->save();
} catch (\Exception $e) {
    throw new \Magento\Framework\Exception\LocalizedException(
                    __($e->getMessage())
                );
}

This is our guide Mageplaza highly recommended for you to create shipment programmatically. Hope it is helpful for your operation.

Final words

Let’s me know if you have any questions for encounter any issue while following this tutorial.

Happy coding!

Image Description
With over a decade of experience crafting innovative tech solutions for ecommerce businesses built on Magento, Jacker is the mastermind behind our secure and well-functioned extensions. With his expertise in building user-friendly interfaces and robust back-end systems, Mageplaza was able to deliver exceptional Magento solutions and services for over 122K+ customers around the world.

People also searched for

  • magento 2 create shipment programmatically
  • how to create a shipment magento 2
  • create shipment programmatically in magento 2
  • magento 2 create partial shipment programmatically
  • how to create shipment programmatically in magento 2
  • magento 2 create shipping label programmatically
  • create shipment programmatically magento 2
  • magento 2 create shipping method programmatically
  • magento 2 create order shipment programmatically
  • 2.3.x, 2.4.x
x