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
2 Answers
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";
}
-
Hello Prince. Do you know about how to manage different list.phtml in each category?Mehta Prem– Mehta Prem2020-09-19 17:24:25 +00:00Commented Sep 19, 2020 at 17:24
-
What if I want to include the tracking number and provider with the shipment?David Stone– David Stone2024-06-12 21:50:07 +00:00Commented Jun 12, 2024 at 21:50
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())
);
}
-
You can put above file in root directory.Kishan Patadia– Kishan Patadia2018-03-23 09:47:17 +00:00Commented 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 30Mark henry– Mark henry2018-03-23 09:58:06 +00:00Commented Mar 23, 2018 at 9:58
-
That order may be already shipped. Can you check with another order id ?Kishan Patadia– Kishan Patadia2018-03-23 10:32:02 +00:00Commented Mar 23, 2018 at 10:32
-
@Kishan how do I add tracking number before save the shipment? (one tracking per order)Mage Explorer– Mage Explorer2019-04-10 08:16:32 +00:00Commented Apr 10, 2019 at 8:16