2

How to create shipment programmatically in magento 2 via script I have order id & i want to generate shipment can someone provide direct script to run from root folder

0

2 Answers 2

4

Check this article to: Programmatically Create Shipment In MSI

I used this script in many live projects and it's working without any issue. You can use orderId or increment id in this script.

<?php

use \Magento\Framework\App\Bootstrap;

include('app/bootstrap.php');

$bootstrap = Bootstrap::create(BP, $_SERVER);

$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$orderInterface = $objectManager->get('\Magento\Sales\Api\Data\OrderInterface');


//Use this if you have orderId
//$orderId = "100"; //Order Id
//$order = $orderInterface->load($orderId);
 
$incrementId = "000000165"; //Increment Id
$order = $objectManager->create('Magento\Sales\Model\Order')
        ->loadByAttribute('increment_id', $incrementId);

if ($order->canShip()) {
    // Initialize the order shipment object
    $convertOrder = $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
        //$objectManager->create('Magento\Shipping\Model\ShipmentNotifier')
        //    ->notify($shipment);
        //$shipment->save();
    } catch (\Exception $e) {
       echo "Shipment Not Created". $e->getMessage(); exit;
    }

    echo "Shipment Succesfully Generated for order: #".$incrementId;
} else {
    echo "Shipment Not Created Becuase It's already created or something went wrong";
}
2
  • Hello Prince. Do you know about how to manage different list.phtml in each category? Commented Sep 19, 2020 at 17:24
  • What if I want to include the tracking number and provider with the shipment? Commented Jun 12, 2024 at 21:50
0

Use following code to create shipment programatically.

<?php
use Magento\Framework\App\Bootstrap;

/**
 * If your external file is in root folder
 */
require __DIR__ . '/app/bootstrap.php';

/**
 * If your external file is NOT in root folder
 * Let's suppose, your file is inside a folder named 'xyz'
 *
 * And, let's suppose, your root directory path is
 * /var/www/html/magento2
 */
// $rootDirectoryPath = '/var/www/html/magento2';
// require $rootDirectoryPath . '/app/bootstrap.php';

$params = $_SERVER;

$bootstrap = Bootstrap::create(BP, $params);

$_objectManager = $bootstrap->getObjectManager();   

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

// 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 = $_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
    $_objectManager->create('Magento\Shipping\Model\ShipmentNotifier')
        ->notify($shipment);

    $shipment->save();
} catch (\Exception $e) {
    throw new \Magento\Framework\Exception\LocalizedException(
                    __($e->getMessage())
                );
}
4
  • You can put above file in root directory. Commented Mar 23, 2018 at 9:47
  • error : ! ) Fatal error: Uncaught Magento\Framework\Exception\LocalizedException: You can't create an shipment. in /var/www/magento2/shipment.php on line 30 ( ! ) Magento\Framework\Exception\LocalizedException: You can't create an shipment. in /var/www/magento2/shipment.php on line 30 Commented Mar 23, 2018 at 9:58
  • That order may be already shipped. Can you check with another order id ? Commented Mar 23, 2018 at 10:32
  • @Kishan how do I add tracking number before save the shipment? (one tracking per order) Commented Apr 10, 2019 at 8:16

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.