Magento2 Create Shipment for Order Direclty By PHP code

Kindly i am share with you the code of how to make partial Shipment for existing Order in Magento2

I will write the code that can be executed  external of magento files

First Create new file and name it as you want , For me i named it (create-shipment-magento2.php)

add this code to the file

<?php
require __DIR__ . '/app/bootstrap.php';
class ZeoApp
    extends \Magento\Framework\App\Http
    implements \Magento\Framework\AppInterface 
    {
    protected $shipmentLoader;
    
    public function launch()
    {

        //the method must end with this line
        return $this->_response;
    }

    public function catchException(\Magento\Framework\App\Bootstrap $bootstrap, \Exception $exception)
    {
        return false;
    }

}
// Start your App
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$app = $bootstrap->createApplication('ZeoApp');
$bootstrap->run($app);

in this code we make new application of magento to run our custom application.

Next Step is to add our code to create the shipment inside lunch function, Please add this code

 

 $this->_state->setAreaCode("frontend");
        $this->shipmentLoader=$this->_objectManager->create('\Magento\Shipping\Controller\Adminhtml\Order\ShipmentLoader');
        
        $increment_id="2000000008";
        $items=array();
        $items[]=array("order_item_id"=>21,"qty"=>1);
        
        // Shipment Data
        $data = array ();
        $data["increment_id"]=$increment_id;
        $data ["items"]=array();
        foreach ($items as $item){
            
            $data["items"][$item["order_item_id"]]=$item["qty"];
        }
       /* $data ["items"] = array (
                "21" => 1
        );
        */
        $data ["comment_text"] = "Comment by Zeo";
        $data ['comment_customer_notify']=true;
        $data ['comment_customer_notify']=true;
        $data ['is_visible_on_front']=true;
        $data ['tracks']=array(
                array("carrier_code"=>"custom","title"=>"Carrier Title","number"=>"10")
                );
                
        $tracks=$data["tracks"];
        
        $order = $this->_objectManager->create ( '\Magento\Sales\Model\Order' );
        $order->loadByIncrementId ( $data["increment_id"] );
        $order_id = $order->getEntityId ();
            $this->shipmentLoader->setOrderId ( $order_id );
            $this->shipmentLoader->setShipmentId ( null );
            $this->shipmentLoader->setShipment ( $data );
            $this->shipmentLoader->setTracking($tracks);
        
            $shipment = $this->shipmentLoader->load ();
            
            if (! empty ( $data ['comment_text'] )) {
                $shipment->addComment ( $data ['comment_text'], isset ( $data ['comment_customer_notify'] ), isset ( $data ['is_visible_on_front'] ) );
            
                $shipment->setCustomerNote ( $data ['comment_text'] );
                $shipment->setCustomerNoteNotify ( isset ( $data ['comment_customer_notify'] ) );
            }
    
            $shipment->register ();
            $transaction = $this->_objectManager->create ( 'Magento\Framework\DB\Transaction' );
            $transaction->addObject ( $shipment )->addObject ( $shipment->getOrder () )->save ();
            
            
      echo $shipment->getIncrementId();

The last code is create new shipment for one product of order [#2000000008]

the item_id is [21] and the Quantity is [1]

The full code will be like this 

 

<?php
require __DIR__ . '/app/bootstrap.php';
class ZeoApp
    extends \Magento\Framework\App\Http
    implements \Magento\Framework\AppInterface 
    {
    protected $shipmentLoader;
    
    public function launch()
    {
        $this->_state->setAreaCode("frontend");
        $this->shipmentLoader=$this->_objectManager->create('\Magento\Shipping\Controller\Adminhtml\Order\ShipmentLoader');
        
        $increment_id="2000000008";
        $items=array();
        $items[]=array("order_item_id"=>21,"qty"=>1);
        
        // Shipment Data
        $data = array ();
        $data["increment_id"]=$increment_id;
        $data ["items"]=array();
        foreach ($items as $item){
            
            $data["items"][$item["order_item_id"]]=$item["qty"];
        }
       /* $data ["items"] = array (
                "21" => 1
        );
        */
        $data ["comment_text"] = "Comment by Zeo";
        $data ['comment_customer_notify']=true;
        $data ['comment_customer_notify']=true;
        $data ['is_visible_on_front']=true;
        $data ['tracks']=array(
                array("carrier_code"=>"custom","title"=>"Carrier Title","number"=>"10")
                );
                
        $tracks=$data["tracks"];
        
        $order = $this->_objectManager->create ( '\Magento\Sales\Model\Order' );
        $order->loadByIncrementId ( $data["increment_id"] );
        $order_id = $order->getEntityId ();
            $this->shipmentLoader->setOrderId ( $order_id );
            $this->shipmentLoader->setShipmentId ( null );
            $this->shipmentLoader->setShipment ( $data );
            $this->shipmentLoader->setTracking($tracks);
        
            $shipment = $this->shipmentLoader->load ();
            
            if (! empty ( $data ['comment_text'] )) {
                $shipment->addComment ( $data ['comment_text'], isset ( $data ['comment_customer_notify'] ), isset ( $data ['is_visible_on_front'] ) );
            
                $shipment->setCustomerNote ( $data ['comment_text'] );
                $shipment->setCustomerNoteNotify ( isset ( $data ['comment_customer_notify'] ) );
            }
    
            $shipment->register ();
            $transaction = $this->_objectManager->create ( 'Magento\Framework\DB\Transaction' );
            $transaction->addObject ( $shipment )->addObject ( $shipment->getOrder () )->save ();
            
            
      echo $shipment->getIncrementId();
        
        
        //the method must end with this line
        return $this->_response;
    }

    public function catchException(\Magento\Framework\App\Bootstrap $bootstrap, \Exception $exception)
    {
        return false;
    }

}
// Start your App
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$app = $bootstrap->createApplication('ZeoApp');
$bootstrap->run($app);

 

you can download the file from Github

https://github.com/zexperto/magento2-direct-code/blob/master/create-shipment-magento2.php

Thank you for reading this wiki